def test_log_routes(logger_mock): action_object = MockAction() app = Pantam() app.actions = [ { "file_name": "index.py", "module_name": "index", "action_class": MockAction, "action_obj": action_object, "routes": [ { "method": "fetchAll", "verb": "get", "url": "/", }, { "method": "fetchSingle", "verb": "get", "url": "/:id", }, ], }, ] app.log_routes() logger_mock.assert_called_with("""Available Routes: GET -> / -> index.py -> fetchAll GET -> /:id -> index.py -> fetchSingle GET -> /healthz [health check endpoint]""")
def test_prepare_error(logger_mock, app_mock): def throw_error(): raise Exception("Fake Error") app_mock.side_effect = throw_error app = Pantam() app.build() logger_mock.assert_called_with("Unable to build Pantam application!")
def test_handle_read_actions_error(logger_mock, os_mock): def throw_error(): raise Exception("Fake Error") os_mock.side_effect = throw_error app = Pantam() app.read_actions_folder() logger_mock.assert_called_with( "Unable to read actions folder! Check `actions_folder` config setting." )
def test_override_some_options(): app = Pantam(actions_folder="axs") config = { "actions_folder": "axs", "actions_index": "index", "on_shutdown": None, "debug": False, "dev_port": 5000, "port": 5000, "reload": None, } assert app.get_config() == config
def test_get_default_options(): app = Pantam() default_config = { "actions_folder": "actions", "actions_index": "index", "on_shutdown": None, "debug": False, "dev_port": 5000, "port": 5000, "reload": None, } assert app.get_config() == default_config
def test_bind_routes_error(logger_mock): app = Pantam() app.get_actions = Mock(return_value=[ { "file_name": "index.py", "module_name": "index", "class_name": "index", "action_class": MockEmptyAction, "action_obj": MockEmptyAction(), }, ]) app.bind_routes() logger_mock.assert_called_with("No methods found for `index` action.")
def test_bind_routes(): app = Pantam() app.get_actions = Mock(return_value=[ { "file_name": "index.py", "module_name": "index", "class_name": "index", "action_class": MockSmallAction, "action_obj": MockSmallAction(), }, ]) app.bind_routes() assert len(app.routes) == 3 assert app.routes[0].path == "/" assert app.routes[1].path == "/{id}" assert app.routes[2].path == "/healthz"
def test_load_actions(): app = Pantam() app.read_actions_folder = Mock(return_value=["index.py"]) # type: ignore app.import_action_module = Mock(return_value=MockAction) # type: ignore app.discover_actions() app.load_actions() actions = app.get_actions() assert actions[0]["action_class"] == MockAction assert isinstance(actions[0]["action_obj"], MockAction) assert actions[0]["file_name"] == "index.py" assert actions[0]["module_name"] == "index" assert actions[0]["class_name"] == "Index" assert actions[0]["routes"] == []
def test_make_routes(): app = Pantam() routes = app.make_routes("index", MockAction) assert routes == [ { "method": "fetch_all", "url": "/", "verb": "get", }, { "method": "fetch_single", "url": "/{id}", "verb": "get", }, { "method": "get_custom", "url": "/custom/", "verb": "get", }, { "method": "create", "url": "/", "verb": "post", }, { "method": "do_custom", "url": "/custom/{id}", "verb": "post", }, { "method": "set_custom", "url": "/custom/", "verb": "post", }, { "method": "update", "url": "/{id}", "verb": "patch", }, { "method": "delete", "url": "/{id}", "verb": "delete", }, ]
def test_discover_actions(): app = Pantam() app.read_actions_folder = Mock(return_value=["index.py", "auth_test.py" ]) # type: ignore actions = app.discover_actions() expected = [ { "file_name": "index.py", "module_name": "index", "class_name": "Index", "routes": [], }, { "file_name": "auth_test.py", "module_name": "auth-test", "class_name": "AuthTest", "routes": [], }, ] assert actions == expected
def test_override_all_options(): callback = lambda: True app = Pantam( actions_folder="axs", actions_index="main", on_shutdown=callback, debug=True, dev_port=5001, port=80, reload=False, ) config = { "actions_folder": "axs", "actions_index": "main", "on_shutdown": callback, "debug": True, "dev_port": 5001, "port": 80, "reload": False, } assert app.get_config() == config
def test_make_default_urls(): app = Pantam() assert app.make_url("index", "fetch_all") == "/" assert app.make_url("index", "fetch_single") == "/{id}" assert app.make_url("index", "create") == "/" assert app.make_url("index", "update") == "/{id}" assert app.make_url("index", "delete") == "/{id}"
def test_make_custom_resource_urls(): app = Pantam() assert app.make_url("custom-action", "fetch_all") == "/custom-action/" assert app.make_url("custom-action", "fetch_single") == "/custom-action/{id}" assert app.make_url("custom-action", "create") == "/custom-action/" assert app.make_url("custom-action", "update") == "/custom-action/{id}" assert app.make_url("custom-action", "delete") == "/custom-action/{id}"
def test_get_action_routes(): app = Pantam() app.actions = "test" assert app.get_actions() == "test"
def test_read_actions_folder(mock): mock.return_value = ["foo.py", "bar.py", "rubbish.txt", "__init__.py"] app = Pantam() files = app.read_actions_folder() assert files == ["foo.py", "bar.py"]
def test_import_action_module(logger_mock): app = Pantam() app.import_action_module("index", "Index") logger_mock.assert_called_with("Unable to load `actions.index` module.")
def test_set_options_programmatically(): app = Pantam() config = app.get_config() config["port"] = 82 app.set_config(config) assert app.get_config() == config
def test_import_custom_action_module(logger_mock): app = Pantam(actions_folder="test/actions") app.import_action_module("index", "Index") logger_mock.assert_called_with( "Unable to load `test.actions.index` module.")
def test_get_routes(logger_mock): app = Pantam() app.get_routes() logger_mock.assert_called_with("No routes have been defined.")
def test_action_routes_error(logger_mock): app = Pantam() app.get_actions() logger_mock.assert_called_with( "You have no loaded actions. Check for files in the actions folder.")
def test_make_custom_method_urls(): app = Pantam() assert app.make_url("index", "get_my_custom_method") == "/my-custom-method/" assert app.make_url("foo", "set_your_magic_method") == "/foo/your-magic-method/"
from pantam import Pantam pantam = Pantam(debug=True, actions_folder="domain/actions") app = pantam.build()
from pantam import Pantam pantam = Pantam(debug=True) app = pantam.build()