# Path: uploads/models.py
# class Document(models.Model):
#     """Note that a number of fields have indexes in the database despite
#     lacking them here:
# 
#     mysql> show index from uploads_document;
#         +------------------+-----------------+-------------+-------------+
#         | Table            | Key_name        | Column_name | Cardinality |
#         +------------------+-----------------+-------------+-------------+
#         | uploads_document | PRIMARY         | id          |    26652929 |
#         | uploads_document | court           | court       |         206 |
#         | uploads_document | court           | casenum     |     1025112 |
#         | uploads_document | court           | docnum      |    26652929 |
#         | uploads_document | court           | subdocnum   |    26652929 |
#         | uploads_document | docid_idx       | docid       |    26652929 |
#         | uploads_document | document_free_i | free_import |           2 |
#         | uploads_document | lastdateindex   | lastdate    |    26652929 |
#         | uploads_document | modifiedindex   | modified    |     6663232 |
#         +------------------+-----------------+-------------+-------------+
# 
#     (https://dev.mysql.com/doc/refman/5.0/en/show-index.html)
# 
#     """
#     court = models.CharField(
#         maxlength=10,
#     )
#     casenum = models.CharField(
#         maxlength=30,
#     )
#     docnum = models.CharField(
#         maxlength=30,
#     )
#     subdocnum = models.CharField(
#         maxlength=30,
#     )
#     docid = models.CharField(
#         maxlength=32,
#         null=True,
#         blank=True,
#         db_index=True,
#     )
#     de_seq_num = models.PositiveIntegerField(
#         null=True,
#         blank=True,
#     )
#     dm_id = models.PositiveIntegerField(
#         null=True,
#         blank=True,
#     )
#     sha1 = models.CharField(
#         maxlength=40,
#         null=True,
#         blank=True,
#     )
#     available = models.BooleanField(
#         default=0,
#     )
#     lastdate = models.DateTimeField(
#         null=True,
#         blank=True,
#         db_index=True,
#     )
#     modified = models.DateTimeField(
#         null=True,
#         blank=True,
#         auto_now_add=True,
#         db_index=True,
#     )
#     free_import = models.BooleanField(
#         default=0,
#         db_index=True,
#     )
#     team_name = models.CharField(
#         maxlength=40,
#         null=True,
#         blank=True
#     )
# 
#     class Meta:
#         unique_together = (
#             (
#                 "court",
#                 "casenum",
#                 "docnum",
#                 "subdocnum",
#             ),
#         )
# 
# class Uploader(models.Model):
#     # implicit 'id' IntegerField being used.
#     key = models.CharField(
#         maxlength=8,
#     )
#     name = models.CharField(
#         maxlength=64,
#     )
# 
# class BucketLock(models.Model):
#     court = models.CharField(
#         maxlength=10,
#     )
#     casenum = models.CharField(
#         maxlength=30,
#     )
#     uploaderid = models.IntegerField()
#     nonce = models.CharField(
#         maxlength=6,
#     )
#     ready = models.BooleanField(
#         default=0,
#     )
#     processing = models.BooleanField(
#         default=0,
#     )
#     locktime = models.DateTimeField(
#         auto_now_add=True,
#     )
# 
#     class Meta:
#         unique_together = (
#             (
#                 "court",
#                 "casenum",
#             ),
#         )
# 
#     def __unicode__(self):
#         return str(self.court) + "." + str(self.casenum)
# 
# class PickledPut(models.Model):
#     filename = models.CharField(
#         maxlength=128,
#         primary_key=True,
#     )
#     docket = models.BooleanField(
#         default=0,
#     )
#     ready = models.BooleanField(
#         default=0,
#     )
#     processing = models.BooleanField(
#         default=0,
#     )


from uploads.BeautifulSoup import BeautifulSoup,HTMLParseError, Tag, NavigableString
from mock import patch, Mock
from django.test.client import Client
from django.utils import simplejson
from uploads.models import Document, Uploader, BucketLock, PickledPut
from test_constants import TEST_SUPPORT_PATH, TEST_OPINION_PATH, TEST_DOC1_PATH
import uploads.ParsePacer as PP
import uploads.DocketXML as DocketXML
import os
import re
import uploads.InternetArchiveCommon as IACommon
import uploads.InternetArchive as IA
import unittest
import datetime
import time
    def test_valid_query_cases_response_unavailable_doc_currently_uploading(self):
        self.available_doc.available = 0
        self.available_doc.save()
        ppquery = PickledPut(filename=IACommon.get_docketxml_name('nysd', '1234'))
        ppquery.save()
        response = self.client.post('/recap/query_cases/', {'json': simplejson.dumps(self.valid_params)})
        self.assertEquals("{}", response.content)
        PickledPut.objects.all().delete()

    def test_valid_query_cases_response_old_doc_currently_uploading(self):
        self.available_doc.available = 0
        two_days_ago = datetime.datetime.now() - datetime.timedelta(2)
        self.available_doc.modified= two_days_ago
        self.available_doc.save()

        ppquery = PickledPut(filename=IACommon.get_docketxml_name('nysd', '1234'))
        ppquery.save()
        response = self.client.post('/recap/query_cases/', {'json': simplejson.dumps(self.valid_params)})

        output= simplejson.loads(response.content)

        self.assertEquals(2, len(output))
        self.assertEquals(self.available_doc.lastdate.strftime("%m/%d/%y"), output['timestamp'])
        self.assertEquals(IACommon.get_dockethtml_url('nysd', '1234'), output['docket_url'])
        PickledPut.objects.all().delete()


class TestQueryView(unittest.TestCase):
    def setUp(self):
        self.client = Client()

