Robyn icon indicating copy to clipboard operation
Robyn copied to clipboard

Added add_template_global function to JinjaTemplate.

Open carrycooldude opened this issue 11 months ago • 12 comments

Description

This PR fixes #

  • Added add_template_global function to JinjaTemplate.
  • Added the docs

carrycooldude avatar Feb 25 '24 14:02 carrycooldude

@carrycooldude is attempting to deploy a commit to the sparckles Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Feb 25 '24 14:02 vercel[bot]

Hey @carrycooldude 👋

Good job with the PR 😄 I have some inline suggestions.

sansyrox avatar Feb 25 '24 22:02 sansyrox

CodSpeed Performance Report

Merging #758 will not alter performance

Comparing carrycooldude:main (f72d0ea) with main (bccbe4c)

Summary

✅ 106 untouched benchmarks

🆕 2 new benchmarks

Benchmarks breakdown

Benchmark main carrycooldude:main Change
🆕 test_url_for[async] N/A 24 ms N/A
🆕 test_url_for[sync] N/A 24 ms N/A

codspeed-hq[bot] avatar Feb 27 '24 11:02 codspeed-hq[bot]

Hey @carrycooldude 👋

Good update. Two more things

We already have a documentation page for jinja (https://github.com/sparckles/Robyn/blob/main/docs_src/src/pages/documentation/api_reference/templating.mdx) , can you add pertinent documentation there?

Also, can you add an integration test in the integration_tests/base_routes.py folder?

sansyrox avatar Feb 27 '24 22:02 sansyrox

Hey @sansyrox , I never did integration testing before but let me know what I am suggesting below is it the same?

import pytest
from robyn.templating import JinjaTemplate

@pytest.mark.integration
def test_add_template_global():
    template = JinjaTemplate()
    template.add_template_global("my_global", "Hello, world!")
    rendered_template = template.render_template("my_template.html")
    assert "Hello, world!" in rendered_templateimport pytest
from robyn.templating import JinjaTemplate

@pytest.mark.integration
def test_jinja_template():
    template = JinjaTemplate()
    template.add_template_global("my_global", "Hello, world!")
    rendered_template = template.render_template("my_template.html")
    assert "Hello, world!" in rendered_template

    # Test loading static resources
    static_url = template.render_template("static_url.html")
    assert static_url == "/static/images/planets.jpeg"

    # Test route URL
    route_url = template.render_template("route_url.html")
    assert route_url == "/"

carrycooldude avatar Feb 28 '24 16:02 carrycooldude

Screen Shot 2024-02-28 at 22 03 33

@carrycooldude , first add a route like this in the base_routes/integration_tests.py file. Screen Shot 2024-02-28 at 22 23 32

Then alter this test in integration_tests/test_get_requests.py .

What you have written atm is a unit test. But I like to write integration tests for most changes. I can explain the rationale on a call. But I hope this makes sense for now. 😄

sansyrox avatar Feb 28 '24 22:02 sansyrox

Hey @carrycooldude 👋

Thank you for the PR. I have some suggestions for the code.

sansyrox avatar Feb 28 '24 22:02 sansyrox

Screen Shot 2024-02-28 at 22 03 33

@carrycooldude , first add a route like this in the base_routes/integration_tests.py file. Screen Shot 2024-02-28 at 22 23 32

Then alter this test in integration_tests/test_get_requests.py .

What you have written atm is a unit test. But I like to write integration tests for most changes. I can explain the rationale on a call. But I hope this makes sense for now. 😄

Hey @sansyrox , for integration testing , we need add both sync and async routes right with different endpoint ? This is kinda example for the same

import unittest
from flask import Flask, url_for

class TestURLGeneration(unittest.TestCase):
    def setUp(self):
        self.app = Flask(__name__)

        # Define some routes for testing
        @self.app.route('/')
        def home():
            return 'Home Page'

        @self.app.route('/user/<username>')
        def profile(username):
            return f'Profile Page for {username}'

        # Define a static file route
        @self.app.route('/static/<path:filename>')
        def static_file(filename):
            return f'Static File: {filename}'

    def test_home_url(self):
        with self.app.test_request_context():
            # Generate URL for the home route
            url = url_for('home')
            self.assertEqual(url, '/')

    def test_profile_url(self):
        with self.app.test_request_context():
            # Generate URL for the profile route with a username parameter
            url = url_for('profile', username='john_doe')
            self.assertEqual(url, '/user/john_doe')

    def test_static_file_url(self):
        with self.app.test_request_context():
            # Generate URL for a static file
            url = url_for('static', filename='style.css')
            self.assertEqual(url, '/static/style.css')

if __name__ == '__main__':
    unittest.main()

carrycooldude avatar Feb 29 '24 19:02 carrycooldude

@carrycooldude ,

what you have mentioned is again a unitTest . You need to add it to the existing endpoints in the codebase.

sansyrox avatar Mar 01 '24 00:03 sansyrox

@carrycooldude , I have left a few comments.

sansyrox avatar Mar 08 '24 13:03 sansyrox

Screenshot from 2024-03-10 00-24-38 I don't know why importing error is coming... Same error with tests_get_requests.py

carrycooldude avatar Mar 09 '24 19:03 carrycooldude

@sansyrox need your help in this...

carrycooldude avatar Mar 11 '24 05:03 carrycooldude