def test_wrap_action_config(self): # action_type is an old alias for action self.assertEqual( Command._wrap_action_config({ 'action_type': 'receipts', 'subaction': 'some subaction', '_keyword': 'some word', 'caption': 'some caption', }), { 'action': 'receipts', 'subaction': 'some subaction', '_keyword': 'some word', 'caption': 'some caption', }) # actions named 'lost' get 'loss' subactions self.assertEqual( Command._wrap_action_config({ 'action': 'consume', 'name': 'lost', 'subaction': 'some subaction', '_keyword': 'some word', 'caption': 'some caption', }), { 'action': 'consume', 'subaction': 'loss', '_keyword': 'some word', 'caption': 'some caption', })
def test_wrap_stock_restore_config(self): # force_to_consumption_case_types is an old alias for force_consumption_case_types self.assertEqual( Command._wrap_stock_restore_config({ 'force_to_consumption_case_types': [1], 'force_consumption_case_types': [], 'section_to_consumption_types': {}, 'use_dynamic_product_list': True, }), { 'force_consumption_case_types': [1], 'section_to_consumption_types': {}, 'use_dynamic_product_list': True, }) # Don't overwrite if there's already a value in force_consumption_case_types self.assertEqual( Command._wrap_stock_restore_config({ 'force_to_consumption_case_types': [1], 'force_consumption_case_types': [2], 'section_to_consumption_types': {}, 'use_dynamic_product_list': True, }), { 'force_to_consumption_case_types': [1], 'force_consumption_case_types': [2], 'section_to_consumption_types': {}, 'use_dynamic_product_list': True, })
def _migration_sync_to_sql(self, sql_object): from corehq.apps.commtrack.management.commands.populate_commtrackconfig import Command for field_name in Command.attrs_to_sync(): value = getattr(self, field_name) setattr(sql_object, field_name, value) submodels = [] for spec in Command.one_to_one_submodels(): couch_submodel = getattr(self, spec['couch_attr'], {}) sql_submodel = spec['sql_class'](**{ field: getattr(couch_submodel, field) for field in spec['fields'] }) setattr(sql_object, spec['sql_class'].__name__.lower(), sql_submodel) submodels.append(sql_submodel) # Make sure model has id so that submodels can be saved if not sql_object.id: sql_object.save(sync_to_couch=False) for sql_submodel in submodels: setattr(sql_submodel, 'commtrack_config', sql_object) sql_submodel.save() sql_object.set_actions([ SQLActionConfig( action=action.action, subaction=action.subaction, _keyword=action._keyword, caption=action.caption, ) for action in self.actions ]) sql_object.save(sync_to_couch=False)
def test_diff_action_attributes(self): couch = self._create_unsynced_couch().to_json() sql = self._create_unsynced_sql() couch['actions'][0]['subaction'] = 'other-subaction' couch['actions'][1]['_keyword'] = 'dos' self.assertEqual(Command.diff_couch_and_sql(couch, sql), "\n".join([ "subaction: couch value 'other-subaction' != sql value 'sub-receipts'", "_keyword: couch value 'dos' != sql value 'two'", ]))
def test_diff_top_level_attributes(self): couch = self._create_unsynced_couch().to_json() sql = self._create_unsynced_sql() couch['domain'] = 'other_project' couch['use_auto_emergency_levels'] = True self.assertEqual(Command.diff_couch_and_sql(couch, sql), "\n".join([ "domain: couch value 'other_project' != sql value 'my_project'", "use_auto_emergency_levels: couch value True != sql value False", ]))
def test_diff_remove_submodel(self): couch = self._create_unsynced_couch().to_json() sql = self._create_unsynced_sql() couch.pop('alert_config') self.assertEqual(Command.diff_couch_and_sql(couch, sql), "\n".join([ "stock_out_facilities: couch value None != sql value True", "stock_out_commodities: couch value None != sql value True", "stock_out_rates: couch value None != sql value True", "non_report: couch value None != sql value True", ]))
def test_diff_action_order(self): couch = self._create_unsynced_couch().to_json() sql = self._create_unsynced_sql() (action1, action2) = couch['actions'] couch['actions'] = [action2, action1] self.assertEqual(Command.diff_couch_and_sql(couch, sql), "\n".join([ "_keyword: couch value 'two' != sql value 'one'", "caption: couch value 'second action' != sql value 'first action'", "_keyword: couch value 'one' != sql value 'two'", "caption: couch value 'first action' != sql value 'second action'", ]))
def test_diff_submodel_attributes(self): couch = self._create_unsynced_couch().to_json() sql = self._create_unsynced_sql() couch['consumption_config']['min_window'] = 4 couch['ota_restore_config']['force_consumption_case_types'] = ['type2'] couch['ota_restore_config']['section_to_consumption_types'] = {'s1': 'c1', 's2': 'c2'} self.assertEqual(Command.diff_couch_and_sql(couch, sql), "\n".join([ "min_window: couch value 4 != sql value 2", "section_to_consumption_types: couch value {'s1': 'c1', 's2': 'c2'} != sql value {'s1': 'c1'}", "force_consumption_case_types: couch value ['type2'] != sql value ['type1']", ]))
def _migration_sync_to_couch(self, couch_object): from corehq.apps.commtrack.management.commands.populate_commtrackconfig import Command for field_name in Command.attrs_to_sync(): value = getattr(self, field_name) couch_object[field_name] = value couch_object.actions = [ CommtrackActionConfig( action=action.action, subaction=action.subaction, _keyword=action._keyword, caption=action.caption, ) for action in self.all_actions ] for spec in Command.one_to_one_submodels(): sql_submodel = getattr(self, spec['sql_class'].__name__.lower()) couch_object[spec['couch_attr']] = spec['couch_class']({ field: getattr(sql_submodel, field) for field in spec['fields'] }) sql_submodel.save() couch_object.save(sync_to_sql=False)
def test_diff_identical(self): couch = self._create_unsynced_couch().to_json() sql = self._create_unsynced_sql() self.assertIsNone(Command.get_diff_as_string(couch, sql))
def test_make_domain_commtrack(self): domain_obj = create_domain("test-make-domain-commtrack") make_domain_commtrack(domain_obj) self.assertTrue(domain_obj.commtrack_enabled) self.assertTrue(domain_obj.locations_enabled) config = SQLCommtrackConfig.for_domain(domain_obj.name) self.assertEqual( config.to_json(), { 'domain': 'test-make-domain-commtrack', 'actions': [{ 'action': 'receipts', 'subaction': None, '_keyword': 'r', 'caption': 'Received', }, { 'action': 'consumption', 'subaction': None, '_keyword': 'c', 'caption': 'Consumed', }, { 'action': 'consumption', 'subaction': 'loss', '_keyword': 'l', 'caption': 'Losses', }, { 'action': 'stockonhand', 'subaction': None, '_keyword': 'soh', 'caption': 'Stock on hand', }, { 'action': 'stockout', 'subaction': None, '_keyword': 'so', 'caption': 'Stock-out', }], 'use_auto_emergency_levels': False, 'sync_consumption_fixtures': False, 'use_auto_consumption': False, 'individual_consumption_defaults': False, 'alert_config': { 'stock_out_facilities': False, 'stock_out_commodities': False, 'stock_out_rates': False, 'non_report': False, }, 'consumption_config': { 'min_transactions': 2, 'min_window': 10, 'optimal_window': None, 'use_supply_point_type_default_consumption': False, 'exclude_invalid_periods': False, }, 'ota_restore_config': { 'section_to_consumption_types': {}, 'force_consumption_case_types': [], 'use_dynamic_product_list': False, }, 'stock_levels_config': { 'emergency_level': 0.5, 'understock_threshold': 1.5, 'overstock_threshold': 3, } }) doc = CommtrackConfig.get_db().get(config.couch_id) self.assertIsNone(Command.get_diff_as_string(doc, config)) config.delete() domain_obj.delete()