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

Added some unit tests #921

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
459 changes: 84 additions & 375 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion hug/output_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def register_json_converter(function):
def numpy_listable(item):
return item.tolist()

@json_convert(str, numpy.unicode_)
@json_convert(str, numpy.str_)
def numpy_stringable(item):
return str(item)

Expand Down
Binary file added readme_img/AFTER_UNITTESTS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/BEFORE_UNITTESTS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/after/apiinit_after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/after/localroutercall_after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/after/localrouterdirectives_after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/after/localrouterinit_after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/after/localroutervalidate_after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/after/localrouterversion_after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/after/modulesingletoncall_after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/after/routerinit_after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/before/apiinit_before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/before/localroutercall_before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/before/localrouterinit_before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/before/localroutervalidate_before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/before/localrouterversion_before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/before/modulesingletoncall_before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme_img/before/routerinit_before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 81 additions & 0 deletions tests/test_api2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""This is the test module for the Localrouter class,
made by Marwan Amrhar & Yahia Noeman & Ayoub Bouazza & Sajeed Bouzziane.
It covers the functions:
API.__init__
ModuleSingleton.__call__"""

import unittest
from hug.api import API

class MockModule:
__name__ = "mock_module"
__doc__ = "This is a mock module"

class TestAPIInit(unittest.TestCase):
def test_api_init_with_module(self):
"""Test API initialization with module"""
module = MockModule()
api = API(module=module)

self.assertEqual(api.module, module)
self.assertEqual(api.name, "mock_module")
self.assertEqual(api.doc, "This is a mock module")
self.assertFalse(api.started)
self.assertFalse(api.cli_error_exit_codes)
self.assertFalse(api.future)

def test_api_init_without_module(self):
"""Test API initialization without module"""
name = "test_name"
doc = "This is a test doc"
cli_error_exit_codes = True
future = True

api = API(name=name, doc=doc, cli_error_exit_codes=cli_error_exit_codes, future=future)

self.assertIsNone(api.module)
self.assertEqual(api.name, name)
self.assertEqual(api.doc, doc)
self.assertFalse(api.started)
self.assertTrue(api.cli_error_exit_codes)
self.assertTrue(api.future)

def test_api_init_with_partial_module_attributes(self):
"""Test API initialization with module and partial attributes"""
module = MockModule()
name = "custom_name"
doc = "Custom documentation"

api = API(module=module, name=name, doc=doc)

self.assertEqual(api.module, module)
self.assertEqual(api.name, name)
self.assertEqual(api.doc, doc)
self.assertFalse(api.started)
self.assertFalse(api.cli_error_exit_codes)
self.assertFalse(api.future)

def test_api_init_with_empty_module(self):
"""Test API initialization with empty module attributes"""
class EmptyModule:
__name__ = None
__doc__ = None

module = EmptyModule()
api = API(module=module)

self.assertEqual(api.module, module)
self.assertEqual(api.name, "")
self.assertEqual(api.doc, "")
self.assertFalse(api.started)
self.assertFalse(api.cli_error_exit_codes)
self.assertFalse(api.future)

def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestAPIInit))
return suite

if __name__ == "__main__":
runner = unittest.TextTestRunner()
runner.run(test_suite())
90 changes: 90 additions & 0 deletions tests/test_local_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""This is the test module for the Localrouter class,
made by Marwan Amrhar & Yahia Noeman & Ayoub Bouazza & Sajeed Bouzziane.
It covers the functions:
Localrouter.init
Localrouter.directives
Localrouter.validate
Localrouter.version
Localrouter.call"""

import unittest
from unittest.mock import patch
import hug

from hug.routing import LocalRouter

class TestLocalRouter(unittest.TestCase):

def setUp(self):
self.router = LocalRouter(api="test_api")

## localrouter.init
def test_initialization_with_defaults(self):
"""Test LocalRouter initialization with default parameters"""
instance = LocalRouter()
self.assertNotIn("version", instance.route)
self.assertNotIn("skip_directives", instance.route)
self.assertNotIn("skip_validation", instance.route)

