def test_must_skip_invalid_swagger(self, SwaggerEditorMock): event_id = "id" properties = { "RestApiId": { "Ref": "restid" }, "Path": "/hello", "Method": "GET" } original_swagger = {"this": "is", "valid": "swagger"} mock_api = SamResource({ "Type": "AWS::Serverless::Api", "Properties": { "DefinitionBody": original_swagger, "a": "b" } }) SwaggerEditorMock.is_valid = Mock() SwaggerEditorMock.is_valid.return_value = False template_mock = Mock() template_mock.get = Mock() template_mock.set = Mock() template_mock.get.return_value = mock_api self.plugin._add_api_to_swagger(event_id, properties, template_mock) SwaggerEditorMock.is_valid.assert_called_with(original_swagger) template_mock.get.assert_called_with('restid') SwaggerEditorMock.assert_not_called() template_mock.set.assert_not_called()
def test_must_add_path_method_to_swagger_of_api_resource(self, SwaggerEditorMock): event_id = "id" properties = {"RestApiId": {"Ref": "restid"}, "Path": "/hello", "Method": "GET"} original_swagger = {"this": "is", "valid": "swagger"} updated_swagger = "updated swagger" mock_api = SamResource( { "Type": "AWS::Serverless::Api", "Properties": {"__MANAGE_SWAGGER": True, "DefinitionBody": original_swagger, "a": "b"}, } ) SwaggerEditorMock.is_valid = Mock() SwaggerEditorMock.is_valid.return_value = True editor_mock = Mock() SwaggerEditorMock.return_value = editor_mock editor_mock.swagger = updated_swagger self.plugin.editor = SwaggerEditorMock template_mock = Mock() template_mock.get = Mock() template_mock.set = Mock() template_mock.get.return_value = mock_api self.plugin._add_api_to_swagger(event_id, properties, template_mock) SwaggerEditorMock.is_valid.assert_called_with(original_swagger) template_mock.get.assert_called_with("restid") editor_mock.add_path("/hello", "GET") template_mock.set.assert_called_with("restid", mock_api) self.assertEqual(mock_api.properties["DefinitionBody"], updated_swagger)
def test_must_skip_if_definition_body_is_not_present( self, SwaggerEditorMock): event_id = "id" properties = { "RestApiId": { "Ref": "restid" }, "Path": "/hello", "Method": "GET" } mock_api = SamResource({ "Type": "AWS::Serverless::Api", "Properties": { "DefinitionUri": "s3://bucket/key", } }) SwaggerEditorMock.is_valid = Mock() SwaggerEditorMock.is_valid.return_value = False template_mock = Mock() template_mock.get = Mock() template_mock.set = Mock() template_mock.get.return_value = mock_api self.plugin._add_api_to_swagger(event_id, properties, template_mock) SwaggerEditorMock.is_valid.assert_called_with(None) template_mock.get.assert_called_with('restid') SwaggerEditorMock.assert_not_called() template_mock.set.assert_not_called()
def test_must_collect_errors_and_raise_on_invalid_events(self, SamTemplateMock): template_dict = {"a": "b"} function_resources = [("id1", "function1"), ("id2", "function2"), ("id3", "function3")] api_event_errors = [InvalidEventException("eventid1", "msg"), InvalidEventException("eventid3", "msg"), InvalidEventException("eventid3", "msg")] sam_template = Mock() SamTemplateMock.return_value = sam_template sam_template.set = Mock() sam_template.iterate = Mock() sam_template.iterate.return_value = function_resources self.plugin._get_api_events.return_value = ["1", "2"] self.plugin._process_api_events.side_effect = api_event_errors with self.assertRaises(InvalidDocumentException) as context: self.plugin.on_before_transform_template(template_dict) # Verify the content of exception. There are two exceptions embedded one inside another # InvalidDocumentException -> InvalidResourceException -> contains the msg from InvalidEventException causes = context.exception.causes self.assertEquals(3, len(causes)) for index, cause in enumerate(causes): self.assertTrue(isinstance(cause, InvalidResourceException)) # Resource's logicalID must be correctly passed self.assertEquals(function_resources[index][0], cause._logical_id) # Message must directly come from InvalidEventException self.assertEquals(api_event_errors[index].message, cause._message) # Must cleanup even if there an exception self.plugin._maybe_remove_implicit_api.assert_called_with(sam_template)
def test_must_skip_if_api_resource_properties_are_invalid( self, SwaggerEditorMock): event_id = "id" properties = { "RestApiId": { "Ref": "restid" }, "Path": "/hello", "Method": "GET" } mock_api = SamResource({ "Type": "AWS::Serverless::Api", "Properties": "this is not a valid property" }) SwaggerEditorMock.is_valid = Mock() template_mock = Mock() template_mock.get = Mock() template_mock.set = Mock() template_mock.get.return_value = mock_api self.plugin._add_api_to_swagger(event_id, properties, template_mock) SwaggerEditorMock.is_valid.assert_not_called() template_mock.get.assert_called_with('restid') SwaggerEditorMock.assert_not_called() template_mock.set.assert_not_called()
def test_must_process_functions(self, SamTemplateMock): template_dict = {"a": "b"} function1 = SamResource({"Type": "AWS::Serverless::Function"}) function2 = SamResource({"Type": "AWS::Serverless::Function"}) function3 = SamResource({"Type": "AWS::Serverless::Function"}) function_resources = [("id1", function1), ("id2", function2), ("id3", function3)] api_events = ["event1", "event2"] sam_template = Mock() SamTemplateMock.return_value = sam_template sam_template.set = Mock() sam_template.iterate = Mock() sam_template.iterate.return_value = function_resources self.plugin._get_api_events.return_value = api_events self.plugin.on_before_transform_template(template_dict) SamTemplateMock.assert_called_with(template_dict) sam_template.set.assert_called_with(IMPLICIT_API_LOGICAL_ID, ImplicitApiResource().to_dict()) # Make sure this is called only for Functions and State Machines sam_template.iterate.assert_any_call({"AWS::Serverless::Function", "AWS::Serverless::StateMachine"}) sam_template.iterate.assert_any_call({"AWS::Serverless::Api"}) self.plugin._get_api_events.assert_has_calls([call(function1), call(function2), call(function3)]) self.plugin._process_api_events.assert_has_calls( [ call(function1, ["event1", "event2"], sam_template, None, None, None), call(function2, ["event1", "event2"], sam_template, None, None, None), call(function3, ["event1", "event2"], sam_template, None, None, None), ] ) self.plugin._maybe_remove_implicit_api.assert_called_with(sam_template)
def test_fails_number_more_than_2(self): r = Mock() r.get = Mock(return_value=1) r.set = Mock(return_value=True) r.zadd = Mock(return_value=True) casino = Casino(r) self.assertRaises(casino.escoger_juego(3))
def test_must_skip_if_api_manage_swagger_flag_is_not_present(self, SwaggerEditorMock): event_id = "id" properties = { "RestApiId": {"Ref": "restid"}, "Path": "/hello", "Method": "GET" } original_swagger = {"this": "is a valid swagger"} mock_api = SamResource({ "Type": "AWS::Serverless::Api", "Properties": { "DefinitionBody": original_swagger, "StageName": "prod", # __MANAGE_SWAGGER flag is *not* present } }) SwaggerEditorMock.is_valid = Mock() template_mock = Mock() template_mock.get = Mock() template_mock.set = Mock() template_mock.get.return_value = mock_api self.plugin._add_api_to_swagger(event_id, properties, template_mock) SwaggerEditorMock.is_valid.assert_called_with(original_swagger) template_mock.get.assert_called_with('restid') SwaggerEditorMock.assert_not_called() template_mock.set.assert_not_called()
def test_must_skip_if_api_resource_properties_are_invalid(self, SwaggerEditorMock): event_id = "id" properties = { "RestApiId": {"Ref": "restid"}, "Path": "/hello", "Method": "GET" } mock_api = SamResource({ "Type": "AWS::Serverless::Api", "Properties": "this is not a valid property" }) SwaggerEditorMock.is_valid = Mock() template_mock = Mock() template_mock.get = Mock() template_mock.set = Mock() template_mock.get.return_value = mock_api self.plugin._add_api_to_swagger(event_id, properties, template_mock) SwaggerEditorMock.is_valid.assert_not_called() template_mock.get.assert_called_with('restid') SwaggerEditorMock.assert_not_called() template_mock.set.assert_not_called()
def test_must_skip_if_api_manage_swagger_flag_is_not_present( self, SwaggerEditorMock): event_id = "id" properties = { "RestApiId": { "Ref": "restid" }, "Path": "/hello", "Method": "GET" } original_swagger = {"this": "is a valid swagger"} mock_api = SamResource({ "Type": "AWS::Serverless::Api", "Properties": { "DefinitionBody": original_swagger, "StageName": "prod", # __MANAGE_SWAGGER flag is *not* present } }) SwaggerEditorMock.is_valid = Mock() template_mock = Mock() template_mock.get = Mock() template_mock.set = Mock() template_mock.get.return_value = mock_api self.plugin._add_api_to_swagger(event_id, properties, template_mock) SwaggerEditorMock.is_valid.assert_called_with(original_swagger) template_mock.get.assert_called_with('restid') SwaggerEditorMock.assert_not_called() template_mock.set.assert_not_called()
def test_must_skip_invalid_swagger(self, SwaggerEditorMock): event_id = "id" properties = { "RestApiId": {"Ref": "restid"}, "Path": "/hello", "Method": "GET" } original_swagger = {"this": "is", "valid": "swagger"} mock_api = SamResource({ "Type": "AWS::Serverless::Api", "Properties": { "DefinitionBody": original_swagger, "a": "b" } }) SwaggerEditorMock.is_valid = Mock() SwaggerEditorMock.is_valid.return_value = False template_mock = Mock() template_mock.get = Mock() template_mock.set = Mock() template_mock.get.return_value = mock_api self.plugin._add_api_to_swagger(event_id, properties, template_mock) SwaggerEditorMock.is_valid.assert_called_with(original_swagger) template_mock.get.assert_called_with('restid') SwaggerEditorMock.assert_not_called() template_mock.set.assert_not_called()
def test_must_skip_if_definition_body_is_not_present(self, SwaggerEditorMock): event_id = "id" properties = { "RestApiId": {"Ref": "restid"}, "Path": "/hello", "Method": "GET" } mock_api = SamResource({ "Type": "AWS::Serverless::Api", "Properties": { "DefinitionUri": "s3://bucket/key", } }) SwaggerEditorMock.is_valid = Mock() SwaggerEditorMock.is_valid.return_value = False template_mock = Mock() template_mock.get = Mock() template_mock.set = Mock() template_mock.get.return_value = mock_api self.plugin._add_api_to_swagger(event_id, properties, template_mock) SwaggerEditorMock.is_valid.assert_called_with(None) template_mock.get.assert_called_with('restid') SwaggerEditorMock.assert_not_called() template_mock.set.assert_not_called()
def test_must_process_functions(self, SamTemplateMock): template_dict = {"a": "b"} function1 = SamResource({"Type": "AWS::Serverless::Function"}) function2 = SamResource({"Type": "AWS::Serverless::Function"}) function3 = SamResource({"Type": "AWS::Serverless::Function"}) function_resources = [("id1", function1), ("id2", function2), ("id3", function3)] api_events = ["event1", "event2"] sam_template = Mock() SamTemplateMock.return_value = sam_template sam_template.set = Mock() sam_template.iterate = Mock() sam_template.iterate.return_value = function_resources self.plugin._get_api_events.return_value = api_events self.plugin.on_before_transform_template(template_dict) SamTemplateMock.assert_called_with(template_dict) sam_template.set.assert_called_with(IMPLICIT_API_LOGICAL_ID, ImplicitApiResource().to_dict()) # Make sure this is called only for Functions sam_template.iterate.assert_any_call("AWS::Serverless::Function") sam_template.iterate.assert_any_call("AWS::Serverless::Api") self.plugin._get_api_events.assert_has_calls([call(function1), call(function2), call(function3)]) self.plugin._process_api_events.assert_has_calls([ call(function1, ["event1", "event2"], sam_template, None), call(function2, ["event1", "event2"], sam_template, None), call(function3, ["event1", "event2"], sam_template, None), ]) self.plugin._maybe_remove_implicit_api.assert_called_with(sam_template)
def test_ayadir_jugador(self): r = Mock() r.get= Mock(return_value=1) r.set = Mock(return_value=True) r.zadd = Mock(return_value=True) casino = Casino(r) self.assertEqual(casino.ayadir_jugador("pepe", 29), True)
def test_must_skip_functions_without_events(self, SamTemplateMock): template_dict = {"a": "b"} function1 = SamResource({"Type": "AWS::Serverless::Function"}) function2 = SamResource({"Type": "AWS::Serverless::Function"}) function3 = SamResource({"Type": "AWS::Serverless::Function"}) function_resources = [("id1", function1), ("id2", function2), ("id3", function3)] # NO EVENTS for any function api_events = [] sam_template = Mock() SamTemplateMock.return_value = sam_template sam_template.set = Mock() sam_template.iterate = Mock() sam_template.iterate.return_value = function_resources self.plugin._get_api_events.return_value = api_events self.plugin.on_before_transform_template(template_dict) self.plugin._get_api_events.assert_has_calls( [call(function1), call(function2), call(function3)]) self.plugin._process_api_events.assert_not_called() self.plugin._maybe_remove_implicit_api.assert_called_with(sam_template)
def mock_sublime_settings(dictionary): def setter(key, value): dictionary[key] = value settings = Mock() settings.get = lambda key, default_value = None: default_value if not key in dictionary else dictionary[key] settings.set = setter return settings
def set_up(cls, clump_size, frame_iterable): node = Mock(["parm"]) parm = Mock(["eval", "set"]) parm.eval = Mock(return_value=clump_size) parm.set = Mock() node.parm = Mock(return_value=parm) frame_spec.frame_set = Mock(return_value=set(frame_iterable)) frame_spec._update_frames_stats = Mock() return (node, parm)
def mc_factory(self, was_404): """ churn out mocked caches with a preset .get(). Also a rapper? """ mc = Mock() ret = was_404 mc.get = Mock(return_value=ret) mc.set = Mock() return mc
def test_fails_number_less_than_0(self): r = Mock() r.get = Mock(return_value=1) r.set = Mock(return_value=True) r.zadd = Mock(return_value=True) casino = Casino(r) self.assertRaises(casino.ayadir_jugador("pepe", -1)) self.assertRaises(casino.escoger_juego(-1)) self.assertRaises(casino.ingresar_creditos("pepe", -1)) self.assertRaises(casino.sacar_creditos("pepe", -1))
def test_fails_tupla(self): r = Mock() r.get = Mock(return_value=1) r.set = Mock(return_value=True) r.zadd = Mock(return_value=True) casino = Casino(r) self.assertRaises(Exception, casino.ayadir_jugador((1, 2, 3), 2)) self.assertRaises(Exception, casino.ingresar_creditos((1, 2, 3), 2)) self.assertRaises(Exception, casino.abandonar_juego((1, 2, 3), 2)) self.assertRaises(Exception, casino.obtener_creditos_varios_jugadores((1, 2, 3), 2)) self.assertRaises(Exception, casino.obtener_creditos((1, 2)))
def test_must_restore_if_existing_resource_present(self): resource = SamResource({"Type": "AWS::Serverless::Api", "Properties": {"DefinitionBody": {"paths": {}}}}) template = Mock() template.get = Mock() template.set = Mock() template.get.return_value = resource self.plugin.existing_implicit_api_resource = resource self.plugin._maybe_remove_implicit_api(template) template.get.assert_called_with(IMPLICIT_API_LOGICAL_ID) # Must restore original resource template.set.assert_called_with(IMPLICIT_API_LOGICAL_ID, resource)
class MockBackendMixin(object): """ Mocks a cache backend which is to be mixed with the Cache class. """ def __init__(self, *args, **kwargs): self._cache = Mock() def add(self, key, value, timeout=None, version=None): self._cache.add(key, value, timeout, version=version) def set(self, key, value, timeout=None, version=None): self._cache.set(key, value, timeout, version=version) def get(self, key, default=None, version=None): val = self._cache.get(key, version=version) if val is not None: return val return default def delete(self, key, version=None): self._cache.delete(key, version=version)
def test_fails_bool(self): r = Mock() r.get = Mock(return_value=1) r.set = Mock(return_value=True) r.zadd = Mock(return_value=True) casino = Casino(r) self.assertRaises(Exception, casino.ayadir_jugador("pepe", True)) self.assertRaises(Exception, casino.sacar_creditos("pepe", True)) self.assertRaises(Exception, casino.ingresar_creditos("pepe", True)) self.assertRaises(Exception, casino.abandonar_juego("pepe", True)) self.assertRaises(Exception, casino.obtener_creditos_varios_jugadores("pepe", True)) self.assertRaises(Exception, casino.ayadir_jugador(True, "pepe")) self.assertRaises(Exception, casino.escoger_juego(True)) self.assertRaises(Exception, casino.obtener_creditos(True))
def __do_not_call(self): # s = Settings s = Mock() # methods: s.isOptionWritable = Mock(return_value=True) s.set = Mock(return_value=None) s.save = Mock() # props: s.log = Mock() # subobjects s.p = Mock() s.p.getboolean = Mock(return_value=True) s.p.has_option = Mock
def __do_not_call(self): # s = Settings s = Mock() # methods: s.is_option_writable = Mock(return_value=True) s.set = Mock(return_value=None) s.save = Mock() # props: s.log = Mock() # subobjects s.p = Mock() s.p.getboolean = Mock(return_value=True) s.p.has_option = Mock
def do_init_and_set(self, memcachering_mock, telnet_mock, read_until_ret_list): tn_mock = Mock() telnet_mock.return_value = tn_mock read_until_mock = Mock() def read_until_ret_func(*args, **kwargs): return read_until_ret_list.pop(0) read_until_mock.side_effect = read_until_ret_func tn_mock.read_until = read_until_mock ring_mock = Mock() memcachering_mock.return_value = ring_mock ring_mock.set = Mock() endpoint = 'test.lwgyhw.cfg.usw2.cache.amazonaws.com:11211' mc = MemcacheClient(endpoint) mc.set('foo', 'bar') mc.stop_timer() ring_mock.set.assert_called_with('foo', 'bar')
def test_must_skip_without_functions(self, SamTemplateMock): template_dict = {"a": "b"} # NO FUNCTIONS function_resources = [] sam_template = Mock() SamTemplateMock.return_value = sam_template sam_template.set = Mock() sam_template.iterate = Mock() sam_template.iterate.return_value = function_resources self.plugin.on_before_transform_template(template_dict) self.plugin._get_api_events.assert_not_called() self.plugin._process_api_events.assert_not_called() # This is called always, even if there are no functions self.plugin._maybe_remove_implicit_api.assert_called_with(sam_template)
def test_must_restore_if_existing_resource_present(self): resource = SamResource({ "Type": "AWS::Serverless::Api", "Properties": { "DefinitionBody": { "paths": {} } } }) template = Mock() template.get = Mock() template.set = Mock() template.get.return_value = resource self.plugin.existing_implicit_api_resource = resource self.plugin._maybe_remove_implicit_api(template) template.get.assert_called_with(IMPLICIT_API_LOGICAL_ID) # Must restore original resource template.set.assert_called_with(IMPLICIT_API_LOGICAL_ID, resource)
def test_must_skip_functions_without_events(self, SamTemplateMock): template_dict = {"a": "b"} function_resources = [("id1", "function1"), ("id2", "function2"), ("id3", "function3")] # NO EVENTS for any function api_events = [] sam_template = Mock() SamTemplateMock.return_value = sam_template sam_template.set = Mock() sam_template.iterate = Mock() sam_template.iterate.return_value = function_resources self.plugin._get_api_events.return_value = api_events self.plugin.on_before_transform_template(template_dict) self.plugin._get_api_events.assert_has_calls([call("function1"), call("function2"), call("function3")]) self.plugin._process_api_events.assert_not_called() self.plugin._maybe_remove_implicit_api.assert_called_with(sam_template)
def redis_mock(): cache = {} def set(name=None, value=None, **_): cache[name] = value def get(name): return cache.get(name) def delete(key): cache.pop(key) _MockRedis = Mock(spec='redislite.StrictRedis') _MockRedis.cache = cache _MockRedis.get = Mock(side_effect=get) _MockRedis.set = Mock(side_effect=set) _MockRedis.delete = Mock(side_effect=delete) _MockRedis.flushall = Mock() return _MockRedis
def test_must_skip_state_machines_without_events(self, SamTemplateMock): template_dict = {"a": "b"} statemachine1 = SamResource({"Type": "AWS::Serverless::StateMachine"}) statemachine2 = SamResource({"Type": "AWS::Serverless::StateMachine"}) statemachine3 = SamResource({"Type": "AWS::Serverless::StateMachine"}) statemachine_resources = [("id1", statemachine1), ("id2", statemachine2), ("id3", statemachine3)] # NO EVENTS for any state machine api_events = [] sam_template = Mock() SamTemplateMock.return_value = sam_template sam_template.set = Mock() sam_template.iterate = Mock() sam_template.iterate.return_value = statemachine_resources self.plugin._get_api_events.return_value = api_events self.plugin.on_before_transform_template(template_dict) self.plugin._get_api_events.assert_has_calls([call(statemachine1), call(statemachine2), call(statemachine3)]) self.plugin._process_api_events.assert_not_called() self.plugin._maybe_remove_implicit_api.assert_called_with(sam_template)
def test_must_work_with_rest_api_id_as_string(self, SwaggerEditorMock): event_id = "id" properties = { # THIS IS A STRING, not a {"Ref"} "RestApiId": "restid", "Path": "/hello", "Method": "GET" } original_swagger = {"this": "is", "valid": "swagger"} updated_swagger = "updated swagger" mock_api = SamResource({ "Type": "AWS::Serverless::Api", "Properties": { "__MANAGE_SWAGGER": True, "DefinitionBody": original_swagger, "a": "b" } }) SwaggerEditorMock.is_valid = Mock() SwaggerEditorMock.is_valid.return_value = True editor_mock = Mock() SwaggerEditorMock.return_value = editor_mock editor_mock.swagger = updated_swagger template_mock = Mock() template_mock.get = Mock() template_mock.set = Mock() template_mock.get.return_value = mock_api self.plugin._add_api_to_swagger(event_id, properties, template_mock) SwaggerEditorMock.is_valid.assert_called_with(original_swagger) template_mock.get.assert_called_with('restid') editor_mock.add_path("/hello", "GET") template_mock.set.assert_called_with("restid", mock_api) self.assertEqual(mock_api.properties["DefinitionBody"], updated_swagger)
def test_sacar_creditos(self): r = Mock() r.get = Mock(return_value=1) r.set = Mock(return_value=True) casino = Casino(r) self.assertEquals(casino.sacar_creditos("pepe", 1), True)