# Path: dplace_app/load.py
# def load(repos, test=True):
#     configure_logging(test=test)
#     repos = Repos(repos)
# 
#     for func in [
#         load_societies,
#         load_society_relations,
#         load_regions,
#         society_locations,
#         load_vars,
#         load_languages,
#         load_references,
#         load_data,
#         load_phylogenies,
#     ]:
#         with transaction.atomic():
#             if not test:
#                 print("%s..." % func.__name__)  # pragma: no cover
#             start = time()
#             res = func(repos)
#             if not test:  # pragma: no cover
#                 print("{0} loaded in {1:.2f} secs".format(res, time() - start))
# 
# Path: dplace_app/loader/sources.py
# _SOURCE_CACHE = {}
# def get_source(ds):
# def load_references(repos):


import json
from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from clldutils.path import Path
from dplace_app.models import *
from dplace_app.load import load
from dplace_app.loader import sources
# coding: utf8
from __future__ import unicode_literals




class Test(APITestCase):
    """Tests rest-framework API"""
    def _fixture_teardown(self):
        try:
            APITestCase._fixture_teardown(self)
        except:
            pass

    def get_json(self, urlname, *args, **kw):
        kw.setdefault('format', 'json')
        reverse_args = kw.pop('reverse_args', [])
        response = self.client.get(reverse(urlname, args=reverse_args), *args, **kw)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        try:  # csv download doesn't return json
            return json.loads(response.content)
        except:
            return response.content

    def obj_in_results(self, obj, response):
        return getattr(obj, 'id', obj) in [x['id'] for x in response['results']]

    def setUp(self):
        sources._SOURCE_CACHE = {}

# Path: dplace_app/load.py
# def load(repos, test=True):
#     configure_logging(test=test)
#     repos = Repos(repos)
# 
#     for func in [
#         load_societies,
#         load_society_relations,
#         load_regions,
#         society_locations,
#         load_vars,
#         load_languages,
#         load_references,
#         load_data,
#         load_phylogenies,
#     ]:
#         with transaction.atomic():
#             if not test:
#                 print("%s..." % func.__name__)  # pragma: no cover
#             start = time()
#             res = func(repos)
#             if not test:  # pragma: no cover
#                 print("{0} loaded in {1:.2f} secs".format(res, time() - start))
# 
# Path: dplace_app/loader/sources.py
# _SOURCE_CACHE = {}
# def get_source(ds):
# def load_references(repos):


# Path: dplace_app/tests/test_api.py
import json
from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from clldutils.path import Path
from dplace_app.models import *
from dplace_app.load import load
from dplace_app.loader import sources

# coding: utf8
from __future__ import unicode_literals




class Test(APITestCase):
    """Tests rest-framework API"""
    def _fixture_teardown(self):
        try:
            APITestCase._fixture_teardown(self)
        except:
            pass

    def get_json(self, urlname, *args, **kw):
        kw.setdefault('format', 'json')
        reverse_args = kw.pop('reverse_args', [])
        response = self.client.get(reverse(urlname, args=reverse_args), *args, **kw)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        try:  # csv download doesn't return json
            return json.loads(response.content)
        except:
            return response.content

    def obj_in_results(self, obj, response):
        return getattr(obj, 'id', obj) in [x['id'] for x in response['results']]

    def setUp(self):
        sources._SOURCE_CACHE = {}

        load(Path(__file__).parent.joinpath('data'))