# Path: scraper/inmate_details.py
# class InmateDetails:
#     """
#     Handles the processing of the Inmate Detail information page on the
#     Cook County Jail website.
#     Presents a consistent named interface to the information
# 
#     Strips spurious whitespace from text content before returning them
# 
#     Dates are returned as datetime objects
#     """
# 
#     def __init__(self, html):
#         inmate_doc = pq(html)
#         self.__columns = inmate_doc('table tr:nth-child(2n) td')
# 
#     def age_at_booking(self):
#         """
#         Calculates the inmates age at the time of booking,
#         code taken from http://is.gd/ep7Thb
#         """
#         birth_date, booking_date = self.__birth_date(), self.booking_date()
#         if (birth_date.month <= booking_date.month and
#                 birth_date.day <= booking_date.day):
#             return booking_date.year - birth_date.year
#         return booking_date.year - birth_date.year - 1
# 
#     def bail_amount(self):
#         return self.__column_content(10)
# 
#     def __birth_date(self):
#         return self.__convert_datetime(2)
# 
#     def booking_date(self):
#         booking_date = self.__convert_date(7)
#         return booking_date if booking_date else self.__convert_jail_id_2_booking_date()
# 
#     def charges(self):
#         return self.__column_content(11)
# 
#     def __column_content(self, columns_index):
#         return self.__columns[columns_index].text_content().strip().replace(u'\xa0', u' ')
# 
#     def __convert_date(self, column_index):
#         result = self.__convert_datetime(column_index)
#         return result if result is None else result.date()
# 
#     def __convert_datetime(self, column_index):
#         try:
#             result = datetime.strptime(self.__column_content(column_index), '%m/%d/%Y')
#         except ValueError:
#             result = None
#         return result
# 
#     def __convert_jail_id_2_booking_date(self):
#         return datetime.strptime(self.jail_id()[0:9], '%Y-%m%d').date()
# 
#     def court_house_location(self):
#         return self.__column_content(13)
# 
#     def gender(self):
#         return self.__column_content(4)
# 
#     def hash_id(self):
#         id_string = "%s%s%s%s" % (
#             self.__name().replace(" ", ""),
#             self.__birth_date().strftime('%m%d%Y'),
#             self.race()[0],
#             self.gender(),
#         )
#         byte_string = id_string.encode('utf-8')
#         return hashlib.sha256(byte_string).hexdigest()
# 
#     def height(self):
#         return self.__column_content(5)
# 
#     def housing_location(self):
#         return self.__column_content(8)
# 
#     def jail_id(self):
#         return self.__column_content(0)
# 
#     def __name(self):
#         return self.__column_content(1)
# 
#     def next_court_date(self):
#         return self.__convert_datetime(12)
# 
#     def race(self):
#         return self.__column_content(3)
# 
#     def weight(self):
#         return self.__column_content(6)


from datetime import datetime, date
from scraper.inmate_details import InmateDetails
                             # conversion of &nbsp; value, u'\xa0' is converted to ordinary space

INMATE_2 = u'2014-1107234'  # Has missing booking date


class Test_InmateDetails:

    def setup_method(self, method):
        self.__inmates_html = {}
        for jail_id in [INMATE_1, INMATE_2]:
            if jail_id not in self.__inmates_html:
                with open("tests/data/%s.html" % jail_id, "r") as inmates_file:
                    self.__inmates_html[jail_id] = inmates_file.read()

    def test_inmate_details(self):
        inmate_details = InmateDetails(self.__inmates_html[INMATE_1])
        assert inmate_details.age_at_booking() == 52
        assert inmate_details.bail_amount() == u'20,000'
        assert inmate_details.charges() == u"625 ILCS 5 6-303(d) [5883000]\n\t  DRIVING REVOKED/SUSPENDED 2ND+"
        assert inmate_details.court_house_location() == MARKHAM_COURT_HOUSE_LOCATION
        assert inmate_details.gender() == u'M'
        assert inmate_details.hash_id() == u'10e8e7c4a10c26216c8567b66156937240875bd945702d0f003c97fce773f29b'
        assert inmate_details.height() == u'509'
        assert inmate_details.housing_location() == u'02-D2-T-3-T'
        assert inmate_details.jail_id() == INMATE_1
        assert inmate_details.next_court_date() == datetime(2014, 2, 7)
        assert inmate_details.race() == u'BK'
        assert inmate_details.weight() == u'195'

    def test_missing_booking_date(self):

