# Path: rest_api_framework/controllers.py
# class WSGIDispatcher(DispatcherMiddleware):
#     """
#     WSGIDispatcher take a list of :class:`.Controller` and mount them
#     on their ressource mount point.
#     basic syntax is:
# 
#     .. code-block:: python
# 
#        app = WSGIDispatcher([FirstApp, SecondApp])
#     """
# 
#     def __init__(self, apps, name='PRAF', version='devel',
#                  base_url=None, formats=None, autodoc=True,
#                  autospore=True, hello=True):
#         if formats is None:
#             formats = []
#         endpoints = {}
#         for elem in apps:
#             endpoints["/{0}".format(elem.ressource["ressource_name"])] = elem()
#         if not formats:
#             formats = ["json"]
#         if autodoc:
#             endpoints["/schema"] = self.make_schema(apps)
#         if autospore:
#             endpoints["/spore"] = self.make_spore(apps, name, base_url,
#                                                   version)
#         if hello:
#             endpoints["/"] = self.make_hello(name, version)
# 
#         app = NotFound()
#         mounts = endpoints
#         super(WSGIDispatcher, self).__init__(app, mounts=mounts)
# 
#     def make_schema(self, apps):
#         return AutoDocGenerator(apps)
# 
#     def make_spore(self, apps, name, base_url, version):
#         return AutoSporeGenerator(apps, name, base_url, version)
# 
#     def make_hello(self, name, version):
#         return HelloGenerator(name, version)
# 
# Path: tests/app.py
# class ApiApp(Controller):
#     ressource = {
#         "ressource_name": "address",
#         "ressource": ressources,
#         "model": ApiModel,
#         "datastore": PythonListDataStore
#         }
# 
#     controller = {
#         "list_verbs": ["GET", "POST"],
#         "unique_verbs": ["GET", "PUT", "DELETE"],
#         "options": {"pagination": Pagination(20)}
#         }
# 
#     view = {"response_class": JsonResponse}


from unittest import TestCase
from werkzeug.wrappers import BaseResponse
from werkzeug.test import Client
from rxjson import Rx
from rest_api_framework.controllers import WSGIDispatcher
from .app import ApiApp
import json
import os

HERE = os.path.dirname(os.path.abspath(__file__))


class TestSpore(TestCase):

    def test_spore(self):
        client = Client(WSGIDispatcher([ApiApp], name='ApiApp', version='1.0',
                                       base_url='http://apiapp.com'),
                        response_wrapper=BaseResponse)
        resp = client.get("/spore/")

        self.assertEqual(resp.status_code, 200)
        spore = json.loads(resp.data)

        # basic fields
        self.assertEqual(spore['name'], 'ApiApp')
        self.assertEqual(spore['base_url'], 'http://apiapp.com')
        self.assertEqual(spore['version'], '1.0')

        # methods
        self.assertIn('list_address', spore['methods'])
        self.assertEqual('/address/:identifier/',
                         spore['methods']['get_address']['path'])
        self.assertIn(
            'identifier',
            spore['methods']['get_address']['required_params'])

    def test_rxjson_spore(self):
        rx = Rx.Factory({'register_core_types': True})

# Path: rest_api_framework/controllers.py
# class WSGIDispatcher(DispatcherMiddleware):
#     """
#     WSGIDispatcher take a list of :class:`.Controller` and mount them
#     on their ressource mount point.
#     basic syntax is:
# 
#     .. code-block:: python
# 
#        app = WSGIDispatcher([FirstApp, SecondApp])
#     """
# 
#     def __init__(self, apps, name='PRAF', version='devel',
#                  base_url=None, formats=None, autodoc=True,
#                  autospore=True, hello=True):
#         if formats is None:
#             formats = []
#         endpoints = {}
#         for elem in apps:
#             endpoints["/{0}".format(elem.ressource["ressource_name"])] = elem()
#         if not formats:
#             formats = ["json"]
#         if autodoc:
#             endpoints["/schema"] = self.make_schema(apps)
#         if autospore:
#             endpoints["/spore"] = self.make_spore(apps, name, base_url,
#                                                   version)
#         if hello:
#             endpoints["/"] = self.make_hello(name, version)
# 
#         app = NotFound()
#         mounts = endpoints
#         super(WSGIDispatcher, self).__init__(app, mounts=mounts)
# 
#     def make_schema(self, apps):
#         return AutoDocGenerator(apps)
# 
#     def make_spore(self, apps, name, base_url, version):
#         return AutoSporeGenerator(apps, name, base_url, version)
# 
#     def make_hello(self, name, version):
#         return HelloGenerator(name, version)
# 
# Path: tests/app.py
# class ApiApp(Controller):
#     ressource = {
#         "ressource_name": "address",
#         "ressource": ressources,
#         "model": ApiModel,
#         "datastore": PythonListDataStore
#         }
# 
#     controller = {
#         "list_verbs": ["GET", "POST"],
#         "unique_verbs": ["GET", "PUT", "DELETE"],
#         "options": {"pagination": Pagination(20)}
#         }
# 
#     view = {"response_class": JsonResponse}


# Path: tests/test_spore.py
from unittest import TestCase
from werkzeug.wrappers import BaseResponse
from werkzeug.test import Client
from rxjson import Rx
from rest_api_framework.controllers import WSGIDispatcher
from .app import ApiApp
import json
import os


HERE = os.path.dirname(os.path.abspath(__file__))


class TestSpore(TestCase):

    def test_spore(self):
        client = Client(WSGIDispatcher([ApiApp], name='ApiApp', version='1.0',
                                       base_url='http://apiapp.com'),
                        response_wrapper=BaseResponse)
        resp = client.get("/spore/")

        self.assertEqual(resp.status_code, 200)
        spore = json.loads(resp.data)

        # basic fields
        self.assertEqual(spore['name'], 'ApiApp')
        self.assertEqual(spore['base_url'], 'http://apiapp.com')
        self.assertEqual(spore['version'], '1.0')

        # methods
        self.assertIn('list_address', spore['methods'])
        self.assertEqual('/address/:identifier/',
                         spore['methods']['get_address']['path'])
        self.assertIn(
            'identifier',
            spore['methods']['get_address']['required_params'])

    def test_rxjson_spore(self):
        rx = Rx.Factory({'register_core_types': True})

        client = Client(WSGIDispatcher([ApiApp], name='ApiApp', version='1.0',