# Path: dashboard/apps/hub/models.py
# class State(models.Model):
#     """Top-level window into state data
# 
#     Intended as a resource to track general state information and
#     as a skeleton for admin inlines such as election data and FOIA logs
# 
#     """
#     STATUS_OPTIONS = (
#         ('not-started', 'Not Started'),
#         ('partial', 'Partial'),
#         ('up-to-date', 'Up-to-date'),
#     )
#     PAIN_CHOICES = (
#         ('easy', 'Easy'),
#         ('medium', 'Medium'),
#         ('hard', 'Hard'),
#         ('excruciating', 'Excruciating'),
#     )
#     postal = models.CharField(max_length=2, choices=US_STATES, primary_key=True)
#     name = models.CharField(max_length=25, help_text="Full state name")
#     metadata_status = models.CharField(max_length=20, choices=STATUS_OPTIONS, db_index=True, help_text="Status of metadata collection for state")
#     note = models.TextField("Overview", blank=True)
#     pain = models.CharField(max_length=15, blank=True, choices=PAIN_CHOICES, default='', help_text="Degree of difficulty for loading a state's results.")
#     results_description = models.TextField(blank=True, help_text="Quality and consistency of results over time. E.g., CSV files with consistent formats for all years except 2000 and 2002")
# 
#     objects = StateManager()
# 
#     class Meta:
#         ordering = ['name']
# 
#     def __str__(self):
#         return '%s' % self.name
# 
#     def status_entry(self):
#         """
#         Returns a dict, suitable for serialization that represents
#         the state's completion status.
#         """
#         volunteers = [v.status_entry() for v in
#                       self.volunteer_set.all()]
#         dev_volunteers = [v.status_entry() for v in
#             self.volunteer_set.filter(roles__slug="dev")]
#         metadata_volunteers = [v.status_entry() for v in
#             self.volunteer_set.filter(roles__slug="metadata")]
# 
#         return {
#             'name': self.name,
#             'postal': self.postal,
#             'metadata_status': self.metadata_status,
#             'results_status': self.results_status,
#             'volunteers': volunteers,
#             'dev_volunteers': dev_volunteers,
#             'metadata_volunteers': metadata_volunteers,
#         }
# 
#     @property
#     def results_status(self):
#         """
#         The status of results for this state.
# 
#         The value can be one of these:
# 
#         None: Unknown status.
#         "partial": A developer has been assigned to this state.
#         "raw": Raw results available for at least some elections.
#         "clean": Cleaned/transformed results available for at least some
#             elections.
#         """
#         statuses = (
#             {
#                 'in': 'baked',
#                 'out': 'clean',
#             },
#             {
#                 'in': 'baked-raw',
#                 'out': 'raw',
#             },
#         )
#         final_status = None
#         # Check if we have any clean or raw results
#         for status in statuses:
#             q = (Q(precinct_level_status=status['in']) | Q(county_level_status=status['in']) |
#                 Q(cong_dist_level_status=status['in']) | Q(state_leg_level_status=status['in']) |
#                 Q(state_level_status=status['in']))
# 
#             if self.election_set.filter(q).exists():
#                 final_status = status['out']
#                 break
# 
#         # No clean or raw results, see if a developer volunteer has been
#         # assigned.
#         if (final_status is None and
#                 self.volunteer_set.filter(roles__slug='dev').exists()):
#             final_status = 'partial'
# 
#         return final_status


import json
from django.test import TestCase
from ..models import State 



class TestStateManager(TestCase):
    fixtures = [
        'test_state_status',
    ]

    def test_status_json(self):
        status_json = State.objects.status_json() 
        statuses = json.loads(status_json)
        self.assertEqual(len(statuses), 2)
        status = statuses[0]
        self.assertEqual(status['postal'], "IL")
        self.assertEqual(status['name'], "Illinois")
        self.assertEqual(status['metadata_status'], "partial")

