# Path: tests/factories.py
# class QueryFactory(TagsFactory):
#     class Meta:
#         model = website.models.Query
# 
#     title = "title"
#     description = "description"
#     db = factory.SubFactory(DbFactory)
#     owner = factory.SubFactory(UserFactory)
#     graph_extra = "{}"
#     query_text = "select username, last_login from auth_user"
#     pivot_data = False
#     insert_limit = True
# 
# class QueryDefaultFactory(factory.django.DjangoModelFactory):
#     class Meta:
#         model = website.models.QueryDefault
#     query = factory.SubFactory(QueryFactory)
#     search_for = "<DEFAULT>"
#     replace_with = ""
#     data_type = 'String'
# 
# class UserFactory(factory.DjangoModelFactory):
#     class Meta:
#         model = User
# 
#     username = factory.Sequence(lambda x: "user{}".format(x))
#     email = factory.Sequence(lambda x: "user{}@example.com".format(x))
#     password = factory.PostGenerationMethodCall('set_password', "password")
# 
# class QueryPrecedentFactory(factory.django.DjangoModelFactory):
#     class Meta:
#         model = website.models.QueryPrecedent
#     final_query = factory.SubFactory(QueryFactory)
#     preceding_query = factory.SubFactory(QueryFactory)
# 
# Path: tests/functional/testcases.py
# class APITestCase(TestCaseMixin, TransactionTestCase):
# 
#     def login(self):
#         self.client.login(username=self.username, password=self.password)


import json
import datetime
import os
from ..factories import QueryFactory, QueryDefaultFactory, UserFactory, \
    QueryPrecedentFactory
from .testcases import APITestCase
        endpoints = {
            '/api/query/1':
                'http://testserver/accounts/login?next=/api/query/1',
            '/api/query_interactive/':
                'http://testserver/admin/login/?next=/api/query_interactive/',
            '/api/database_explorer/':
                'http://testserver/admin/login/?next=/api/database_explorer/'
        }
        for resp, endpoint in endpoints.iteritems():
            response = self.client.get(resp)
            self.assertEqual(response.status_code, 302)
            #  logging.warning(response['location'])
            self.assertEqual(response['location'], endpoint)

    def test_invalid_query_id(self):
        """
        Test a 404 for query api
        """
        self.create_user()
        self.login()
        data = self.get_query(1)
        self.assert_query_data(
            data,
            error=True,
            data="No Query matches the given query."
        )

    def test_unsafe_query(self):
        user = self.create_user()
        self.login()

# Path: tests/factories.py
# class QueryFactory(TagsFactory):
#     class Meta:
#         model = website.models.Query
# 
#     title = "title"
#     description = "description"
#     db = factory.SubFactory(DbFactory)
#     owner = factory.SubFactory(UserFactory)
#     graph_extra = "{}"
#     query_text = "select username, last_login from auth_user"
#     pivot_data = False
#     insert_limit = True
# 
# class QueryDefaultFactory(factory.django.DjangoModelFactory):
#     class Meta:
#         model = website.models.QueryDefault
#     query = factory.SubFactory(QueryFactory)
#     search_for = "<DEFAULT>"
#     replace_with = ""
#     data_type = 'String'
# 
# class UserFactory(factory.DjangoModelFactory):
#     class Meta:
#         model = User
# 
#     username = factory.Sequence(lambda x: "user{}".format(x))
#     email = factory.Sequence(lambda x: "user{}@example.com".format(x))
#     password = factory.PostGenerationMethodCall('set_password', "password")
# 
# class QueryPrecedentFactory(factory.django.DjangoModelFactory):
#     class Meta:
#         model = website.models.QueryPrecedent
#     final_query = factory.SubFactory(QueryFactory)
#     preceding_query = factory.SubFactory(QueryFactory)
# 
# Path: tests/functional/testcases.py
# class APITestCase(TestCaseMixin, TransactionTestCase):
# 
#     def login(self):
#         self.client.login(username=self.username, password=self.password)


# Path: tests/functional/test_api.py
import json
import datetime
import os
from ..factories import QueryFactory, QueryDefaultFactory, UserFactory, \
    QueryPrecedentFactory
from .testcases import APITestCase

        endpoints = {
            '/api/query/1':
                'http://testserver/accounts/login?next=/api/query/1',
            '/api/query_interactive/':
                'http://testserver/admin/login/?next=/api/query_interactive/',
            '/api/database_explorer/':
                'http://testserver/admin/login/?next=/api/database_explorer/'
        }
        for resp, endpoint in endpoints.iteritems():
            response = self.client.get(resp)
            self.assertEqual(response.status_code, 302)
            #  logging.warning(response['location'])
            self.assertEqual(response['location'], endpoint)

    def test_invalid_query_id(self):
        """
        Test a 404 for query api
        """
        self.create_user()
        self.login()
        data = self.get_query(1)
        self.assert_query_data(
            data,
            error=True,
            data="No Query matches the given query."
        )

    def test_unsafe_query(self):
        user = self.create_user()
        self.login()

        query = QueryFactory(