# Path: scraper/inmate_details.py
# class InmateDetails:
#     """
#     Handles the processing of the Inmate Detail information page on the
#     Cook County Jail website.
#     Presents a consistent named interface to the information
# 
#     Strips spurious whitespace from text content before returning them
# 
#     Dates are returned as datetime objects
#     """
# 
#     def __init__(self, html):
#         inmate_doc = pq(html)
#         self.__columns = inmate_doc('table tr:nth-child(2n) td')
# 
#     def age_at_booking(self):
#         """
#         Calculates the inmates age at the time of booking,
#         code taken from http://is.gd/ep7Thb
#         """
#         birth_date, booking_date = self.__birth_date(), self.booking_date()
#         if (birth_date.month <= booking_date.month and
#                 birth_date.day <= booking_date.day):
#             return booking_date.year - birth_date.year
#         return booking_date.year - birth_date.year - 1
# 
#     def bail_amount(self):
#         return self.__column_content(10)
# 
#     def __birth_date(self):
#         return self.__convert_datetime(2)
# 
#     def booking_date(self):
#         booking_date = self.__convert_date(7)
#         return booking_date if booking_date else self.__convert_jail_id_2_booking_date()
# 
#     def charges(self):
#         return self.__column_content(11)
# 
#     def __column_content(self, columns_index):
#         return self.__columns[columns_index].text_content().strip().replace(u'\xa0', u' ')
# 
#     def __convert_date(self, column_index):
#         result = self.__convert_datetime(column_index)
#         return result if result is None else result.date()
# 
#     def __convert_datetime(self, column_index):
#         try:
#             result = datetime.strptime(self.__column_content(column_index), '%m/%d/%Y')
#         except ValueError:
#             result = None
#         return result
# 
#     def __convert_jail_id_2_booking_date(self):
#         return datetime.strptime(self.jail_id()[0:9], '%Y-%m%d').date()
# 
#     def court_house_location(self):
#         return self.__column_content(13)
# 
#     def gender(self):
#         return self.__column_content(4)
# 
#     def hash_id(self):
#         id_string = "%s%s%s%s" % (
#             self.__name().replace(" ", ""),
#             self.__birth_date().strftime('%m%d%Y'),
#             self.race()[0],
#             self.gender(),
#         )
#         byte_string = id_string.encode('utf-8')
#         return hashlib.sha256(byte_string).hexdigest()
# 
#     def height(self):
#         return self.__column_content(5)
# 
#     def housing_location(self):
#         return self.__column_content(8)
# 
#     def jail_id(self):
#         return self.__column_content(0)
# 
#     def __name(self):
#         return self.__column_content(1)
# 
#     def next_court_date(self):
#         return self.__convert_datetime(12)
# 
#     def race(self):
#         return self.__column_content(3)
# 
#     def weight(self):
#         return self.__column_content(6)


# Path: tests/scraper/test_inmate_details.py
from datetime import datetime, date
from scraper.inmate_details import InmateDetails

                             # conversion of &nbsp; value, u'\xa0' is converted to ordinary space

INMATE_2 = u'2014-1107234'  # Has missing booking date


class Test_InmateDetails:

    def setup_method(self, method):
        self.__inmates_html = {}
        for jail_id in [INMATE_1, INMATE_2]:
            if jail_id not in self.__inmates_html:
                with open("tests/data/%s.html" % jail_id, "r") as inmates_file:
                    self.__inmates_html[jail_id] = inmates_file.read()

    def test_inmate_details(self):
        inmate_details = InmateDetails(self.__inmates_html[INMATE_1])
        assert inmate_details.age_at_booking() == 52
        assert inmate_details.bail_amount() == u'20,000'
        assert inmate_details.charges() == u"625 ILCS 5 6-303(d) [5883000]\n\t  DRIVING REVOKED/SUSPENDED 2ND+"
        assert inmate_details.court_house_location() == MARKHAM_COURT_HOUSE_LOCATION
        assert inmate_details.gender() == u'M'
        assert inmate_details.hash_id() == u'10e8e7c4a10c26216c8567b66156937240875bd945702d0f003c97fce773f29b'
        assert inmate_details.height() == u'509'
        assert inmate_details.housing_location() == u'02-D2-T-3-T'
        assert inmate_details.jail_id() == INMATE_1
        assert inmate_details.next_court_date() == datetime(2014, 2, 7)
        assert inmate_details.race() == u'BK'
        assert inmate_details.weight() == u'195'

    def test_missing_booking_date(self):

        inmate_details = InmateDetails(self.__inmates_html[INMATE_2])