# Path: example/dj/apps/app/tests/test_case.py
# class PystonTestCase(RestTestCase):
# 
#     USER_API_URL = '/api/user/'
#     USER_WITH_FORM_API_URL = '/api/user-form/'
#     ISSUE_API_URL = '/api/issue/'
#     ISSUE_WITH_FORM_API_URL = '/api/issue-form/'
#     EXTRA_API_URL = '/api/extra/'
#     COUNT_ISSUES_PER_USER = '/api/count-issues-per-user/'
#     COUNT_WATCHERS_PER_ISSUE = '/api/count-watchers-per-issue/'
#     TEST_CC_API_URL = '/api/test-cc/'
# 
#     user_id = 0
#     issue_id = 0
# 
#     DATA_AMOUNT = 10
# 
#     def get_pk(self, resp):
#         return self.deserialize(resp).get('id')
# 
#     def get_pk_list(self, resp, only_pks=None):
#         return [obj.get('id') for obj in self.deserialize(resp) if not only_pks or obj.get('id') in only_pks]
# 
#     def get_user_data(self, prefix='', **kwargs):
#         result = {'email': '%suser_%s@test.cz' % (prefix, self.user_id)}
#         self.user_id += 1
#         result.update(kwargs)
#         return result
# 
#     def get_issue_data(self, prefix='', **kwargs):
#         result = {'name': 'Issue %s' % self.issue_id, 'created_by': self.get_user_data(prefix),
#                   'leader': self.get_user_data(prefix)}
#         self.issue_id += 1
#         result.update(kwargs)
#         return result
# 
#     def get_users_data(self, prefix='', flat=False):
#         result = []
#         for i in range(self.DATA_AMOUNT):
#             if flat:
#                 result.append(self.get_user_data(prefix))
#             else:
#                 result.append((i, self.get_user_data(prefix)))
#         return result
# 
#     def get_issues_data(self, prefix='', flat=False):
#         result = []
#         for i in range(self.DATA_AMOUNT):
#             result.append((i, self.get_issue_data(prefix)))
#         return result
# 
#     def get_issues_and_users_data(self, prefix=''):
#         result = []
#         for i in range(self.DATA_AMOUNT):
#             issue_data = self.get_issue_data(prefix)
#             user_data = issue_data['created_by']
#             del issue_data['created_by']
#             result.append((i, issue_data, user_data))
#         return result
# 
# Path: example/dj/apps/app/tests/factories.py
# class UserFactory(factory.django.DjangoModelFactory):
# 
#     first_name = factory.Sequence(lambda n: 'John{0}'.format(n))
#     last_name = factory.Sequence(lambda n: 'Doe{0}'.format(n))
#     email = factory.Sequence(lambda n: 'customer_{0}@example.com'.format(n).lower())
# 
#     class Meta:
#         model = models.User
# 
# class IssueFactory(factory.django.DjangoModelFactory):
# 
#     name = factory.fuzzy.FuzzyText(length=100)
#     created_by = factory.SubFactory('app.tests.factories.UserFactory')
#     leader = factory.SubFactory('app.tests.factories.UserFactory')
# 
#     class Meta:
#         model = models.Issue


from germanium.tools.trivials import assert_equal
from germanium.tools.http import assert_http_method_not_allowed
from germanium.tools.rest import assert_valid_JSON_response
from .test_case import PystonTestCase
from .factories import UserFactory, IssueFactory



class ExtraResourceTestCase(PystonTestCase):

    def test_not_supported_message_for_put_post_and_delete(self):
        resp = self.put(self.EXTRA_API_URL, data={})
        assert_http_method_not_allowed(resp)

        resp = self.post(self.EXTRA_API_URL, data={})
        assert_http_method_not_allowed(resp)

        resp = self.delete(self.EXTRA_API_URL)
        assert_http_method_not_allowed(resp)

    def test_should_return_data_for_get(self):
        resp = self.get(self.EXTRA_API_URL)
        assert_valid_JSON_response(resp)

    def test_resource_with_serializable_result(self):
        [IssueFactory(solver=UserFactory()) for _ in range(10)]

        resp_data = self.deserialize(self.get(self.COUNT_ISSUES_PER_USER))
        assert_equal(len(resp_data), 10 * 3)
        for row in resp_data:
            assert_equal(set(row.keys()), {'email', 'created_issues_count'})

    def test_resource_with_serializableobj_result(self):

