from django.test import TestCase, Client
from jsonapi.api import API
from jsonapi.resource import Resource

from ..resources import api


class TestApi(TestCase):
    def setUp(self):
        #self.api = api
        self.api = API()

    def test_resource_registration(self):
        class TestResource(Resource):
            class Meta:
                name = 'test'

        self.api.register(TestResource)
        self.assertEqual(self.api.resource_map['test'], TestResource)

    def test_resource_registration_decorator(self):
        @self.api.register
        class TestResource(Resource):
            class Meta:
                name = 'test'

        self.assertEqual(self.api.resource_map['test'], TestResource)

    def test_resource_registration_decorator_params(self):
        @self.api.register()
        class TestResource(Resource):
            class Meta:
                name = 'test'

        self.assertEqual(self.api.resource_map['test'], TestResource)

    def test_recource_api_reference(self):
        class TestResource(Resource):
            class Meta:
                name = 'test'

        self.assertFalse(hasattr(TestResource.Meta, 'api'))
        self.api.register(TestResource)
        self.assertTrue(TestResource.Meta.api is self.api)

    def test_resource_name_collapse_same_name(self):
        class NewsResource(Resource):
            class Meta:
                name = 'news'

        class NewsOtherResource(Resource):
            class Meta:
                name = 'news'

        self.api.register(NewsResource)
        with self.assertRaises(ValueError):
            # The same resource name
            self.api.register(NewsOtherResource)

    def test_resource_name_collapse_with_plural(self):
        class NewsResource(Resource):
            class Meta:
                name = 'news'

        class NewResource(Resource):
            class Meta:
                name = 'new'

        self.api.register(NewsResource)
        with self.assertRaises(ValueError):
            # NewResource.name_plural = NewsResource.name
            self.api.register(NewResource)

    def test_resource_name_collapse_with_singular(self):
        class NewsResource(Resource):
            class Meta:
                name = 'news'

        class NewssResource(Resource):
            class Meta:
                name = 'newss'

        self.api.register(NewsResource)
        with self.assertRaises(ValueError):
            # NewsResource.name_plural = NewssResource.name
            self.api.register(NewssResource)

    def test_base_url(self):
        self.assertTrue(self.api.base_url is None)
        c = Client()
        c.get(
            '/api/author',
            content_type='application/vnd.api+json'
        )
        self.assertEqual(self.api.base_url, "http://testserver")

    def test_api_url(self):
        self.assertTrue(self.api.api_url is None)
        c = Client()
        c.get(
            '/api/author',
            content_type='application/vnd.api+json'
        )
        self.assertEqual(self.api.api_url, "http://testserver/api")

@@ -108,24 +108,19 @@
 %0Afrom ..
-resource
+url
 s import
@@ -159,48 +159,95 @@
     
-def setUp(self):%0A        #self.api = api
+urls = 'testapp.urls'  # does not work without it. Dont know why?%0A%0A    def setUp(self):
 %0A   
@@ -2622,125 +2622,42 @@
     
-self.assertTrue(self.api.base_url is None)%0A        c = Client()%0A        c.get(%0A            '/api/author',%0A           
+c = Client()%0A        c.get('/api',
  con
@@ -2688,33 +2688,24 @@
 nd.api+json'
-%0A        
 )%0A        se
@@ -2715,29 +2715,24 @@
 assertEqual(
-self.
 api.base_url
@@ -2795,124 +2795,42 @@
     
-self.assertTrue(self.api.api_url is None)%0A        c = Client()%0A        c.get(%0A            '/api/author',%0A           
+c = Client()%0A        c.get('/api',
  con
@@ -2865,25 +2865,16 @@
 pi+json'
-%0A        
 )%0A      
@@ -2888,29 +2888,24 @@
 assertEqual(
-self.
 api.api_url,

fix tests execution