Skip to content

lochinbekdev/JdpuPF

Repository files navigation

JdpuPF Python Freamwork ✨

License: MIT PyPI - Version

Installation

pip install JdpuPF

How to use it

Basic usage:

from app import JdpuPF
from middleware import Middleware

app = JdpuPF()

@app.route('/home',allowed_methods=["get"])
def home(request,response):
    if request.method == "GET":
        response.text = "Hello from the Home Page"
    else:
        response.status.code = 405
        response.text = "Method not allowed"


@app.route("/hello/{name}")
def greeting(request, response ,name):
    response.text = f"Hello , {name} :)"
    
@app.route("/books")
class Books:
    def get(self,request, response):
        response.text = "Books page"

    def post(self,request, response):
        response.text = "Endpoint to create a book"

Unit Tests

The recommended way of writing unit tests is with pytest. There are two built in fixtures that you may want to use when writing unit tests with JdpuPF. The first one is app which is an instance of the main API class:

def test_dublicate_routes_throws_exception(app):
    @app.route("/home")
    def home(req,resp):
        resp.text = "Hello from home Page" 
        assert resp.text == "Hello from home Page"    
    with pytest.raises(AssertionError):
        @app.route("/home")
        def home2(req,resp):
            resp.text = "Hello from home2 Page" 

The other one is client that you can use to send HTTP requests to your handlers. It is based on the famous requests and it should feel very familiar:

def test_parameterized_routing(app,test_client):
    @app.route("/hello/{name}")
    def greeting(request, response ,name):
        response.text = f"Hello {name} :)"
        
    assert test_client.get("http://testserver/hello/Lochinbek").text == "Hello Lochinbek :)"
    assert test_client.get('http://testserver/hello/Matthew').text == "Hello Matthew :)"

Thank you for Usage