<s> from collections import defaultdict <EOL> from datetime import datetime , date <EOL> from unittest import TestCase <EOL> from dimagi . ext . jsonobject import ( JsonObject , DateTimeProperty , <EOL> StringProperty , IntegerProperty , BooleanProperty ) <EOL> from casexml . apps . case . models import CommCareCase <EOL> from couchforms . models import XFormInstance <EOL> from dimagi . utils . dates import DateSpan , add_months <EOL> from . . beneficiary import OPMCaseRow <EOL> from . . import constants <EOL> from . . reports import CaseReportMixin , SharedDataProvider <EOL> class AggressiveDefaultDict ( defaultdict ) : <EOL> """<STR_LIT>""" <EOL> def __contains__ ( self , item ) : <EOL> return True <EOL> def get ( self , key , default = None ) : <EOL> return self [ key ] <EOL> class MockDataProvider ( SharedDataProvider ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , default_date = None , explicit_map = None ) : <EOL> super ( MockDataProvider , self ) . __init__ ( ) <EOL> if explicit_map is not None : <EOL> results = defaultdict ( lambda : defaultdict ( lambda : set ( ) ) ) <EOL> for owner , availability in explicit_map . items ( ) : <EOL> for prop , dates in availability . items ( ) : <EOL> results [ owner ] [ prop ] . update ( dates ) <EOL> self . service_map = results <EOL> else : <EOL> get_default_set = lambda : { default_date } if default_date is not None else set ( ) <EOL> def get_date_set_dict ( ) : <EOL> return AggressiveDefaultDict ( get_default_set ) <EOL> self . service_map = AggressiveDefaultDict ( get_date_set_dict ) <EOL> @ property <EOL> def _service_dates ( self ) : <EOL> return self . service_map <EOL> @ property <EOL> def forms_by_case ( self ) : <EOL> assert getattr ( self , '<STR_LIT>' , None ) is not None , "<STR_LIT>" <EOL> return { case . _id : case . get_forms ( ) for case in self . cases } <EOL> class Report ( CaseReportMixin , JsonObject ) : <EOL> month = IntegerProperty ( required = True ) <EOL> year = IntegerProperty ( required = True ) <EOL> block = StringProperty ( required = True ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Report , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _extra_row_objects = [ ] <EOL> _data_provider = None <EOL> @ property <EOL> def data_provider ( self ) : <EOL> return self . _data_provider <EOL> @ property <EOL> def datespan ( self ) : <EOL> return DateSpan . from_month ( self . month , self . year , inclusive = True ) <EOL> def set_extra_row_objects ( self , row_objects ) : <EOL> self . _extra_row_objects = row_objects <EOL> class OPMCase ( CommCareCase ) : <EOL> opened_on = DateTimeProperty ( datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> block_name = StringProperty ( "<STR_LIT>" ) <EOL> type = StringProperty ( "<STR_LIT>" ) <EOL> closed = BooleanProperty ( default = False ) <EOL> closed_on = DateTimeProperty ( ) <EOL> awc_name = StringProperty ( "<STR_LIT>" ) <EOL> owner_id = StringProperty ( "<STR_LIT>" ) <EOL> def __init__ ( self , forms = None , ** kwargs ) : <EOL> super ( OPMCase , self ) . __init__ ( ** kwargs ) <EOL> self . _id = "<STR_LIT>" <EOL> self . _fake_forms = forms if forms is not None else [ ] <EOL> def get_forms ( self ) : <EOL> return self . _fake_forms <EOL> class Meta : <EOL> app_label = "<STR_LIT>" <EOL> class MockCaseRow ( OPMCaseRow ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , case , report , data_provider = None , child_index = <NUM_LIT:1> , ** kwargs ) : <EOL> self . case = case <EOL> self . report = report <EOL> self . report . is_rendered_as_email = None <EOL> self . report . _data_provider = data_provider or MockDataProvider ( report . datespan . enddate . date ( ) ) <EOL> self . report . _data_provider . cases = [ case ] <EOL> super ( MockCaseRow , self ) . __init__ ( case , report , child_index = child_index , ** kwargs ) <EOL> class OPMCaseReportTestBase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . report_date = date ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:1> ) <EOL> self . report_datetime = datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:1> ) <EOL> self . report = Report ( month = <NUM_LIT:6> , year = <NUM_LIT> , block = "<STR_LIT>" ) <EOL> def get_relative_edd_from_preg_month ( report_date , month ) : <EOL> months_until_edd = <NUM_LIT:9> - month <EOL> new_year , new_month = add_months ( report_date . year , report_date . month , months_until_edd ) <EOL> return type ( report_date ) ( new_year , new_month , <NUM_LIT:1> ) <EOL> def offset_date ( reference_date , offset ) : <EOL> new_year , new_month = add_months ( reference_date . year , reference_date . month , offset ) <EOL> return type ( reference_date ) ( new_year , new_month , <NUM_LIT:1> ) <EOL> class MockDataTest ( OPMCaseReportTestBase ) : <EOL> def test_mock_data ( self ) : <EOL> report = Report ( month = <NUM_LIT:6> , year = <NUM_LIT> , block = "<STR_LIT>" ) <EOL> form = XFormInstance ( form = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , received_on = datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:15> ) ) <EOL> case = OPMCase ( <EOL> forms = [ form ] , <EOL> edd = date ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT:10> ) , <EOL> ) <EOL> row = MockCaseRow ( case , report ) <EOL> def make_case_row ( form_props = None , vhnd_props = None , block = '<STR_LIT>' , child_age = <NUM_LIT:6> ) : <EOL> """<STR_LIT>""" <EOL> report_year = <NUM_LIT> <EOL> report_month = <NUM_LIT:6> <EOL> date_in_month = datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:10> ) <EOL> child_age = child_age <EOL> owner_id = '<STR_LIT>' <EOL> forms = [ XFormInstance ( <EOL> form = { '<STR_LIT>' : { prop : '<STR_LIT:1>' for prop in form_props or [ ] } } , <EOL> received_on = date_in_month , <EOL> xmlns = constants . CFU1_XMLNS , <EOL> ) ] <EOL> dod_year , dod_month = add_months ( report_year , report_month , - child_age ) <EOL> case = OPMCase ( <EOL> forms = forms or [ ] , <EOL> dod = date ( dod_year , dod_month , <NUM_LIT:10> ) , <EOL> owner_id = owner_id , <EOL> ) <EOL> data_provider = MockDataProvider ( explicit_map = { <EOL> owner_id : { <EOL> prop : { date_in_month . date ( ) } <EOL> for prop in vhnd_props or [ ] <EOL> } <EOL> } ) <EOL> report = Report ( <EOL> month = report_month , <EOL> year = report_year , <EOL> block = block , <EOL> ) <EOL> row = MockCaseRow ( case , report , data_provider = data_provider ) <EOL> assert row . child_age == child_age <EOL> return row </s>