def test_initialization_with_version(self):
"""Test LocalRouter initialization with version set"""
instance = LocalRouter(version=1)
self.assertEqual(instance.route.get("version"), 1)
self.assertNotIn("skip_directives", instance.route)
self.assertNotIn("skip_validation", instance.route)

def test_initialization_with_skip_directives(self):
"""Test LocalRouter initialization with directives=False"""
instance = LocalRouter(directives=False)
self.assertNotIn("version", instance.route)
self.assertTrue(instance.route.get("skip_directives"))

def test_initialization_with_skip_validation(self):
"""Test LocalRouter initialization with validate=False"""
instance = LocalRouter(validate=False)
self.assertNotIn("version", instance.route)
self.assertNotIn("skip_directives", instance.route)
self.assertTrue(instance.route.get("skip_validation"))

def test_initialization_with_all_parameters(self):
"""Test LocalRouter initialization with all parameters"""
instance = LocalRouter(directives=False, validate=False, version=1)
self.assertEqual(instance.route.get("version"), 1)
self.assertTrue(instance.route.get("skip_directives"))
self.assertTrue(instance.route.get("skip_validation"))

## localrouter.directives
def test_directives_method(self):
"""Test LocalRouter directives method"""
result = self.router.directives(use=False)
self.assertIsInstance(result, LocalRouter)
self.assertIn("skip_directives", result.route)
self.assertTrue(result.route["skip_directives"])

## localrouter.validate
def test_validate_method(self):
"""Test LocalRouter validate method"""
result = self.router.validate(enforce=False)
self.assertIsInstance(result, LocalRouter)
self.assertIn("skip_validation", result.route)
self.assertTrue(result.route["skip_validation"])

## localrouter.version
def test_version_method(self):
"""Test LocalRouter version method"""
result = self.router.version(supported=2)
self.assertIsInstance(result, LocalRouter)
self.assertEqual(result.route["version"], 2)

## localrouter.call
def test_call_method(self):
"""Test LocalRouter __call__ method"""
def mock_function():
pass
with patch('hug.interface.Local') as mock_local:
self.router(mock_function)
mock_local.assert_called_once_with(self.router.route, mock_function)

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

64 changes: 64 additions & 0 deletions tests/test_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""This is the test module for the Router class,
made by Marwan Amrhar & Yahia Noeman & Ayoub Bouazza & Sajeed Bouzziane
It covers the function:
Router.__init__"""

import unittest
from hug.routing import Router

class TestRouterInit(unittest.TestCase):
def test_router_init_with_all_parameters(self):
"""Test Router initialization with all parameters"""
transform = lambda x: x
output = "output_format"
validate = "validate_function"
api = "api_object"
requires = ("requirement1", "requirement2")
map_params = {"param1": "mapped_param1"}
args = ["arg1", "arg2"]

router = Router(
transform=transform,
output=output,
validate=validate,
api=api,
requires=requires,
map_params=map_params,
args=args,
)

self.assertEqual(router.route["transform"], transform)
self.assertEqual(router.route["output"], output)
self.assertEqual(router.route["validate"], validate)
self.assertEqual(router.route["api"], api)
self.assertEqual(router.route["requires"], requires)
self.assertEqual(router.route["map_params"], map_params)
self.assertEqual(router.route["args"], args)

def test_router_init_with_default_parameters(self):
"""Test Router initialization with default parameters"""
router = Router()

self.assertNotIn("transform", router.route)
self.assertNotIn("output", router.route)
self.assertNotIn("validate", router.route)
self.assertNotIn("api", router.route)
self.assertEqual(router.route.get("requires", ()), ())
self.assertNotIn("map_params", router.route)
self.assertNotIn("args", router.route)

def test_router_init_with_single_requirement(self):
"""Test Router initialization with a single requirement"""
router = Router(requires="requirement1")

self.assertEqual(router.route["requires"], ("requirement1",))

def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestRouterInit))
return suite

if __name__ == "__main__":
runner = unittest.TextTestRunner()
runner.run(test_suite())