Source

vlastic / test / rom_test.py

Full commit
"""Unit tests for ROM(Resource-Object Mapping) module."""

import unittest
from vlastic.rom import *
from vlastic.rom.decorator import *
from vlastic.http import *

def test_view(context):
    return Response(
        ("HTTP/1.1", 200, "OK"),
        {"Content-Type": "text/plain"},
        context["message"]
    )


class TestResource(Resource):
    """A resource class for unit tests."""

    def __init__(self, value):
        self.value = str(value)

    @get(test_view)
    def get_method_test(self):
        return {"message": "GET method test"}

    @post(test_view)
    @put(lambda context, req: "PUT: " + context["message"] + " " +str(req.path))
    def post_method_test(self):
        return {"message": self.value}

    custom_method = method("Custom")(test_view)

    @custom_method
    def custom_method_test(self):
        return {"message": "CUSTOM method test"}


class ResourceTest(unittest.TestCase):
    """Unit tests for Resource class with simple request specified by
    HTTP 0.9.

    """

    def setUp(self):
        self.rsc = TestResource("root")

    def test_dispatch_method_handler(self):
        from vlastic.rom.resource import dispatch_method_handler as dispatch
        self.assertEquals(
            id(self.rsc.get_method_test),
            id(dispatch(self.rsc, "GET")[0])
        )
        self.assertEquals(
            id(test_view),
            id(dispatch(self.rsc, "GET")[1])
        )

    def test_root_get(self):
        self.assertEquals(
            {"message": "GET method test"},
            self.rsc.get_method_test()
        )
        self.assertEquals(
            "GET method test",
            self.rsc(Request("GET / HTTP/1.1\r\n\r\n")).body
        )

    def test_root_post(self):
        self.assertEquals(
            {"message": "root"},
            self.rsc.post_method_test()
        )
        self.assertEquals(
            "root",
            self.rsc(Request("POST / HTTP/1.1\r\n\r\n")).body
        )

    def test_root_put(self):
        self.assertEquals(
            "PUT: root /",
            self.rsc(Request("PUT / HTTP/1.1\r\n\r\n")).body
        )

    def test_root_custom(self):
        self.assertEquals(
            {"message": "CUSTOM method test"},
            self.rsc.custom_method_test()
        )
        self.assertEquals(
            "CUSTOM method test",
            self.rsc(Request("CUSTOM / HTTP/1.1\r\n\r\n")).body
        )

    def test_root_error(self):
        for method in ["DELETE", "UNDef"]:
            response = self.rsc(Request(method + " / HTTP/1.1\r\n\r\n"))
            self.assertTrue(
                isinstance(response, MethodNotAllowedError),
                "Resource should return MethodNotAllowedError when the method"
                " is unsupported."
            )


class TestObjectResource(ObjectResource):
    """A object resource class for unit tests."""

    def __init__(self, value):
        self.abc = TestResource("abc")
        self.value = value

    get_method_test = TestResource.get_method_test
    post_method_test = TestResource.post_method_test
    custom_method_test = TestResource.custom_method_test


class ObjectResourceTest(ResourceTest):
    """Unit tests for ObjectResource."""

    def setUp(self):
        self.rsc = TestObjectResource("root")

    def test_child(self):
        self.assertEquals(
            "GET method test",
            self.rsc(Request("GET /abc HTTP/1.1\r\n\r\n")).body
        )
        self.assertEquals(
            "abc",
            self.rsc(Request("POST /abc HTTP/1.1\r\n\r\n")).body
        )

    def test_404(self):
        res = self.rsc(Request("GET /no HTTP/1.1\r\n\r\n"))
        self.assertTrue(
            isinstance(res, NotFoundError),
            "Resource should return NotFoundError when there isn't the child."
        )


class TestDictionaryResource(DictionaryResource):
    """A simple class for unit testing DictionaryResource."""

    def __init__(self, name):
        self.name = name

    @get(test_view)
    def get_method_test(self):
        return {"message": "{0} can accept GET message.".format(self.name)}


class DictionaryResourceTest(unittest.TestCase):
    """Unit tests for DictionaryResource class with simple request
    specified by HTTP 0.9.

    """

    def setUp(self):
        self.root = TestDictionaryResource("Test Dictionary Resource")
        
    def test_root(self):
        self.assertEquals(
            "Test Dictionary Resource can accept GET message.",
            self.root(Request("GET / HTTP/1.1\r\n\r\n")).body
        )
        response = self.root(Request("POST / HTTP/1.1\r\n\r\n"))
        self.assertTrue(isinstance(response, MethodNotAllowedError))

    def test_child(self):
        self.root["abc"] = TestDictionaryResource("Test Child Resource")
        self.assertEquals(
            "Test Child Resource can accept GET message.",
            self.root(Request("GET /abc HTTP/1.1\r\n\r\n")).body
        )
        from functools import partial
        response = self.root(Request("GET /a HTTP/1.1\r\n\r\n"))
        self.assertTrue(isinstance(response, NotFoundError))
        response = partial(self.root.__getitem__, 1)
        self.assertRaises(KeyError, response)