# Path: dashboard/apps/hub/models.py
# class State(models.Model):
#     """Top-level window into state data
# 
#     Intended as a resource to track general state information and
#     as a skeleton for admin inlines such as election data and FOIA logs
# 
#     """
#     STATUS_OPTIONS = (
#         ('not-started', 'Not Started'),
#         ('partial', 'Partial'),
#         ('up-to-date', 'Up-to-date'),
#     )
#     PAIN_CHOICES = (
#         ('easy', 'Easy'),
#         ('medium', 'Medium'),
#         ('hard', 'Hard'),
#         ('excruciating', 'Excruciating'),
#     )
#     postal = models.CharField(max_length=2, choices=US_STATES, primary_key=True)
#     name = models.CharField(max_length=25, help_text="Full state name")
#     metadata_status = models.CharField(max_length=20, choices=STATUS_OPTIONS, db_index=True, help_text="Status of metadata collection for state")
#     note = models.TextField("Overview", blank=True)
#     pain = models.CharField(max_length=15, blank=True, choices=PAIN_CHOICES, default='', help_text="Degree of difficulty for loading a state's results.")
#     results_description = models.TextField(blank=True, help_text="Quality and consistency of results over time. E.g., CSV files with consistent formats for all years except 2000 and 2002")
# 
#     objects = StateManager()
# 
#     class Meta:
#         ordering = ['name']
# 
#     def __str__(self):
#         return '%s' % self.name
# 
#     def status_entry(self):
#         """
#         Returns a dict, suitable for serialization that represents
#         the state's completion status.
#         """
#         volunteers = [v.status_entry() for v in
#                       self.volunteer_set.all()]
#         dev_volunteers = [v.status_entry() for v in
#             self.volunteer_set.filter(roles__slug="dev")]
#         metadata_volunteers = [v.status_entry() for v in
#             self.volunteer_set.filter(roles__slug="metadata")]
# 
#         return {
#             'name': self.name,
#             'postal': self.postal,
#             'metadata_status': self.metadata_status,
#             'results_status': self.results_status,
#             'volunteers': volunteers,
#             'dev_volunteers': dev_volunteers,
#             'metadata_volunteers': metadata_volunteers,
#         }
# 
#     @property
#     def results_status(self):
#         """
#         The status of results for this state.
# 
#         The value can be one of these:
# 
#         None: Unknown status.
#         "partial": A developer has been assigned to this state.
#         "raw": Raw results available for at least some elections.
#         "clean": Cleaned/transformed results available for at least some
#             elections.
#         """
#         statuses = (
#             {
#                 'in': 'baked',
#                 'out': 'clean',
#             },
#             {
#                 'in': 'baked-raw',
#                 'out': 'raw',
#             },
#         )
#         final_status = None
#         # Check if we have any clean or raw results
#         for status in statuses:
#             q = (Q(precinct_level_status=status['in']) | Q(county_level_status=status['in']) |
#                 Q(cong_dist_level_status=status['in']) | Q(state_leg_level_status=status['in']) |
#                 Q(state_level_status=status['in']))
# 
#             if self.election_set.filter(q).exists():
#                 final_status = status['out']
#                 break
# 
#         # No clean or raw results, see if a developer volunteer has been
#         # assigned.
#         if (final_status is None and
#                 self.volunteer_set.filter(roles__slug='dev').exists()):
#             final_status = 'partial'
# 
#         return final_status


# Path: dashboard/apps/hub/tests/test_managers.py
import json
from django.test import TestCase
from ..models import State 




class TestStateManager(TestCase):
    fixtures = [
        'test_state_status',
    ]

    def test_status_json(self):
        status_json = State.objects.status_json() 
        statuses = json.loads(status_json)
        self.assertEqual(len(statuses), 2)
        status = statuses[0]
        self.assertEqual(status['postal'], "IL")
        self.assertEqual(status['name'], "Illinois")
        self.assertEqual(status['metadata_status'], "partial")

        self.assertEqual(len(status['volunteers']), 1)