# Path: example/dj/apps/app/tests/test_case.py
# class PystonTestCase(RestTestCase):
# 
#     USER_API_URL = '/api/user/'
#     USER_WITH_FORM_API_URL = '/api/user-form/'
#     ISSUE_API_URL = '/api/issue/'
#     ISSUE_WITH_FORM_API_URL = '/api/issue-form/'
#     EXTRA_API_URL = '/api/extra/'
#     COUNT_ISSUES_PER_USER = '/api/count-issues-per-user/'
#     COUNT_WATCHERS_PER_ISSUE = '/api/count-watchers-per-issue/'
#     TEST_CC_API_URL = '/api/test-cc/'
# 
#     user_id = 0
#     issue_id = 0
# 
#     DATA_AMOUNT = 10
# 
#     def get_pk(self, resp):
#         return self.deserialize(resp).get('id')
# 
#     def get_pk_list(self, resp, only_pks=None):
#         return [obj.get('id') for obj in self.deserialize(resp) if not only_pks or obj.get('id') in only_pks]
# 
#     def get_user_data(self, prefix='', **kwargs):
#         result = {'email': '%suser_%s@test.cz' % (prefix, self.user_id)}
#         self.user_id += 1
#         result.update(kwargs)
#         return result
# 
#     def get_issue_data(self, prefix='', **kwargs):
#         result = {'name': 'Issue %s' % self.issue_id, 'created_by': self.get_user_data(prefix),
#                   'leader': self.get_user_data(prefix)}
#         self.issue_id += 1
#         result.update(kwargs)
#         return result
# 
#     def get_users_data(self, prefix='', flat=False):
#         result = []
#         for i in range(self.DATA_AMOUNT):
#             if flat:
#                 result.append(self.get_user_data(prefix))
#             else:
#                 result.append((i, self.get_user_data(prefix)))
#         return result
# 
#     def get_issues_data(self, prefix='', flat=False):
#         result = []
#         for i in range(self.DATA_AMOUNT):
#             result.append((i, self.get_issue_data(prefix)))
#         return result
# 
#     def get_issues_and_users_data(self, prefix=''):
#         result = []
#         for i in range(self.DATA_AMOUNT):
#             issue_data = self.get_issue_data(prefix)
#             user_data = issue_data['created_by']
#             del issue_data['created_by']
#             result.append((i, issue_data, user_data))
#         return result
# 
# Path: example/dj/apps/app/tests/factories.py
# class UserFactory(factory.django.DjangoModelFactory):
# 
#     first_name = factory.Sequence(lambda n: 'John{0}'.format(n))
#     last_name = factory.Sequence(lambda n: 'Doe{0}'.format(n))
#     email = factory.Sequence(lambda n: 'customer_{0}@example.com'.format(n).lower())
# 
#     class Meta:
#         model = models.User
# 
# class IssueFactory(factory.django.DjangoModelFactory):
# 
#     name = factory.fuzzy.FuzzyText(length=100)
#     created_by = factory.SubFactory('app.tests.factories.UserFactory')
#     leader = factory.SubFactory('app.tests.factories.UserFactory')
# 
#     class Meta:
#         model = models.Issue


# Path: example/dj/apps/app/tests/extra_resource.py
from germanium.tools.trivials import assert_equal
from germanium.tools.http import assert_http_method_not_allowed
from germanium.tools.rest import assert_valid_JSON_response
from .test_case import PystonTestCase
from .factories import UserFactory, IssueFactory




class ExtraResourceTestCase(PystonTestCase):

    def test_not_supported_message_for_put_post_and_delete(self):
        resp = self.put(self.EXTRA_API_URL, data={})
        assert_http_method_not_allowed(resp)

        resp = self.post(self.EXTRA_API_URL, data={})
        assert_http_method_not_allowed(resp)

        resp = self.delete(self.EXTRA_API_URL)
        assert_http_method_not_allowed(resp)

    def test_should_return_data_for_get(self):
        resp = self.get(self.EXTRA_API_URL)
        assert_valid_JSON_response(resp)

    def test_resource_with_serializable_result(self):
        [IssueFactory(solver=UserFactory()) for _ in range(10)]

        resp_data = self.deserialize(self.get(self.COUNT_ISSUES_PER_USER))
        assert_equal(len(resp_data), 10 * 3)
        for row in resp_data:
            assert_equal(set(row.keys()), {'email', 'created_issues_count'})

    def test_resource_with_serializableobj_result(self):

        issues = [IssueFactory() for _ in range(10)]