from mock import patch

from django.test import TestCase
from rest_framework.reverse import reverse

from .. import serializers
from . import factories
from pages.utils import build_url


class PageSerializerTest(TestCase):
    def expected_data(self, page):
        expected = {
            'id': page.pk,
            'url': page.get_absolute_url(),
            'name': page.name,
            'slug': page.slug,
            'regions': page.rendered_regions(),
        }
        return expected

    def test_serialize(self):
        page = factories.PageFactory.create()
        serializer = serializers.PageSerializer(page)
        self.assertEqual(serializer.data, self.expected_data(page))


class GroupTest(TestCase):
    url_path = 'pages.models.Group.get_absolute_url'
    mocked_url = '/mocked_url'

    def expected_data(self, group):
        slug = group.slug
        return {
            'url': self.mocked_url,
            'slug': slug,
            'links': {
                'pages': build_url(reverse('pages:page-list'), {'group': slug}),
            },
        }

    @patch(url_path)
    def test_serialize(self, group_url):
        group_url.return_value = self.mocked_url
        group = factories.GroupFactory.create()

        serializer = serializers.GroupSerializer(group)
        self.assertEqual(serializer.data, self.expected_data(group))

from rest_framework.reverse import reverse
from user_management.models.tests.utils import APIRequestTestCase

from .. import serializers
from . import factories
from pages.utils import build_url


class PageSerializerTest(APIRequestTestCase):
    def setUp(self):
        self.request = self.create_request()
        self.context = {'request': self.request}

    def expected_data(self, page):
        expected = {
            'id': page.pk,
            'url': page.get_absolute_url(self.request),
            'name': page.name,
            'slug': page.slug,
            'regions': page.rendered_regions(),
        }
        return expected

    def test_serialize(self):
        page = factories.PageFactory.create()
        serializer = serializers.PageSerializer(page, context=self.context)
        self.assertEqual(serializer.data, self.expected_data(page))


class GroupTest(APIRequestTestCase):
    def setUp(self):
        self.request = self.create_request()
        self.context = {'request': self.request}

    def expected_data(self, group):
        slug = group.slug
        return {
            'url': group.get_absolute_url(self.request),
            'slug': slug,
            'links': {
                'pages': build_url(
                    reverse('pages:page-list', request=self.request),
                    {'group': slug},
                ),
            },
        }

    def test_serialize(self):
        group = factories.GroupFactory.create()

        serializer = serializers.GroupSerializer(group, context=self.context)
        self.assertEqual(serializer.data, self.expected_data(group))

Fix DeprecationWarnings for serializer context
Fix DeprecationWarnings for serializer context

* Pass a context containing a request to every serializer

Conflicts:
	pages/tests/test_serializers.py

- from mock import patch
- 
- from django.test import TestCase
  from rest_framework.reverse import reverse
+ from user_management.models.tests.utils import APIRequestTestCase
  
  from .. import serializers
  from . import factories
  from pages.utils import build_url
  
  
- class PageSerializerTest(TestCase):
+ class PageSerializerTest(APIRequestTestCase):
+     def setUp(self):
+         self.request = self.create_request()
+         self.context = {'request': self.request}
+ 
      def expected_data(self, page):
          expected = {
              'id': page.pk,
-             'url': page.get_absolute_url(),
+             'url': page.get_absolute_url(self.request),
              'name': page.name,
              'slug': page.slug,
              'regions': page.rendered_regions(),
          }
          return expected
  
      def test_serialize(self):
          page = factories.PageFactory.create()
-         serializer = serializers.PageSerializer(page)
+         serializer = serializers.PageSerializer(page, context=self.context)
          self.assertEqual(serializer.data, self.expected_data(page))
  
  
- class GroupTest(TestCase):
+ class GroupTest(APIRequestTestCase):
-     url_path = 'pages.models.Group.get_absolute_url'
-     mocked_url = '/mocked_url'
+     def setUp(self):
+         self.request = self.create_request()
+         self.context = {'request': self.request}
  
      def expected_data(self, group):
          slug = group.slug
          return {
-             'url': self.mocked_url,
+             'url': group.get_absolute_url(self.request),
              'slug': slug,
              'links': {
-                 'pages': build_url(reverse('pages:page-list'), {'group': slug}),
+                 'pages': build_url(
+                     reverse('pages:page-list', request=self.request),
+                     {'group': slug},
+                 ),
              },
          }
  
-     @patch(url_path)
-     def test_serialize(self, group_url):
+     def test_serialize(self):
-         group_url.return_value = self.mocked_url
          group = factories.GroupFactory.create()
  
-         serializer = serializers.GroupSerializer(group)
+         serializer = serializers.GroupSerializer(group, context=self.context)
          self.assertEqual(serializer.data, self.expected_data(group))
  
Fix DeprecationWarnings for serializer context
## Code Before:
from mock import patch

from django.test import TestCase
from rest_framework.reverse import reverse

from .. import serializers
from . import factories
from pages.utils import build_url


class PageSerializerTest(TestCase):
    def expected_data(self, page):
        expected = {
            'id': page.pk,
            'url': page.get_absolute_url(),
            'name': page.name,
            'slug': page.slug,
            'regions': page.rendered_regions(),
        }
        return expected

    def test_serialize(self):
        page = factories.PageFactory.create()
        serializer = serializers.PageSerializer(page)
        self.assertEqual(serializer.data, self.expected_data(page))


class GroupTest(TestCase):
    url_path = 'pages.models.Group.get_absolute_url'
    mocked_url = '/mocked_url'

    def expected_data(self, group):
        slug = group.slug
        return {
            'url': self.mocked_url,
            'slug': slug,
            'links': {
                'pages': build_url(reverse('pages:page-list'), {'group': slug}),
            },
        }

    @patch(url_path)
    def test_serialize(self, group_url):
        group_url.return_value = self.mocked_url
        group = factories.GroupFactory.create()

        serializer = serializers.GroupSerializer(group)
        self.assertEqual(serializer.data, self.expected_data(group))

## Instruction:
Fix DeprecationWarnings for serializer context
## Code After:
from rest_framework.reverse import reverse
from user_management.models.tests.utils import APIRequestTestCase

from .. import serializers
from . import factories
from pages.utils import build_url


class PageSerializerTest(APIRequestTestCase):
    def setUp(self):
        self.request = self.create_request()
        self.context = {'request': self.request}

    def expected_data(self, page):
        expected = {
            'id': page.pk,
            'url': page.get_absolute_url(self.request),
            'name': page.name,
            'slug': page.slug,
            'regions': page.rendered_regions(),
        }
        return expected

    def test_serialize(self):
        page = factories.PageFactory.create()
        serializer = serializers.PageSerializer(page, context=self.context)
        self.assertEqual(serializer.data, self.expected_data(page))


class GroupTest(APIRequestTestCase):
    def setUp(self):
        self.request = self.create_request()
        self.context = {'request': self.request}

    def expected_data(self, group):
        slug = group.slug
        return {
            'url': group.get_absolute_url(self.request),
            'slug': slug,
            'links': {
                'pages': build_url(
                    reverse('pages:page-list', request=self.request),
                    {'group': slug},
                ),
            },
        }

    def test_serialize(self):
        group = factories.GroupFactory.create()

        serializer = serializers.GroupSerializer(group, context=self.context)
        self.assertEqual(serializer.data, self.expected_data(group))
