Skip to content

Commit

Permalink
Refactor the code, create documentation to code, and update tests (#43)
Browse files Browse the repository at this point in the history
* chore(.gitignore): add ignore rules for database configuration files in migrations and instance directories
test(routes_test.py): refactor test cases to use unittest.TestCase and improve readability
test(routes_test.py): add test case to confirm if blueprints were registered
test(routes_test.py): add test case to verify if there are no duplicate blueprints registered
test(routes_test.py): add test case to check if all registered routes are present
test(routes_test.py): add test case to check if all registered endpoints are present
test(routes_test.py): add test case to count the number of HTTP verbs registered for routes

* fix(routes_test.py): remove unused property 'endpoints' to improve code readability and maintainability
fix(routes_test.py): remove unused property 'blueprints' to improve code readability and maintainability
refactor(routes_test.py): simplify test_when_blueprints_have_been_registered by directly comparing expected blueprints with actual blueprints
refactor(routes_test.py): simplify test_when_not_exists_registered_blueprints by directly comparing expected blueprints with actual blueprints
refactor(routes_test.py): simplify test_when_endpoints_have_been_registered by directly comparing expected endpoints with actual endpoints
refactor(routes_test.py): simplify test_when_methods_have_been_registered by directly comparing expected methods with actual methods

* chore(version): update package version from 2.5.0 to 2.7.0
test(version_test.py): add unit test to verify the updated package version is correct

* feat(tests): add conftest.py file for test fixtures and client setup
refactor(tests): remove fixtures.py file as it is no longer needed
feat(tests): add messages_enpoint_test.py file for testing message endpoints
refactor(tests): remove request_test.py file as it is no longer needed

* chore(makefile): update test command in makefile to use pytest instead of ward for consistency with other projects
chore(makefile): update format command in makefile to use line length limit of 89 characters instead of 79 characters for consistency with other projects
chore(makefile): update check command in makefile to use line length limit of 89 characters instead of 79 characters for consistency with other projects
chore(pyproject.toml): add flask-sqlalchemy and pytest as dev dependencies for the project

* test(routes_test.py): refactor test methods to improve readability and maintainability
test(routes_test.py): add assertions to verify that the expected blueprints have been registered
test(routes_test.py): add assertions to verify that the expected routes have been registered
test(routes_test.py): add assertions to verify that the expected endpoints have been registered

* feat(router.py): add Router class for managing routes in a web application

The Router class provides methods to define and manage different HTTP routes (GET, POST, PUT, DELETE) for the application's controllers and actions. It includes the following methods:

- `_method_route()`: Private method that organizes routes by HTTP method.
- `namespace(name: str)`: Static method to create a namespace for routes.
- `get(path: str, resource: str)`: Static method to define a GET route.
- `post(path: str, resource: str)`: Static method to define a POST route.
- `put(path: str, resource: str)`: Static method to define a PUT route.
- `delete(path: str, resource: str)`: Static method to define a DELETE route.
- `all(resource: str, only=None, base_path="")`: Static method to define routes for all standard RESTful actions for a resource.
- `_add_routes(name, actions, base_path)`: Private method to add routes for specified actions under a given name and base path.

fix(routes_test.py): remove empty line

* test(messages_endpoint_test.py): add tests for checking if blueprints have been registered and if messages routes and endpoints have been registered
test(messages_endpoint_test.py): add tests for checking the number of registered routes and their methods
test(messages_enpoint_test.py): delete unused test file

* chore(routes.py): remove unused route definition for "user" endpoint
chore(conftest.py): uncomment and refactor browser fixture to use test_client() and properly handle database session
feat(messages_form_test.py): add tests for updating and deleting messages using PUT HTTP method
chore(routes_test.py): remove assertion for "user" blueprint in test_when_blueprints_have_been_registered() test

* chore(tests): remove outdated version test file
test(version_test.py): update version test to use the correct version number

* test(messages_endpoint_test.py): remove unnecessary blank line to improve code readability
test(version_test.py): remove unnecessary blank line to improve code readability

* fix(__init__.py): fix import statement for http_method_override middleware module
feat(http_method_override.py): add HTTPMethodOverrideMiddleware and CustomRequest classes to handle HTTP method override functionality

* fix(messages_controller.py): refactor update method to handle both JSON and form data for updating message title
test(messages_direct_request_test.py): add tests for creating and updating a message using direct requests

* chore(posts_controller.py): remove unused methods and add new method to improve code organization and readability
chore(user_controller.py): remove unused file to improve code organization and maintainability
chore(routes.py): comment out unused routes to improve code organization and maintainability
test(messages_endpoint_test copy.py): add tests to ensure proper registration of messages endpoints
test(routes_test.py): add tests to ensure proper registration of routes and methods count

* chore: bump version to 2.7.1

Bump the version from 2.7.0 to 2.7.1 in __version__.py, pyproject.toml, and version_test.py to reflect the latest changes and improvements in the mvc-flask project.
  • Loading branch information
marcuxyz authored Nov 12, 2023
1 parent 3088c94 commit d1262ef
Show file tree
Hide file tree
Showing 21 changed files with 347 additions and 353 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
__pycache__
.vscode
.env
dist
dist

# Ignore configuration of database
migrations
instance
8 changes: 4 additions & 4 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
.PHONY: test
test:
FLAKS_ENV=testing ward
FLAKS_ENV=testing pytest -vvv



.PHONY: format
format:
@black . -l 79
@black . -l 89


.PHONY: check
check:
@black . -l 79 --check
@black . -l 89 --check

6 changes: 2 additions & 4 deletions mvc_flask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from flask.blueprints import Blueprint

from .router import Router
from .middleware.http_method_override import (
from .middlewares.http_method_override import (
HTTPMethodOverrideMiddleware,
CustomRequest,
)
Expand Down Expand Up @@ -33,9 +33,7 @@ def register_blueprint(self, app: Flask):
controller = route[0]
blueprint = Blueprint(controller, controller)

obj = import_module(
f"{self.path}.controllers.{controller}_controller"
)
obj = import_module(f"{self.path}.controllers.{controller}_controller")
view_func = getattr(obj, f"{controller.title()}Controller")

self.hook.register(view_func, blueprint)
Expand Down
2 changes: 1 addition & 1 deletion mvc_flask/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.5.0"
__version__ = "2.7.1"
111 changes: 102 additions & 9 deletions mvc_flask/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,49 @@


class Router:
"""
Router class for managing routes in a web application.
This class provides methods to define and manage different HTTP routes
(GET, POST, PUT, DELETE) for the application's controllers and actions.
Attributes:
ROUTES (list): A class-level list that stores all the routes registered
with their respective HTTP methods, paths, controllers,
and actions.
Methods:
_method_route(): Private method that organizes routes by HTTP method.
namespace(name: str): Static method to create a namespace for routes.
get(path: str, resource: str): Static method to define a GET route.
post(path: str, resource: str): Static method to define a POST route.
put(path: str, resource: str): Static method to define a PUT route.
delete(path: str, resource: str): Static method to define a DELETE route.
all(resource: str, only=None, base_path=""): Static method to define routes for all
standard RESTful actions for a resource.
_add_routes(name, actions, base_path): Private method to add routes for specified
actions under a given name and base path.
"""

ROUTES = []

@staticmethod
def _method_route():
"""
Organizes routes by HTTP method.
Returns:
dict: A dictionary where each key is an HTTP method and the value is a list
of routes associated with that method.
"""

routes = {}

for route in Router.ROUTES:
Expand All @@ -23,38 +62,83 @@ def _method_route():

@staticmethod
def namespace(name: str):
"""
Creates a namespace for routes.
Args:
name (str): The name of the namespace.
Returns:
Namespace: An instance of Namespace associated with the given name.
"""

return Namespace(name, Router)

@staticmethod
def get(path: str, resource: str):
"""
Defines a GET route.
Args:
path (str): URL path for the route.
resource (str): The 'controller#action' string specifying the controller and action.
"""

controller, action = resource.split("#")
Router.ROUTES.append(
{controller: Model(["GET"], path, controller, action)}
)
Router.ROUTES.append({controller: Model(["GET"], path, controller, action)})

@staticmethod
def post(path: str, resource: str):
"""
Defines a POST route.
Args:
path (str): URL path for the route.
resource (str): The 'controller#action' string specifying the controller and action.
"""

controller, action = resource.split("#")
Router.ROUTES.append(
{controller: Model(["POST"], path, controller, action)}
)
Router.ROUTES.append({controller: Model(["POST"], path, controller, action)})

@staticmethod
def put(path: str, resource: str):
"""
Defines a PUT route.
Args:
path (str): URL path for the route.
resource (str): The 'controller#action' string specifying the controller and action.
"""

controller, action = resource.split("#")
Router.ROUTES.append(
{controller: Model(["PUT", "PATCH"], path, controller, action)},
)

@staticmethod
def delete(path: str, resource: str):
"""
Defines a DELETE route.
Args:
path (str): URL path for the route.
resource (str): The 'controller#action' string specifying the controller and action.
"""

controller, action = resource.split("#")
Router.ROUTES.append(
{controller: Model(["DELETE"], path, controller, action)}
)
Router.ROUTES.append({controller: Model(["DELETE"], path, controller, action)})

@staticmethod
def all(resource: str, only=None, base_path=""):
"""
Defines routes for all standard RESTful actions for a resource.
Args:
resource (str): The name of the resource.
only (str or None): A space-separated string of actions to limit the routes to.
base_path (str): The base path to prepend to the resource path.
"""

group = [
"index",
"show",
Expand All @@ -69,6 +153,15 @@ def all(resource: str, only=None, base_path=""):

@staticmethod
def _add_routes(name, actions, base_path):
"""
Adds routes for specified actions under a given name and base path.
Args:
name (str): The name of the resource.
actions (list): A list of actions to create routes for.
base_path (str): The base path to prepend to the resource path.
"""

groups = {
"index": "get",
"new": "get",
Expand Down
Loading

0 comments on commit d1262ef

Please sign in to comment.