# Path: uploads/models.py
# class Document(models.Model):
#     """Note that a number of fields have indexes in the database despite
#     lacking them here:
# 
#     mysql> show index from uploads_document;
#         +------------------+-----------------+-------------+-------------+
#         | Table            | Key_name        | Column_name | Cardinality |
#         +------------------+-----------------+-------------+-------------+
#         | uploads_document | PRIMARY         | id          |    26652929 |
#         | uploads_document | court           | court       |         206 |
#         | uploads_document | court           | casenum     |     1025112 |
#         | uploads_document | court           | docnum      |    26652929 |
#         | uploads_document | court           | subdocnum   |    26652929 |
#         | uploads_document | docid_idx       | docid       |    26652929 |
#         | uploads_document | document_free_i | free_import |           2 |
#         | uploads_document | lastdateindex   | lastdate    |    26652929 |
#         | uploads_document | modifiedindex   | modified    |     6663232 |
#         +------------------+-----------------+-------------+-------------+
# 
#     (https://dev.mysql.com/doc/refman/5.0/en/show-index.html)
# 
#     """
#     court = models.CharField(
#         maxlength=10,
#     )
#     casenum = models.CharField(
#         maxlength=30,
#     )
#     docnum = models.CharField(
#         maxlength=30,
#     )
#     subdocnum = models.CharField(
#         maxlength=30,
#     )
#     docid = models.CharField(
#         maxlength=32,
#         null=True,
#         blank=True,
#         db_index=True,
#     )
#     de_seq_num = models.PositiveIntegerField(
#         null=True,
#         blank=True,
#     )
#     dm_id = models.PositiveIntegerField(
#         null=True,
#         blank=True,
#     )
#     sha1 = models.CharField(
#         maxlength=40,
#         null=True,
#         blank=True,
#     )
#     available = models.BooleanField(
#         default=0,
#     )
#     lastdate = models.DateTimeField(
#         null=True,
#         blank=True,
#         db_index=True,
#     )
#     modified = models.DateTimeField(
#         null=True,
#         blank=True,
#         auto_now_add=True,
#         db_index=True,
#     )
#     free_import = models.BooleanField(
#         default=0,
#         db_index=True,
#     )
#     team_name = models.CharField(
#         maxlength=40,
#         null=True,
#         blank=True
#     )
# 
#     class Meta:
#         unique_together = (
#             (
#                 "court",
#                 "casenum",
#                 "docnum",
#                 "subdocnum",
#             ),
#         )
# 
# class Uploader(models.Model):
#     # implicit 'id' IntegerField being used.
#     key = models.CharField(
#         maxlength=8,
#     )
#     name = models.CharField(
#         maxlength=64,
#     )
# 
# class BucketLock(models.Model):
#     court = models.CharField(
#         maxlength=10,
#     )
#     casenum = models.CharField(
#         maxlength=30,
#     )
#     uploaderid = models.IntegerField()
#     nonce = models.CharField(
#         maxlength=6,
#     )
#     ready = models.BooleanField(
#         default=0,
#     )
#     processing = models.BooleanField(
#         default=0,
#     )
#     locktime = models.DateTimeField(
#         auto_now_add=True,
#     )
# 
#     class Meta:
#         unique_together = (
#             (
#                 "court",
#                 "casenum",
#             ),
#         )
# 
#     def __unicode__(self):
#         return str(self.court) + "." + str(self.casenum)
# 
# class PickledPut(models.Model):
#     filename = models.CharField(
#         maxlength=128,
#         primary_key=True,
#     )
#     docket = models.BooleanField(
#         default=0,
#     )
#     ready = models.BooleanField(
#         default=0,
#     )
#     processing = models.BooleanField(
#         default=0,
#     )


# Path: uploads/tests/alltests.py
from uploads.BeautifulSoup import BeautifulSoup,HTMLParseError, Tag, NavigableString
from mock import patch, Mock
from django.test.client import Client
from django.utils import simplejson
from uploads.models import Document, Uploader, BucketLock, PickledPut
from test_constants import TEST_SUPPORT_PATH, TEST_OPINION_PATH, TEST_DOC1_PATH
import uploads.ParsePacer as PP
import uploads.DocketXML as DocketXML
import os
import re
import uploads.InternetArchiveCommon as IACommon
import uploads.InternetArchive as IA
import unittest
import datetime
import time

    def test_valid_query_cases_response_unavailable_doc_currently_uploading(self):
        self.available_doc.available = 0
        self.available_doc.save()
        ppquery = PickledPut(filename=IACommon.get_docketxml_name('nysd', '1234'))
        ppquery.save()
        response = self.client.post('/recap/query_cases/', {'json': simplejson.dumps(self.valid_params)})
        self.assertEquals("{}", response.content)
        PickledPut.objects.all().delete()

    def test_valid_query_cases_response_old_doc_currently_uploading(self):
        self.available_doc.available = 0
        two_days_ago = datetime.datetime.now() - datetime.timedelta(2)
        self.available_doc.modified= two_days_ago
        self.available_doc.save()

        ppquery = PickledPut(filename=IACommon.get_docketxml_name('nysd', '1234'))
        ppquery.save()
        response = self.client.post('/recap/query_cases/', {'json': simplejson.dumps(self.valid_params)})

        output= simplejson.loads(response.content)

        self.assertEquals(2, len(output))
        self.assertEquals(self.available_doc.lastdate.strftime("%m/%d/%y"), output['timestamp'])
        self.assertEquals(IACommon.get_dockethtml_url('nysd', '1234'), output['docket_url'])
        PickledPut.objects.all().delete()


class TestQueryView(unittest.TestCase):
    def setUp(self):
        self.client = Client()

        self.not_avail_doc= Document(court='nysd', casenum='1234', docnum='1', subdocnum='0',