Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The test_func.py unit test template for Python Knative functions in invalid #2448

Open
the-gigi opened this issue Jul 28, 2024 · 1 comment

Comments

@the-gigi
Copy link

The generated test code invoke sthe function's main() entrypoint with an empty dictionary:

import unittest

func = __import__("func")

class TestFunc(unittest.TestCase):

  def test_func_empty_request(self):
    resp, code = func.main({})
    self.assertEqual(resp, "{}")
    self.assertEqual(code, 200)

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

See code here

But, the main() function expects a a parliament Context that contains a Flask request

from parliament import Context
from flask import Request

...

def main(context: Context):
    """
    Function template
    The context parameter contains the Flask request object and any
    CloudEvent received with the request.
    """
   ...

It should probably generate a context with some mock request. This is what I did for my function, which expects to be invoked with a parameter named descriptions:

import unittest
from parliament import Context


func = __import__("func")


class DummyRequest:
    def __init__(self, descriptions):
        self.descriptions = descriptions

    @property
    def args(self):
        return dict(descriptions=self.descriptions)


class TestFunc(unittest.TestCase):
    def test_func(self):

        result, code = func.main(Context(DummyRequest('flame,confused')))
        expected = """{"flame": "('fire', '🔥')", "confused": "('confused_face', '😕')"}"""
        self.assertEqual(expected, result)
        self.assertEqual(code, 200)


if __name__ == "__main__":
    unittest.main()
Copy link
Contributor

This issue is stale because it has been open for 90 days with no
activity. It will automatically close after 30 more days of
inactivity. Reopen the issue with /reopen. Mark the issue as
fresh by adding the comment /remove-lifecycle stale.

@github-actions github-actions bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 27, 2024
@lkingland lkingland removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants