<commit_before>import unittest
import urllib.parse
from collections import OrderedDict
from unittest import mock

from tornado.testing import LogTrapTestCase
from tornadowebapi.http import httpstatus
from tornadowebapi.registry import Registry
from tornadowebapi.traitlets import Absent
from tornadowebapi.web_handlers import ResourceWebHandler, CollectionWebHandler
from tornadowebapi.tests import resource_handlers
from tornadowebapi.tests.utils import AsyncHTTPTestCase
from tornado import web, escape

ALL_RESOURCES = (
    resource_handlers.AlreadyPresentHandler,
    resource_handlers.ExceptionValidatedHandler,
    resource_handlers.NullReturningValidatedHandler,
    resource_handlers.CorrectValidatedHandler,
    resource_handlers.OurExceptionValidatedHandler,
    resource_handlers.BrokenHandler,
    resource_handlers.UnsupportsCollectionHandler,
    resource_handlers.UnprocessableHandler,
    resource_handlers.UnsupportAllHandler,
    resource_handlers.StudentHandler,
    resource_handlers.TeacherHandler,
    resource_handlers.InvalidIdentifierHandler,
    resource_handlers.OurExceptionInvalidIdentifierHandler,
)


class TestWebAPI(AsyncHTTPTestCase, LogTrapTestCase):
    def setUp(self):
        super().setUp()
        resource_handlers.StudentHandler.collection = OrderedDict()
        resource_handlers.StudentHandler.id = 0

    def get_app(self):
        registry = Registry()
        handlers = registry.api_handlers('/')
        for resource in ALL_RESOURCES:
            registry.register(resource)
        app = web.Application(handlers=handlers)
        app.hub = mock.Mock()
        return app

    def test_items(self):
        res = self.fetch("/api/v1/students/")

        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {
                             "total": 0,
                             "offset": 0,
                             "items": {}})

        handler = resource_handlers.StudentHandler
        handler.collection[1] = handler.resource_class(identifier="1")
        handler.collection[2] = handler.resource_class(identifier="2")
        handler.collection[3] = handler.resource_class(identifier="3")

        res = self.fetch("/api/v1/students/")
        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {
                             "total": 3,
                             "offset": 0,
                             "items": {
                                 "1": {},
                                 "2": {},
                                 "3": {}}
                         })

    def test_items_with_query_params(self):
        res = self.fetch("/api/v1/students/?limit=1")

        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {"total": 0,
                          "offset": 0,
                          "items": {}})

        res = self.fetch("/api/v1/students/?offset=1")

        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {
                             "total": 0,
                             "offset": 1,
                             "items": {}
                         })

        res = self.fetch("/api/v1/students/?offset=1&limit=1")

        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {
                             "total": 0,
                             "offset": 1,
                             "items": {}
                         })

        handler = resource_handlers.StudentHandler
        handler.collection[1] = handler.resource_class(identifier="1")
        handler.collection[2] = handler.resource_class(identifier="2")
        handler.collection[3] = handler.resource_class(identifier="3")

        res = self.fetch("/api/v1/students/?limit=2")
        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {
                             "total": 3,
                             "offset": 0,
                             "items": {"1": {},
                                       "2": {}}})

        res = self.fetch("/api/v1/students/?offset=1")
        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {
                             "total": 3,
                             "offset": 1,
                             "items": {
                                 "2": {},
                                 "3": {}
                             }
                         })

        res = self.fetch("/api/v1/students/?offset=1&limit=1")
        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {
                             "total": 3,
                             "offset": 1,
                             "items": {
                                 "2": {}
                             }
                         })

    def test_create(self):
        res = self.fetch(
            "/api/v1/students/",
            method="POST",
            body=escape.json_encode({
                "name": "<NAME>",
                "age": 19,
            })
        )

        self.assertEqual(res.code, httpstatus.CREATED)
        self.assertIn("api/v1/students/0/", res.headers["Location"])

        res = self.fetch(
            "/api/v1/students/",
            method="POST",
            body=escape.json_encode({
                "name": "<NAME> 2",<commit_msg>Fixed tests<commit_after>import unittest
import urllib.parse
from collections import OrderedDict
from unittest import mock

from tornado.testing import LogTrapTestCase
from tornadowebapi.http import httpstatus
from tornadowebapi.registry import Registry
from tornadowebapi.traitlets import Absent
from tornadowebapi.web_handlers import ResourceWebHandler, CollectionWebHandler
from tornadowebapi.tests import resource_handlers
from tornadowebapi.tests.utils import AsyncHTTPTestCase
from tornado import web, escape

ALL_RESOURCES = (
    resource_handlers.AlreadyPresentHandler,
    resource_handlers.ExceptionValidatedHandler,
    resource_handlers.NullReturningValidatedHandler,
    resource_handlers.CorrectValidatedHandler,
    resource_handlers.OurExceptionValidatedHandler,
    resource_handlers.BrokenHandler,
    resource_handlers.UnsupportsCollectionHandler,
    resource_handlers.UnprocessableHandler,
    resource_handlers.UnsupportAllHandler,
    resource_handlers.StudentHandler,
    resource_handlers.TeacherHandler,
    resource_handlers.InvalidIdentifierHandler,
    resource_handlers.OurExceptionInvalidIdentifierHandler,
)


class TestWebAPI(AsyncHTTPTestCase, LogTrapTestCase):
    def setUp(self):
        super().setUp()
        resource_handlers.StudentHandler.collection = OrderedDict()
        resource_handlers.StudentHandler.id = 0

    def get_app(self):
        registry = Registry()
        handlers = registry.api_handlers('/')
        for resource in ALL_RESOURCES:
            registry.register(resource)
        app = web.Application(handlers=handlers)
        app.hub = mock.Mock()
        return app

    def test_items(self):
        res = self.fetch("/api/v1/students/")

        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {
                             "total": 0,
                             "offset": 0,
                             "items": {}})

        handler = resource_handlers.StudentHandler
        handler.collection[1] = handler.resource_class(
            identifier="1",
            name="<NAME>",
            age=39)
        handler.collection[2] = handler.resource_class(
            identifier="2",
            name="<NAME>",
            age=39)
        handler.collection[3] = handler.resource_class(
            identifier="3",
            name="<NAME>",
            age=39)

        res = self.fetch("/api/v1/students/")
        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {
                             "total": 3,
                             "offset": 0,
                             "items": {
                                 "1": {
                                     "name": "<NAME>",
                                     "age": 39,
                                 },
                                 "2": {
                                     "name": "<NAME>",
                                     "age": 39,
                                 },
                                 "3": {
                                     "name": "<NAME>",
                                     "age": 39,
                                 }
                             }
                         })

    def test_items_with_query_params(self):
        res = self.fetch("/api/v1/students/?limit=1")

        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {"total": 0,
                          "offset": 0,
                          "items": {}})

        res = self.fetch("/api/v1/students/?offset=1")

        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {
                             "total": 0,
                             "offset": 1,
                             "items": {}
                         })

        res = self.fetch("/api/v1/students/?offset=1&limit=1")

        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {
                             "total": 0,
                             "offset": 1,
                             "items": {}
                         })

        handler = resource_handlers.StudentHandler
        handler.collection[1] = handler.resource_class(
            identifier="1",
            name="<NAME>",
            age=39)
        handler.collection[2] = handler.resource_class(
            identifier="2",
            name="<NAME>",
            age=39)
        handler.collection[3] = handler.resource_class(
            identifier="3",
            name="<NAME>",
            age=39)

        res = self.fetch("/api/v1/students/?limit=2")
        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {
                             "total": 3,
                             "offset": 0,
                             "items": {
                                 "1": {
                                     "name": "<NAME>",
                                     "age": 39,
                                 },
                                 "2": {
                                     "name": "<NAME>",
                                     "age": 39,
                                 }
                             }
                         })

        res = self.fetch("/api/v1/students/?offset=1")
        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {
                             "total": 3,
                             "offset": 1,
                             "items": {
                                 "2": {
                                     "name": "<NAME>",
                                     "age": 39,
                                 },
                                 "3": {
                                     "name": "<NAME>",
                                     "age": 39,
                                 }
                             }
                         })

        res = self.fetch("/api/v1/students/?offset=1&limit=1")
        self.assertEqual(res.code, httpstatus.OK)
        self.assertEqual(escape.json_decode(res.body),
                         {
                             "total": 3,
                             "offset": 1,
                             "items": {
                                 "2": {
                                     "name": "<NAME>",
                                     "age": 39,
                                 }
                             }
                         })

    def test_create(self):
        res = self.fetch(
            "/api/v1/students/",
            method="POST",
            body=escape.json_encode({
                "name": "<NAME>",
                "age": 19,
            })
        )

        self.assertEqual(res.code, httpstatus.CREATED)
        self.assertIn("api/v1/students/0/", res.headers["Location"])

        res = self.fetch(
            "/api/v1/students/",
            method="POST",
            body=escape.json_encode({
                "name": "<NAME>",
{'fraction_non_alphanumeric': 0.10548134500230309, 'fraction_numerical': 0.011285122063565177, 'mean_word_length': 1.215099472878762, 'pattern_counts': {'":': 86, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5144956', 'n_tokens_mistral': 3323, 'n_tokens_neox': 3138, 'n_words': 485}