def setUp(self): super(AllMetaResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.not_allowed_to_see_location = self.ts.create_arbitrary_location( self.lt.id)
def setUp(self): super(SyncODKResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Mars', location_name='Mars')
def setUp(self): super(SourceSubmissionResourceTest, self).setUp() self.ts = TestSetupHelpers() self.ts.load_some_metadata() ltr = LocationTreeCache() ltr.main() self.mapped_location_id = self.ts.locations[0].id self.loc_map = SourceObjectMap.objects.create( source_object_code='AF001039003000000000', content_type='location', mapped_by_id=self.ts.user.id, master_object_id=self.mapped_location_id) source_campaign_string = '2016 March NID OPV' self.mapped_campaign_id = self.ts.campaigns[0].id self.campaign_map = SourceObjectMap.objects.create( source_object_code=source_campaign_string, content_type='campaign', mapped_by_id=self.ts.user.id, master_object_id=self.mapped_campaign_id) self.mapped_indicator_with_data = self.ts.locations[2].id self.indicator_map = SourceObjectMap.objects.create( source_object_code='Percent missed due to other reasons', content_type='indicator', mapped_by_id=self.ts.user.id, master_object_id=self.mapped_indicator_with_data)
def setUp(self): super(CampaignDataPointResourceTest, self).setUp() # Create a user. self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username,\ '*****@*****.**', self.password) self.get_credentials() self.ts = TestSetupHelpers() ## create a metadata and data for us to use to test ## self.create_metadata() self.country_lt = LocationType.objects\ .create(name='Country',admin_level = 0) self.region_lt = LocationType.objects\ .create(name='Region',admin_level = 1) self.province_lt = LocationType.objects\ .create(name='Province',admin_level = 2) self.top_lvl_location = Location.objects.get(name='Nigeria') ltc = LocationTreeCache() ltc.main() LocationPermission.objects.create(user_id = self.user.id,\ top_lvl_location_id = self.top_lvl_location.id)
def setUp(self): super(UserGroupResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Nigeria', location_name='Nigeria')
def setUp(self): super(IndicatorTagResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location(self.lt.id)
def setUp(self): super(IndicatorToTagResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location(self.lt.id) self.ind_tag = IndicatorTag.objects.create(tag_name='a tag') self.ind = self.ts.create_arbitrary_indicator()
def setUp(self): ## instantiate the test client and all other methods ## super(DocDataPointResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Nigeria', location_name='Nigeria')
def setUp(self): super(IndicatorResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id) LocationPermission.objects.create(user_id=self.ts.user.id, top_lvl_location_id=self.top_lvl_location.id) self.ind = self.ts.create_arbitrary_indicator()
def setUp(self): super(GeoResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = LocationType.objects.create(name='Region', admin_level=1) self.distr, created = \ LocationType.objects.get_or_create(name='District',admin_level = 2) self.planet_location_type = LocationType.objects\ .create(name = 'Planet', admin_level = 0) self.ultimate_parent = Location.objects.create( id=1, name='Earth', location_code='Earth', location_type_id=self.planet_location_type.id) location_df_from_csv = read_csv( 'rhizome/tests/_data/locations_nimroz.csv') self.ts.model_df_to_data(location_df_from_csv, Location) # make sure that the proper level is set for the locs = Location.objects.filter(parent_location_id=6) for loc in locs: loc.location_type_id = self.distr.id loc.save() parent = Location.objects.get(id=6) parent.location_type_id = self.lt.id parent.save() geo_json_df = read_csv('rhizome/tests/_data/geo_json_small.txt', delimiter="|") location_df = DataFrame(list(Location.objects.all()\ .values_list('id','location_code')),columns=['location_id','location_code']) location_tree_df = DataFrame(list(Location.objects.all()\ .values_list('id','parent_location_id'))\ ,columns=['location_id','parent_location_id']) location_tree_df['parent_location_id'].fillna(self.ultimate_parent.id,\ inplace=True) location_tree_df['lvl'] = Series(1, index=location_tree_df.index) self.ts.model_df_to_data(location_tree_df, LocationTree) merged_df = location_df.merge(geo_json_df)[['location_id', 'geo_json']] self.ts.model_df_to_data(merged_df, LocationPolygon) minify_geo_json() LocationPermission.objects.create(user_id=self.ts.user.id, top_lvl_location_id=1)
class QueueProcessResourceTest(RhizomeApiTestCase): def setUp(self): super(QueueProcessResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Nigeria', location_name='Nigeria') # create a document, and one record for the soruce_submission # self.doc = Document.objects.create(doc_title='new doc') self.ss_obj = SourceSubmission.objects.create( document_id = self.doc.id, process_status = 'PROCESSED', row_number = 0 ) def test_get(self): ''' This method is meant to update the soruce_submission table so that it is queued up for re-processing. So to test that.. we create one source_submission, that says it is processed, then we hit the api, and query again ensuring that the processed_status = 'TO_PROCESS', that is.. that the API call did what it was supposed to. ''' data = { 'document_id': self.doc.id } resp = self.ts.get(self, '/api/v1/queue_process/', data=data) response_data = self.deserialize(resp) self.assertHttpOK(resp) new_ss_obj = SourceSubmission.objects.get(id = self.ss_obj.id) self.assertEqual(new_ss_obj.process_status, 'TO_PROCESS') def test_get_no_param(self): ''' We should get an error if we do not pass a document_id ''' resp = self.ts.get(self, '/api/v1/queue_process/') response_data = self.deserialize(resp) self.assertHttpApplicationError(resp) self.assertEqual(str(response_data['error'])\ ,'Missing required parameter document_id')
def setUp(self): super(DateDataPointResourceTest, self).setUp() # Create a user. self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username,\ '*****@*****.**', self.password) self.lt = LocationType.objects.create(name='Country', admin_level=0) self.province_lt = LocationType.objects.create(name='Province'\ ,admin_level = 1) self.district_lt = LocationType.objects.create(name='District'\ ,admin_level = 2) self.ind = Indicator.objects.create(name='Polio Cases', short_name='Polio Cases', data_format='date_int') self.top_lvl_location = Location.objects.create( name='Afghanistan', location_code='Afghanistan', id=1234, location_type_id=self.lt.id, ) self.some_province = Location.objects.create( name='Province', location_code='Province', id=432, parent_location_id=self.top_lvl_location.id, location_type_id=self.province_lt.id) self.some_district = Location.objects.create( name='Achin', location_code='Achin', id=4321, parent_location_id=self.some_province.id, location_type_id=self.district_lt.id) ltc = LocationTreeCache() ltc.main() LocationPermission.objects.create(user_id = self.user.id,\ top_lvl_location_id = self.top_lvl_location.id) self.get_credentials() self.create_polio_cases() self.ts = TestSetupHelpers() self.doc_id = Document.objects.create(doc_title='Data Entry').id
class CampaignTypeResourceTest(RhizomeApiTestCase): def setUp(self): super(CampaignTypeResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Nigeria', location_name='Nigeria') def test_get_campaign_type(self): CampaignType.objects.create(name="test") resp = self.ts.get(self, '/api/v1/campaign_type/') self.assertHttpOK(resp) resp_data = self.deserialize(resp) self.assertEqual(resp_data['objects'][0]['name'], 'test')
def setUp(self): super(DocTransformResourceTest, self).setUp() self.ts = TestSetupHelpers() self.ts.load_some_metadata() ltr = LocationTreeCache() ltr.main() self.mapped_location_id = self.ts.locations[0].id self.mapped_location_id_2 = self.ts.locations[1].id self.loc_map = SourceObjectMap.objects.create( source_object_code="AF001039003000000000", content_type="location", mapped_by_id=self.ts.user.id, master_object_id=self.mapped_location_id, ) source_campaign_string = "2016 March NID OPV" self.mapped_campaign_id = self.ts.campaigns[0].id self.campaign_map = SourceObjectMap.objects.create( source_object_code=source_campaign_string, content_type="campaign", mapped_by_id=self.ts.user.id, master_object_id=self.mapped_campaign_id, ) self.mapped_indicator_with_data = self.ts.locations[2].id self.indicator_map = SourceObjectMap.objects.create( source_object_code="Percent missed due to other reasons", content_type="indicator", mapped_by_id=self.ts.user.id, master_object_id=self.mapped_indicator_with_data, )
def setUp(self): super(SourceSubmissionResourceTest, self).setUp() self.ts = TestSetupHelpers() self.ts.load_some_metadata() ltr = LocationTreeCache() ltr.main() self.mapped_location_id = self.ts.locations[0].id self.loc_map = SourceObjectMap.objects.create( source_object_code='AF001039003000000000', content_type='location', mapped_by_id=self.ts.user.id, master_object_id=self.mapped_location_id ) source_campaign_string = '2016 March NID OPV' self.mapped_campaign_id = self.ts.campaigns[0].id self.campaign_map = SourceObjectMap.objects.create( source_object_code=source_campaign_string, content_type='campaign', mapped_by_id=self.ts.user.id, master_object_id=self.mapped_campaign_id ) self.mapped_indicator_with_data = self.ts.locations[2].id self.indicator_map = SourceObjectMap.objects.create( source_object_code='Percent missed due to other reasons', content_type='indicator', mapped_by_id=self.ts.user.id, master_object_id=self.mapped_indicator_with_data )
def setUp(self): super(DocDetailResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Nigeria', location_name='Nigeria')
def setUp(self): super(SyncODKResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code="Mars", location_name="Mars" )
def setUp(self): super(QueueProcessResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Nigeria', location_name='Nigeria') # create a document, and one record for the soruce_submission # self.doc = Document.objects.create(doc_title='new doc') self.ss_obj = SourceSubmission.objects.create( document_id = self.doc.id, process_status = 'PROCESSED', row_number = 0 )
class GeoResourceTest(RhizomeApiTestCase): def setUp(self): super(GeoResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = LocationType.objects.create(name='Region',admin_level=1) self.distr, created = \ LocationType.objects.get_or_create(name='District',admin_level = 2) self.planet_location_type = LocationType.objects\ .create(name = 'Planet', admin_level = 0) self.ultimate_parent = Location.objects.create( id = 1, name = 'Earth', location_code = 'Earth', location_type_id = self.planet_location_type.id ) location_df_from_csv= read_csv('rhizome/tests/_data/locations_nimroz.csv') self.ts.model_df_to_data(location_df_from_csv,Location) # make sure that the proper level is set for the locs = Location.objects.filter(parent_location_id=6) for loc in locs: loc.location_type_id = self.distr.id loc.save() parent = Location.objects.get(id=6) parent.location_type_id = self.lt.id parent.save() geo_json_df = read_csv('rhizome/tests/_data/geo_json_small.txt',delimiter = "|") location_df = DataFrame(list(Location.objects.all()\ .values_list('id','location_code')),columns=['location_id','location_code']) location_tree_df = DataFrame(list(Location.objects.all()\ .values_list('id','parent_location_id'))\ ,columns=['location_id','parent_location_id']) location_tree_df['parent_location_id'].fillna(self.ultimate_parent.id,\ inplace=True) location_tree_df['lvl'] = Series(1, index=location_tree_df.index) self.ts.model_df_to_data(location_tree_df, LocationTree) merged_df = location_df.merge(geo_json_df)[['location_id','geo_json']] self.ts.model_df_to_data(merged_df, LocationPolygon) minify_geo_json() LocationPermission.objects.create(user_id = self.ts.user.id, top_lvl_location_id = 1) def test_get_geo(self): get_data ={'location_id':6, 'location_depth':1} resp = self.ts.get(self, '/api/v1/geo/', get_data) self.assertHttpOK(resp) response_data = self.deserialize(resp) response_objects = response_data['objects'] self.assertEqual(len(response_objects), 5)
def setUp(self): super(IndicatorTagResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id)
def setUp(self): super(IndicatorToTagResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id) self.ind_tag = IndicatorTag.objects.create(tag_name='a tag') self.ind = self.ts.create_arbitrary_indicator()
class DocDetailTypeResourceTest(RhizomeApiTestCase): def setUp(self): ## instantiate the test client and all other methods ## super(DocDetailTypeResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Nigeria', location_name='Nigeria') def test_doc_detail_type(self): ddt = DocDetailType.objects.create(name="ddt") ddt_2 = DocDetailType.objects.create(name="ddt2") url = '/api/v1/doc_detail_type/' resp = self.ts.get(self, url) response_data = self.deserialize(resp) self.assertHttpOK(resp) self.assertEqual(len(response_data['objects']), 2)
class CalculatedIndicatorResourceTest(RhizomeApiTestCase): def setUp(self): super(CalculatedIndicatorResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Nigeria', location_name='Nigeria') # this is a pretty dumb test but it achieves 100% test coverage def test_indicator_id_missing(self): data = {} resp = self.ts.get(self, '/api/v1/indicator_calculation/', data=data) self.assertHttpOK(resp) resp_data = self.deserialize(resp) self.assertEqual(len(resp_data['objects']), 0)
def setUp(self): ## instantiate the test client and all other methods ## super(DocDetailTypeResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code="Nigeria", location_name="Nigeria" )
class SyncODKResourceTest(RhizomeApiTestCase): def setUp(self): super(SyncODKResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code="Mars", location_name="Mars" ) def test_get(self): """ This test only covers exceptions as this needs a .jar file on the server to execute. The ODK process that this tests is based on an interface between our app and ODK aggregate on ODK app engine. At the time we were writing this, the ODK backend that we had access to was run on google cloud, but there was no REST api to access that information like there is for other ODK providers like ONA. So, this api call is here as a skeleton for other ODK integration in which you would replace the jar file process n the `sync_odk` module, with a GET request for instance to the ONA server holding the ODK form data that you need. """ ## without a form_id, we can't do anything ## resp = self.ts.get(self, "/api/v1/sync_odk/") response_data = self.deserialize(resp) self.assertHttpApplicationError(resp) expected_error_message = "Missing required parameter odk_form_id" self.assertEqual(expected_error_message, str(response_data["error"])) ## without a document_id, we can still proceed, but we need to ensure ## that after we run this method, that we can find the document ## id for what we created filters = {"odk_form_id": "an_odk_form_from_a_server"} resp = self.ts.get(self, "/api/v1/sync_odk/", data=filters) response_data = self.deserialize(resp) self.assertHttpOK(resp)
class LocationTypeResourceTest(RhizomeApiTestCase): def setUp(self): ## instantiate the test client and all other methods ## super(LocationTypeResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Nigeria', location_name='Nigeria') def test_location_type(self): lt = LocationType.objects.create(name="lt", admin_level=1) lt_2 = LocationType.objects.create(name="lt2", admin_level=2) url = '/api/v1/location_type/' resp = self.ts.get(self, url) response_data = self.deserialize(resp) self.assertHttpOK(resp) # the two we just created, plus one created in setup self.assertEqual(len(response_data['objects']), 3)
def setUp(self): super(DateDataPointResourceTest, self).setUp() # Create a user. self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username,\ '*****@*****.**', self.password) self.lt = LocationType.objects.create(name='Country',admin_level = 0) self.province_lt = LocationType.objects.create(name='Province'\ ,admin_level = 1) self.district_lt = LocationType.objects.create(name='District'\ ,admin_level = 2) self.ind = Indicator.objects.create( name = 'Polio Cases', short_name = 'Polio Cases', data_format = 'date_int' ) self.top_lvl_location = Location.objects.create( name = 'Afghanistan', location_code = 'Afghanistan', id=1234, location_type_id = self.lt.id, ) self.some_province = Location.objects.create( name = 'Province', location_code = 'Province', id=432, parent_location_id = self.top_lvl_location.id, location_type_id = self.province_lt.id ) self.some_district = Location.objects.create( name = 'Achin', location_code = 'Achin', id=4321, parent_location_id = self.some_province.id, location_type_id = self.district_lt.id ) ltc = LocationTreeCache() ltc.main() LocationPermission.objects.create(user_id = self.user.id,\ top_lvl_location_id = self.top_lvl_location.id) self.get_credentials() self.create_polio_cases() self.ts = TestSetupHelpers() self.doc_id = Document.objects.create(doc_title='Data Entry').id
class DocDetailTypeResourceTest(RhizomeApiTestCase): def setUp(self): ## instantiate the test client and all other methods ## super(DocDetailTypeResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code="Nigeria", location_name="Nigeria" ) def test_doc_detail_type(self): ddt = DocDetailType.objects.create(name="ddt") ddt_2 = DocDetailType.objects.create(name="ddt2") url = "/api/v1/doc_detail_type/" resp = self.ts.get(self, url) response_data = self.deserialize(resp) self.assertHttpOK(resp) self.assertEqual(len(response_data["objects"]), 2)
class SyncODKResourceTest(RhizomeApiTestCase): def setUp(self): super(SyncODKResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Mars', location_name='Mars') def test_get(self): ''' This test only covers exceptions as this needs a .jar file on the server to execute. The ODK process that this tests is based on an interface between our app and ODK aggregate on ODK app engine. At the time we were writing this, the ODK backend that we had access to was run on google cloud, but there was no REST api to access that information like there is for other ODK providers like ONA. So, this api call is here as a skeleton for other ODK integration in which you would replace the jar file process n the `sync_odk` module, with a GET request for instance to the ONA server holding the ODK form data that you need. ''' ## without a form_id, we can't do anything ## resp = self.ts.get(self, '/api/v1/sync_odk/') response_data = self.deserialize(resp) self.assertHttpApplicationError(resp) expected_error_message = 'Missing required parameter odk_form_id' self.assertEqual(expected_error_message, str(response_data['error'])) ## without a document_id, we can still proceed, but we need to ensure ## that after we run this method, that we can find the document ## id for what we created filters = {'odk_form_id': 'an_odk_form_from_a_server'} resp = self.ts.get(self, '/api/v1/sync_odk/', data=filters) response_data = self.deserialize(resp) self.assertHttpOK(resp)
class LocationTypeResourceTest(RhizomeApiTestCase): def setUp(self): ## instantiate the test client and all other methods ## super(LocationTypeResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code="Nigeria", location_name="Nigeria" ) def test_location_type(self): lt = LocationType.objects.create(name="lt", admin_level=1) lt_2 = LocationType.objects.create(name="lt2", admin_level=2) url = "/api/v1/location_type/" resp = self.ts.get(self, url) response_data = self.deserialize(resp) self.assertHttpOK(resp) # the two we just created, plus one created in setup self.assertEqual(len(response_data["objects"]), 3)
def setUp(self): self.ts = TestSetupHelpers() data_df = read_csv('rhizome/tests/_data/calc_data.csv') self.create_metadata() self.user = User.objects.get(username="******") self.test_df = data_df[data_df['is_raw'] == 1] self.target_df = data_df[data_df['is_raw'] == 0] self.campaign_id = Campaign.objects.all()[0].id self.top_lvl_location = Location.objects.filter(name='Nigeria')[0] ltr = LocationTreeCache() ltr.main()
class CacheMetaResourceTest(RhizomeApiTestCase): def setUp(self): super(CacheMetaResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='The Solar System', location_name='The Solar System' ) self.leaf_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Mars', location_name='Mars', parent_location_id = self.top_lvl_location.id ) def test_get(self): ''' This url should re-build the location_tree, so if i truncate that table, truncate the location table, and add two locations, they should show up in the locatin tree table after the url is hit. ''' LocationTree.objects.all().delete() resp = self.ts.get(self, '/api/v1/cache_meta/') self.assertHttpOK(resp) leaf_location_tree_data = LocationTree.objects\ .filter(location_id = self.leaf_location).values() ## there shoul be one record where the leaf has a parent of itself ## with lvl = 0, and one where the parent is the top_lvl_location ## and the lvl = 1 self.assertEqual(len(leaf_location_tree_data), 2)
class UserGroupResourceTest(RhizomeApiTestCase): def setUp(self): super(UserGroupResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Nigeria', location_name='Nigeria') def test_get(self): user = User.objects.create(username='******') group = Group.objects.create(name="Sam's Group") user_group = UserGroup.objects.create(user=user, group=group) data = {'user_id': user.id} resp = self.ts.get(self, '/api/v1/user_group/', data) response_data = self.deserialize(resp) self.assertHttpOK(resp) self.assertEqual(len(response_data['objects']), 1) resp_obj = response_data['objects'][0] self.assertEqual(resp_obj['group_id'], group.id) self.assertEqual(resp_obj['user_id'], user.id) self.assertEqual(resp_obj['id'], user_group.id) def test_get_all(self): user = User.objects.create(username='******') group = Group.objects.create(name="Sam's Group") user_group_1 = UserGroup.objects.create(user=user, group=group) user_group_2 = UserGroup.objects.create(user=self.ts.user, group=group) resp = self.ts.get(self, '/api/v1/user_group/') response_data = self.deserialize(resp) self.assertHttpOK(resp) self.assertEqual(len(response_data['objects']), UserGroup.objects.count()) def test_create(self): user = User.objects.create(username='******') group = Group.objects.create(name="Sam's Group") self.assertEqual(UserGroup.objects.count(), 0) data = {'user_id': user.id, 'group_id': group.id} resp = self.ts.post(self, '/api/v1/user_group/', data) self.assertHttpCreated(resp) self.assertEqual(UserGroup.objects.count(), 1) response_data = self.deserialize(resp) self.assertEqual(response_data['user_id'], user.id) self.assertEqual(response_data['group_id'], group.id) def test_obj_delete(self): user = User.objects.create(username='******') group = Group.objects.create(name="Sam's Group") user_group = UserGroup.objects.create(user=user, group=group) self.assertEqual(UserGroup.objects.count(), 1) delete_url = '/api/v1/user_group/%s/' % user_group.id self.ts.delete(self, delete_url) self.assertEqual(UserGroup.objects.count(), 0)
def setUp(self): super(IndicatorCalculationResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id) LocationPermission.objects.create(user_id=self.ts.user.id, top_lvl_location_id=self.top_lvl_location.id) self.ind = self.ts.create_arbitrary_indicator()
def setUp(self): ## instantiate the test client and all other methods ## super(CampaignResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Nigeria', location_name='Nigeria') self.sub_location = self.ts.create_arbitrary_location( self.lt.id, location_name='Kano', location_code='Kano', parent_location_id=self.top_lvl_location.id) self.it = IndicatorTag.objects.create(tag_name='Polio') self.ct = CampaignType.objects.create(name='NID') self.ct_2 = CampaignType.objects.create(name='SNID') self.can_see_campaign = self.ts.create_arbitrary_campaign( campaign_type_id=self.ct.id, location_id=self.top_lvl_location.id, indicator_tag_id=self.it.id, name="can_see" ) self.can_see_campaign_2 = self.ts.create_arbitrary_campaign( campaign_type_id=self.ct_2.id, location_id=self.top_lvl_location.id, indicator_tag_id=self.it.id, name="can_see2" ) # self.can_not_see_campaign = self.ts.create_arbitrary_campaign( # campaign_type_id=self.ct.id, # location_id=self.not_allowed_to_see_location.id, # indicator_tag_id=self.it.id, # ) # # ### set the user permission ### # LocationPermission.objects.create(user_id=self.ts.user.id, # top_lvl_location_id=self.top_lvl_location.id) self.ts.get_credentials(self) ltr = LocationTreeCache() ltr.main()
class AllMetaResourceTest(RhizomeApiTestCase): # ./manage.py test rhizome.tests.test_api_all_meta.AllMetaResourceTest --settings=rhizome.settings.test def setUp(self): super(AllMetaResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.not_allowed_to_see_location = self.ts.create_arbitrary_location( self.lt.id) def test_all_meta_get(self): resp = self.ts.get(self, '/api/v1/all_meta/') self.assertHttpOK(resp) response_data = self.deserialize(resp) self.assertEqual(len(response_data['objects']), 1) def test_all_meta_location_json(self): resp = self.ts.get(self, '/api/v1/all_meta/') self.assertHttpOK(resp) response_data = self.deserialize(resp) location_data = response_data['objects'][0]['locations'] self.assertEqual(type(location_data), list)
def setUp(self): super(CacheMetaResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='The Solar System', location_name='The Solar System' ) self.leaf_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Mars', location_name='Mars', parent_location_id = self.top_lvl_location.id )
class SourceSubmissionResourceTest(RhizomeApiTestCase): def setUp(self): super(SourceSubmissionResourceTest, self).setUp() self.ts = TestSetupHelpers() self.ts.load_some_metadata() ltr = LocationTreeCache() ltr.main() self.mapped_location_id = self.ts.locations[0].id self.loc_map = SourceObjectMap.objects.create( source_object_code='AF001039003000000000', content_type='location', mapped_by_id=self.ts.user.id, master_object_id=self.mapped_location_id ) source_campaign_string = '2016 March NID OPV' self.mapped_campaign_id = self.ts.campaigns[0].id self.campaign_map = SourceObjectMap.objects.create( source_object_code=source_campaign_string, content_type='campaign', mapped_by_id=self.ts.user.id, master_object_id=self.mapped_campaign_id ) self.mapped_indicator_with_data = self.ts.locations[2].id self.indicator_map = SourceObjectMap.objects.create( source_object_code='Percent missed due to other reasons', content_type='indicator', mapped_by_id=self.ts.user.id, master_object_id=self.mapped_indicator_with_data ) def test_get_source_submission_by_doc(self): doc = self.ts.create_arbitrary_document( file_type='campaign', document_docfile='eoc_post_campaign.csv') doc.transform_upload() get_data = {'document_id': doc.id} resp = self.ts.get(self, '/api/v1/source_submission/', get_data) self.assertHttpOK(resp) resp_data = self.deserialize(resp) self.assertEqual(len(SourceSubmission.objects.all()), len(resp_data['objects'])) def test_get_source_submission_by_id(self): doc = self.ts.create_arbitrary_document( document_docfile='eoc_post_campaign.csv', file_type='campaign') doc.transform_upload() ss_id = SourceSubmission.objects.all()[0].id resp = self.ts.get(self, '/api/v1/source_submission/%s/' % ss_id) self.assertHttpOK(resp) resp_data = self.deserialize(resp) self.assertEqual(resp_data['id'], ss_id)
class SourceSubmissionResourceTest(RhizomeApiTestCase): def setUp(self): super(SourceSubmissionResourceTest, self).setUp() self.ts = TestSetupHelpers() self.ts.load_some_metadata() ltr = LocationTreeCache() ltr.main() self.mapped_location_id = self.ts.locations[0].id self.loc_map = SourceObjectMap.objects.create( source_object_code='AF001039003000000000', content_type='location', mapped_by_id=self.ts.user.id, master_object_id=self.mapped_location_id) source_campaign_string = '2016 March NID OPV' self.mapped_campaign_id = self.ts.campaigns[0].id self.campaign_map = SourceObjectMap.objects.create( source_object_code=source_campaign_string, content_type='campaign', mapped_by_id=self.ts.user.id, master_object_id=self.mapped_campaign_id) self.mapped_indicator_with_data = self.ts.locations[2].id self.indicator_map = SourceObjectMap.objects.create( source_object_code='Percent missed due to other reasons', content_type='indicator', mapped_by_id=self.ts.user.id, master_object_id=self.mapped_indicator_with_data) def test_get_source_submission_by_doc(self): doc = self.ts.create_arbitrary_document( file_type='campaign', document_docfile='eoc_post_campaign.csv') doc.transform_upload() get_data = {'document_id': doc.id} resp = self.ts.get(self, '/api/v1/source_submission/', get_data) self.assertHttpOK(resp) resp_data = self.deserialize(resp) self.assertEqual(len(SourceSubmission.objects.all()), len(resp_data['objects'])) def test_get_source_submission_by_id(self): doc = self.ts.create_arbitrary_document( document_docfile='eoc_post_campaign.csv', file_type='campaign') doc.transform_upload() ss_id = SourceSubmission.objects.all()[0].id resp = self.ts.get(self, '/api/v1/source_submission/%s/' % ss_id) self.assertHttpOK(resp) resp_data = self.deserialize(resp) self.assertEqual(resp_data['id'], ss_id)
def setUp(self): ## instantiate the test client and all other methods ## super(SourceObjectMapResourceTest, self).setUp() self.test_setup = TestSetupHelpers() self.user = self.test_setup.user self.lt = self.test_setup.create_arbitrary_location_type() self.location = \ self.test_setup.create_arbitrary_location(self.lt.id) self.document = self\ .test_setup.create_arbitrary_document(id=22,file_type ='campaign') self.som_0 = SourceObjectMap.objects.create( source_object_code='This is not mapped', master_object_id = -1, content_type = 'location' ) DocumentSourceObjectMap.objects.create( document_id = self.document.id, source_object_map_id = self.som_0.id ) self.som_1 = SourceObjectMap.objects.create( source_object_code='This is mapped', master_object_id = self.location.id, content_type = 'location' ) DocumentSourceObjectMap.objects.create( document_id = self.document.id, source_object_map_id = self.som_1.id ) indicator_df = read_csv('rhizome/tests/_data/indicators.csv') self.indicators = self.test_setup.model_df_to_data( indicator_df, Indicator)
class IndicatorCalculationResourceTest(RhizomeApiTestCase): def setUp(self): super(IndicatorCalculationResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id) LocationPermission.objects.create(user_id=self.ts.user.id, top_lvl_location_id=self.top_lvl_location.id) self.ind = self.ts.create_arbitrary_indicator() def test_create_calculation(self): Indicator.objects.create(short_name='Test Indicator 1', name='Test Indicator for the Tag 1', data_format='int', description='Test Indicator for the Tag 1 Description', ) Indicator.objects.create(short_name='Test Indicator 2', name='Test Indicator for the Tag 2', data_format='int', description='Test Indicator for the Tag 2 Description', ) list = Indicator.objects.all().order_by('-id') indicator_1 = list[0] indicator_2 = list[1] CalculatedIndicatorComponent.objects.filter(indicator_id=indicator_1.id, indicator_component_id=indicator_2.id).delete() post_data = {'indicator_id': indicator_1.id, 'indicator_component_id': indicator_2.id, 'calculation': 'DENOMINATOR'} resp = self.ts.post( self, '/api/v1/indicator_calculation/', data=post_data) self.assertHttpCreated(resp) response_data = self.deserialize(resp) indicator_calculation = CalculatedIndicatorComponent.objects.all( ).order_by('-id')[0] self.assertEqual(indicator_calculation.id, response_data['id']) self.assertEqual(indicator_1.id, response_data['indicator_id']) self.assertEqual(indicator_2.id, \ response_data['indicator_component_id']) self.assertEqual(indicator_calculation.calculation, response_data['calculation']) def test_remove_calculation(self): Indicator.objects.create(short_name='Test Indicator 1', name='Test Indicator for the Tag 1', description='Test Indicator for the Tag 1 Description', ) Indicator.objects.create(short_name='Test Indicator 2', name='Test Indicator for the Tag 2', description='Test Indicator for the Tag 2 Description', ) list = Indicator.objects.all().order_by('-id') indicator_1 = list[0] indicator_2 = list[1] CalculatedIndicatorComponent.objects.all().delete() component = CalculatedIndicatorComponent.objects\ .create(indicator_id=indicator_1.id, indicator_component_id=indicator_2.id, calculation='test calculation') self.assertEqual(CalculatedIndicatorComponent.objects.count(), 1) delete_url = '/api/v1/indicator_calculation/%s/' % str(component.id) self.api_client.delete(delete_url, format='json', data={ }, authentication=self.ts.get_credentials(self)) self.assertEqual(CalculatedIndicatorComponent.objects.count(), 0) def test_remove_calculation_wrong_id(self): some_id = 123456 delete_url = '/api/v1/indicator_calculation/%s/' % str(some_id) resp = self.api_client.delete(delete_url, format='json', data={ }, authentication=self.ts.get_credentials(self)) self.assertHttpApplicationError(resp) def test_remove_calculation_no_id(self): delete_url = '/api/v1/indicator_calculation/' resp = self.api_client.delete(delete_url, format='json', data={ }, authentication=self.ts.get_credentials(self)) self.assertEqual(resp.status_code, 500) def test_get_calculation(self): ind = Indicator.objects.create(short_name='Test Indicator 1', name='Test Indicator for the Tag 1', description='Test Indicator for the Tag 1 Description', ) ind2 = Indicator.objects.create(short_name='Test Indicator 2', name='Test Indicator for the Tag 2', data_format='int', description='Test Indicator for the Tag 2 Description', ) it = CalculatedIndicatorComponent.objects.create( indicator_id=ind.id, indicator_component_id=ind2.id, calculation='DENOMINATOR', ) get_data = {'indicator_id': ind.id} resp = self.ts.get(self, '/api/v1/indicator_calculation/', get_data) self.assertHttpOK(resp) resp_data = self.deserialize(resp) self.assertEqual(len(resp_data['objects']), 1) def test_get_calculation_invalid_id(self): get_data = {'indicator_id': 1234} resp = self.ts.get(self, '/api/v1/indicator_calculation/', get_data) self.assertHttpOK(resp) resp_data = self.deserialize(resp) self.assertEqual(len(resp_data['objects']), 0)
def setUp(self): super(RefreshMasterAPIResourceTest, self).setUp() self.create_metadata() self.ts = TestSetupHelpers()
class IndicatorResourceTest(RhizomeApiTestCase): def setUp(self): super(IndicatorResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id) LocationPermission.objects.create(user_id=self.ts.user.id, top_lvl_location_id=self.top_lvl_location.id) self.ind = self.ts.create_arbitrary_indicator() def test_auth_valid(self): resp = self.api_client.get('/api/v1/', format='json') self.assertValidJSONResponse(resp) def test_get_indicator_id(self): get_data = {'id': self.ind.id} resp = self.ts.get(self, '/api/v1/indicator/', data=get_data) resp_data = self.deserialize(resp) self.assertEqual(len(resp_data['objects']), 1) def test_get_invalid_indicator_id(self): get_data = {'id': 123456} resp = self.ts.get(self, '/api/v1/indicator/', data=get_data) self.assertHttpOK(resp) resp_data = self.deserialize(resp) self.assertEqual(len(resp_data['objects']), 0) def test_create_indicator(self): Indicator.objects.all().delete() self.assertEqual(Indicator.objects.count(), 0) post_data = {'name': 'New test indicator name', 'short_name': 'New test short name', 'description': 'test', 'bad_bound': 1, 'good_bound': 10, 'data_format': 'int', 'source_name': 'RHIZOME' } resp = self.ts.post(self, '/api/v1/indicator/', data=post_data) self.assertHttpCreated(resp) resp_data = self.deserialize(resp) self.assertEqual(Indicator.objects.count(), 1) self.assertEqual(resp_data['name'], 'New test indicator name') def test_create_indicator_missing_fields(self): Indicator.objects.all().delete() self.assertEqual(Indicator.objects.count(), 0) post_data = {'name': 'New test indicator name', 'short_name': 'New test short name', 'description': 'test', 'source_name': 'RHIZOME' } resp = self.ts.post(self, '/api/v1/indicator/', data=post_data) self.assertHttpApplicationError(resp) # the indicator is invalid because bad_bound and good_bound are passed as # strings def test_create_indicator_invalid(self): Indicator.objects.all().delete() self.assertEqual(Indicator.objects.count(), 0) post_data = {'name': 'New test indicator name', 'short_name': 'New test short name', 'description': 'test', 'bad_bound': "hello", 'good_bound': 'dsds', 'source_name': '???' } resp = self.ts.post(self, '/api/v1/indicator/', data=post_data) self.assertHttpApplicationError(resp) def test_bound_and_tag_json(self): Indicator.objects.all().delete() ind = Indicator.objects.create(**{ 'name': 'test name', 'short_name': 'test short name', 'data_format': 'int', 'description': 'test description', }) ## tags ## ind_tag_0 = IndicatorTag.objects.create(tag_name='tag1') ind_tag_1 = IndicatorTag.objects.create(tag_name='tag2') indicator_to_tag_0 = IndicatorToTag.objects.create( indicator=ind, indicator_tag=ind_tag_0 ) indicator_to_tag_1 = IndicatorToTag.objects.create( indicator=ind, indicator_tag=ind_tag_1 ) ## bounds ## bound_dict_0 = {u'indicator_id': ind.id, u'mn_val': 10, u'mx_val': 20, u'bound_name': u'Good'} bound_dict_1 = {'indicator_id': ind.id, u'mn_val': 20, u'mx_val': 30, u'bound_name': u'Bad'} IndicatorBound.objects.create(**bound_dict_0) IndicatorBound.objects.create(**bound_dict_1) ## cache the indicator id ## ic = IndicatorCache([ind.id]) ic.main() target_tag_json = [ind_tag_0.id, ind_tag_1.id] target_bound_json = [bound_dict_0, bound_dict_1] resp = self.ts.get(self, '/api/v1/indicator/', data={}) data = self.deserialize(resp) objects = data['objects'] ## basic attributes ## self.assertEqual(ind.short_name, objects[0]['short_name']) self.assertEqual(ind.name, objects[0]['name']) self.assertEqual(ind.description, objects[0]['description']) ## pivoted attributes ## self.assertEqual(sorted(target_tag_json), sorted(objects[0]['tag_json'])) self.assertEqual(sorted(target_bound_json), sorted(objects[0]['bound_json']))
class DateDataPointResourceTest(RhizomeApiTestCase): # python manage.py test rhizome.tests.test_api_datapoint_groupby_date --settings=rhizome.settings.test def setUp(self): super(DateDataPointResourceTest, self).setUp() # Create a user. self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username,\ '*****@*****.**', self.password) self.lt = LocationType.objects.create(name='Country',admin_level = 0) self.province_lt = LocationType.objects.create(name='Province'\ ,admin_level = 1) self.district_lt = LocationType.objects.create(name='District'\ ,admin_level = 2) self.ind = Indicator.objects.create( name = 'Polio Cases', short_name = 'Polio Cases', data_format = 'date_int' ) self.top_lvl_location = Location.objects.create( name = 'Afghanistan', location_code = 'Afghanistan', id=1234, location_type_id = self.lt.id, ) self.some_province = Location.objects.create( name = 'Province', location_code = 'Province', id=432, parent_location_id = self.top_lvl_location.id, location_type_id = self.province_lt.id ) self.some_district = Location.objects.create( name = 'Achin', location_code = 'Achin', id=4321, parent_location_id = self.some_province.id, location_type_id = self.district_lt.id ) ltc = LocationTreeCache() ltc.main() LocationPermission.objects.create(user_id = self.user.id,\ top_lvl_location_id = self.top_lvl_location.id) self.get_credentials() self.create_polio_cases() self.ts = TestSetupHelpers() self.doc_id = Document.objects.create(doc_title='Data Entry').id def create_polio_cases(self): df = pd.read_csv('rhizome/tests/_data/AfgPolioCases.csv') for ix, row in df.iterrows(): DataPoint.objects.create( location_id = self.some_district.id, indicator_id = self.ind.id, data_date = datetime.strptime(row.data_date, '%d-%m-%y'), value = 1, source_submission_id = 1, unique_index = str(self.some_district.id) + str(self.ind.id) +\ str(row.data_date) ) def get_credentials(self): result = self.api_client.client.login(username=self.username, password=self.password) return result def test_get_list(self): # python manage.py test rhizome.tests.test_api_datapoint_groupby_date.DateDataPointResourceTest.test_get_list --settings=rhizome.settings.test get = {'group_by_time':'year', 'indicator__in' : self.ind.id, 'start_date' : '2013-01-01', 'end_date' : '2016-12-01', 'location_id' : self.top_lvl_location.id, 'location_depth' : 1 } resp = self.api_client.get('/api/v1/date_datapoint/', \ format='json', data=get, authentication=self.get_credentials()) response_data = self.deserialize(resp) self.assertHttpOK(resp) objects = response_data['objects'] meta = response_data['meta'] ## does the 'meta' object have what the FE needs self.assertEqual(self.ind.id, int(meta['indicator_ids'][0])) # self.assertEqual(self.top_lvl_location.id, int(meta['location_ids'][0])) ## WE SHOULD REMOVE THIS LOGIC FROM FE -- DATES ARE SEPARATE FROM CAMPAIGNS self.assertEqual(set(meta['time_groupings']),set([2014,2015,2016])) self.assertEqual(3, len(objects)) # one for each year # case_dict = {} for obj in objects: case_dict[obj['time_grouping']] = float(obj['value']) self.assertEqual(28.00, case_dict[2014]) self.assertEqual(20.00, case_dict[2015]) self.assertEqual(3.0, case_dict[2016]) # basic test to just get a datapoint at a location for which we have data def test_get_list_no_recursion(self): # python manage.py test rhizome.tests.test_api_datapoint_groupby_date.DateDataPointResourceTest.test_get_list_no_recursion --settings=rhizome.settings.test location_id = 4321 get = { 'group_by_time' :'all_time', 'indicator__in': self.ind.id, 'start_date': '2013-01-01', 'end_date': '2016-01-01', 'location_id': location_id, 'location_depth' : 0 } resp = self.api_client\ .get('/api/v1/date_datapoint/', data = get, format = 'json', authentication = self.get_credentials()) self.assertHttpOK(resp) response_data = self.deserialize(resp) dps_all_time = DataPoint.objects.filter(indicator_id=self.ind.id) total_all_time = 0 for dp in dps_all_time: total_all_time += dp.value self.assertEqual(len(response_data['objects']), 1) self.assertEqual(int(response_data['objects'][0]['location_id']), location_id) # not sure if this is a bug or what, but start and end date seem to be irrelevant when using group_by_time def test_get_list_diff_start_end_dates(self): get = { 'group_by_time' :'year', 'indicator__in': self.ind.id, 'start_date': '2013-01-01', 'end_date': '2016-01-01', 'location_id__in': self.top_lvl_location.id, 'location_depth' : 1 } resp = self.api_client.get('/api/v1/date_datapoint/', data = get, format='json', authentication=self.get_credentials()) self.assertHttpOK(resp) response_data = self.deserialize(resp) objects_1 = response_data['objects'] get_2 = { 'group_by_time' :'year', 'indicator__in': self.ind.id, 'start_date': '2016-01-01', 'end_date': '2016-01-01', 'location_id__in': self.top_lvl_location.id, 'location_depth' : 1 } resp_2 = self.api_client.get('/api/v1/date_datapoint/',\ data = get_2, format='json',\ authentication=self.get_credentials()) self.assertHttpOK(resp_2) response_data_2 = self.deserialize(resp_2) objects_2 = response_data_2['objects'] self.assertEqual(len(objects_1), len(objects_2)) def test_get_list_quarter_and_all_time(self): get = { 'group_by_time' :'quarter', 'indicator__in': self.ind.id, 'start_date': '2013-01-01', 'end_date': '2016-07-01', 'location_id': self.top_lvl_location.id, 'location_depth' : 1 } resp = self.api_client.get('/api/v1/date_datapoint/', \ data = get , format='json', authentication=self.get_credentials()) response_data = self.deserialize(resp) self.assertHttpOK(resp) dps_q1_2014 = DataPoint.objects.filter( data_date__range=('2014-01-01', '2014-03-31'),\ indicator = self.ind.id ) total = 0 for dp in dps_q1_2014: total += dp.value # find the total for q1 2014 q1_found = False for indicator in response_data['objects']: campaign = indicator['time_grouping'] if campaign == '20141': value = float(indicator['value']) self.assertEqual(value, total) q1_found = True self.assertTrue(q1_found) get_2 = { 'group_by_time' :'all_time', 'indicator__in': self.ind.id, 'start_date': '2013-01-01', 'end_date': '2016-07-01', 'location_id': self.top_lvl_location.id, 'location_depth' : 1 } resp = self.api_client.get('/api/v1/date_datapoint/', \ data = get_2, format='json',\ authentication=self.get_credentials()) response_data = self.deserialize(resp) dps_all_time = DataPoint.objects.filter(indicator_id=self.ind.id) total_all_time = 0 for dp in dps_all_time: total_all_time += dp.value self.assertEqual(len(response_data['objects']), 1) self.assertEqual(float(response_data['objects'][0]['value']), total_all_time) # provide a non-existent id def test_get_list_bogus_id(self): get = { 'group_by_time' :'quarter', 'indicator__in': 3223, 'start_date': '2013-01-01', 'end_date': '2016-01-01', 'location_id__in': self.top_lvl_location.id, 'location_depth' : 1 } resp = self.api_client.get('/api/v1/date_datapoint/', \ data = get, format='json', authentication=self.get_credentials()) self.assertHttpOK(resp) response_data = self.deserialize(resp) self.assertEqual(len(response_data['objects']), 0) def test_get_list_wrong_grouping(self): ''' What happens when we request an unsupported time grouping ''' # python manage.py test rhizome.tests.test_api_datapoint_groupby_date.DateDataPointResourceTest.test_get_list_wrong_grouping --settings=rhizome.settings.test get = { 'group_by_time' :'xxx', 'indicator__in': self.ind.id, 'start_date': '2013-01-01', 'end_date': '2016-01-01', 'location_id': self.top_lvl_location.id, 'location_depth' : 1 } resp = self.api_client.get('/api/v1/date_datapoint/',data = get,\ format='json', authentication=self.get_credentials()) self.deserialize(resp) self.assertHttpApplicationError(resp) def _show_missing_data(self): ''' This test is not in the suite because for date_datapoint results, the back end should not be in charge of creating every possible datapoint wiht a null value in order to handle discontinuity. show_all_data should not be a parameter and we should remove this and handle the fallout in the front end charting library. ''' # python manage.py test rhizome.tests.test_api_datapoint_groupby_date.DateDataPointResourceTest.test_show_missing_data --settings=rhizome.settings.test rando_ind = Indicator.objects.create( name = 'some other damn indicator', short_name = 'we don\'t care!', data_format = 'int' ) rando_ind_2 = Indicator.objects.create( name = 'some other indicator', short_name = 'we don care!', data_format = 'int' ) # ind_list = [rando_ind.id, rando_ind_2.id] ind_list = '{0},{1}'.format(rando_ind.id, rando_ind_2.id) get = { 'group_by_time' :'year', 'indicator__in': ind_list, 'start_date': '2013-01-01', 'end_date': '2016-01-01', 'location_id__in': self.top_lvl_location.id, 'location_depth' : 0, 'show_missing_data': 1 } resp = self.api_client.get('/api/v1/date_datapoint/', \ data = get , format='json',\ authentication=self.get_credentials()) response_data = self.deserialize(resp) self.assertHttpOK(resp) self.assertEqual(len(response_data['objects']), 6) ## should be one object for the location, for each Indicator ## for each location and each time grouping. ## 3 yrs * 2 indicators * one location = 6 ## if location_depth = 1, the number would have to take into ## account the number of sub locations one step under the parent def test_patch_date_datapoint(self): ''' create a datapoint with the ORM, submit a PATCH request and see if the value changed. If the user tries to change anything exept the value, there should be an error. ''' dp_to_patch = DataPoint.objects.all()[0] patch_data = {'value': 101.01} dp_url = '/api/v1/date_datapoint/%s/' % dp_to_patch.id ## submit the patch and make sure it has the proper response code resp = self.ts.patch(self, dp_url, data=patch_data) self.assertHttpAccepted(resp) ## now get the dp and see if the value has been is updated ## dp_to_patch = DataPoint.objects.get(id=dp_to_patch.id) self.assertEqual(dp_to_patch.value, patch_data['value']) def test_post_date_datapoint(self): ''' post a record to the datapoint table ''' indicator_id = Indicator.objects.all()[0].id location_id = Location.objects.all()[0].id val = 10.0 data = { 'indicator_id': indicator_id, 'data_date': '2016-01-01', 'location_id': location_id, 'value': val } resp = self.ts.post(self, '/api/v1/date_datapoint/', data) self.assertHttpCreated(resp) response_data = self.deserialize(resp) self.assertEqual(response_data['value'], val) another_date = '2016-02-02' data['data_date'] = another_date resp = self.ts.post(self, '/api/v1/date_datapoint/', data) self.assertHttpCreated(resp) response_data = self.deserialize(resp) self.assertEqual(response_data['data_date'], another_date) def test_post_date_datapoint_missing_data(self): ''' if we do not have all hte keys we need, throw an error ''' data = {'value': 10} resp = self.ts.post(self, '/api/v1/date_datapoint/', data) self.assertHttpApplicationError(resp) def test_post_date_datapoint_invalid_data(self): ''' The indicator, and campaign dont exists, the api should tell us ''' data = { # 'document_id': doc_id, 'indicator_id': 4324, 'time_grouping': 32132123, 'location_id': 4321, 'value': 10 } resp = self.ts.post(self, '/api/v1/date_datapoint/', data) self.assertHttpApplicationError(resp) response_data = self.deserialize(resp) def test_delete_date_datapoint(self): ''' create a datapoint, then delete it, make sure that it is no longer there. ''' dp = DataPoint.objects.all()[0] delete_url = '/api/v1/date_datapoint/%d/' % dp.id resp = self.ts.delete(self, delete_url) ## now make sure that it is not there # dpc_query = DataPoint.objects.filter(id=dp.id) self.assertEqual(len(dpc_query), 0) def test_get_date_datapoint_by_id(self): ''' Here we get one object from the API and ensure it has the proper data from when we inserted it. ''' dp_obj = DataPoint.objects.all()[0] resp = self.ts.get(self, '/api/v1/date_datapoint/%s/' % dp_obj.id) self.assertHttpOK(resp) response_data = self.deserialize(resp) self.assertEqual(response_data['value'], dp_obj.value)
class DocumentResourceTest(RhizomeApiTestCase): def setUp(self): super(DocumentResourceTest, self).setUp() self.ts = TestSetupHelpers() self.create_metadata() def test_obj_create(self): path = os.path.join(os.path.dirname(__file__), "_data/eoc_post_campaign.csv") file_obj = open(path).read() encoded_data = base64.b64encode(file_obj) post_data = {"docfile": encoded_data, "file_type": "campaign", "doc_title": "eoc_post_campaign.csv"} resp = self.ts.post(self, "/api/v1/source_doc/", post_data) self.deserialize(resp) self.assertHttpCreated(resp) def test_xlsx_transform(self): path = os.path.join(os.path.dirname(__file__), "_data/eoc_post_campaign.xlsx") file_obj = open(path).read() encoded_data = base64.b64encode(file_obj) post_data = {"docfile": encoded_data, "file_type": "campaign", "doc_title": "eoc_post_campaign.xlsx"} resp = self.ts.post(self, "/api/v1/source_doc/", post_data) self.assertHttpCreated(resp) resp_data = self.deserialize(resp) get_data = {"document_id": resp_data["id"]} ## this right here emulates what happens when the user clicks the ## `refresh master` button resp = self.ts.get(self, "/api/v1/transform_upload/", get_data) the_value_from_the_database = DataPointComputed.objects.get( campaign_id=self.mapped_campaign_id, indicator_id=self.mapped_indicator_with_data, location_id=self.mapped_location_id, ).value some_cell_value_from_the_file = 0.082670906 # FIXME find this from the data frame by selecting the cell where we # have mapped the data.. self.assertEqual(some_cell_value_from_the_file, the_value_from_the_database) def test_upload_empty_csv(self): file_name = "_data/empty_csv.csv" path = os.path.join(os.path.dirname(__file__), file_name) file_obj = open(path).read() encoded_data = base64.b64encode(file_obj) post_data = {"docfile": encoded_data, "file_type": "campaign", "doc_title": "empty_csv.csv"} resp = self.ts.post(self, "/api/v1/source_doc/", post_data) self.assertHttpApplicationError(resp) def test_upload_empty_excel(self): file_name = "_data/empty_xlsx.xlsx" path = os.path.join(os.path.dirname(__file__), file_name) file_obj = open(path).read() encoded_data = base64.b64encode(file_obj) post_data = {"docfile": encoded_data, "file_type": "campaign", "doc_title": "empty_xlsx.xlsx"} resp = self.ts.post(self, "/api/v1/source_doc/", post_data) self.assertHttpApplicationError(resp) def test_post_no_doc_title(self): path = os.path.join(os.path.dirname(__file__), "_data/eoc_post_campaign.csv") file_obj = open(path).read() encoded_data = base64.b64encode(file_obj) post_data = {"docfile": encoded_data, "file_type": "campaign"} resp = self.ts.post(self, "/api/v1/source_doc/", post_data) self.assertHttpApplicationError(resp) def create_metadata(self): """ Creating the Indicator, location, Campaign, meta data needed for the system to aggregate / caclulate. """ top_lvl_tag = IndicatorTag.objects.create(id=1, tag_name="Polio") campaign_df = read_csv("rhizome/tests/_data/campaigns.csv") campaign_df["start_date"] = to_datetime(campaign_df["start_date"]) campaign_df["end_date"] = to_datetime(campaign_df["end_date"]) location_df = read_csv("rhizome/tests/_data/locations.csv") indicator_df = read_csv("rhizome/tests/_data/indicators.csv") campaign_type = CampaignType.objects.create(id=1, name="test") locations = self.model_df_to_data(location_df, Location) campaigns = self.model_df_to_data(campaign_df, Campaign) indicators = self.model_df_to_data(indicator_df, Indicator) self.user_id = User.objects.create_user("test", "*****@*****.**", "test").id self.mapped_location_id = locations[0].id loc_map = SourceObjectMap.objects.create( source_object_code="AF001039003000000000", content_type="location", mapped_by_id=self.user_id, master_object_id=self.mapped_location_id, ) source_campaign_string = "2016 March NID OPV" self.mapped_campaign_id = campaigns[0].id campaign_map = SourceObjectMap.objects.create( source_object_code=source_campaign_string, content_type="campaign", mapped_by_id=self.user_id, master_object_id=self.mapped_campaign_id, ) self.mapped_indicator_id_0 = indicators[0].id indicator_map = SourceObjectMap.objects.create( source_object_code="Percent missed children_PCA", content_type="indicator", mapped_by_id=self.user_id, master_object_id=self.mapped_indicator_id_0, ) self.mapped_indicator_with_data = locations[2].id indicator_map = SourceObjectMap.objects.create( source_object_code="Percent missed due to other reasons", content_type="indicator", mapped_by_id=self.user_id, master_object_id=self.mapped_indicator_with_data, ) ltc = LocationTreeCache() ltc.main() def model_df_to_data(self, model_df, model): meta_ids = [] non_null_df = model_df.where((notnull(model_df)), None) list_of_dicts = non_null_df.transpose().to_dict() for row_ix, row_dict in list_of_dicts.iteritems(): row_id = model.objects.create(**row_dict) meta_ids.append(row_id) return meta_ids
class LocationPermissionResourceTest(RhizomeApiTestCase): def setUp(self): super(LocationPermissionResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Nigeria', location_name='Nigeria') def test_get_location_permission(self): lp = LocationPermission.objects.create( user_id=self.ts.user.id, top_lvl_location_id=self.top_lvl_location.id) data = {'user_id': self.ts.user.id} resp = self.ts.get(self, '/api/v1/location_responsibility/', data=data) response_data = self.deserialize(resp) self.assertHttpOK(resp) self.assertEqual(response_data['objects'][0]['id'], lp.id) def test_get_location_permission_no_id(self): resp = self.ts.get(self, '/api/v1/location_responsibility/') self.assertHttpApplicationError(resp) def test_create_location_permission(self): data = { 'top_lvl_location_id': self.top_lvl_location.id, 'user_id': self.ts.user.id } resp = self.ts.post(self, '/api/v1/location_responsibility/', data=data) response_data = self.deserialize(resp) self.assertHttpCreated(resp) self.assertEqual(response_data['user_id'], self.ts.user.id) self.assertEqual(response_data['top_lvl_location_id'],\ self.top_lvl_location.id) def test_create_location_permission_missing_id(self): data = {'user_id': self.ts.user.id} resp = self.ts.post(self, '/api/v1/location_responsibility/', data=data) self.assertHttpApplicationError(resp) def test_update_location_permission(self): lp = LocationPermission.objects.create( user_id=self.ts.user.id, top_lvl_location_id=self.top_lvl_location.id) # update the location new_location = self.ts.create_arbitrary_location(self.lt.id, location_code='Kenya', location_name='Kenya') data = { 'top_lvl_location_id': new_location.id, 'user_id': self.ts.user.id } resp = self.ts.patch(self, '/api/v1/location_responsibility/%s/' % lp.id, data=data) response_data = self.deserialize(resp) self.assertHttpAccepted(resp) self.assertEqual(response_data['user_id'], self.ts.user.id) self.assertEqual(response_data['top_lvl_location_id'], new_location.id)
class IndicatorToTagResourceTest(RhizomeApiTestCase): def setUp(self): super(IndicatorToTagResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id) self.ind_tag = IndicatorTag.objects.create(tag_name='a tag') self.ind = self.ts.create_arbitrary_indicator() def test_obj_create(self): data = { 'indicator_tag_id': self.ind_tag.id, 'indicator_id': self.ind.id } resp = self.ts.post(self, '/api/v1/indicator_to_tag/', data=data) response_data = self.deserialize(resp) self.assertHttpCreated(resp) self.assertEqual(response_data['indicator_id'], self.ind.id) self.assertEqual(response_data['indicator_tag_id'], self.ind_tag.id) def test_obj_get_ind_id(self): ind_to_tag = IndicatorToTag.objects.create(indicator_id=self.ind.id, indicator_tag_id=self.ind_tag.id) data = { 'indicator_id': self.ind.id } resp = self.ts.get(self, '/api/v1/indicator_to_tag/', data=data) response_data = self.deserialize(resp) self.assertHttpOK(resp) self.assertEqual(len(response_data['objects']), 1) self.assertEqual(response_data['objects'][0][ 'id'], IndicatorToTag.objects.all()[0].id) def test_obj_get_ind_id_invalid(self): data = { 'indicator_id': 3232 } resp = self.ts.get(self, '/api/v1/indicator_to_tag/', data=data) response_data = self.deserialize(resp) self.assertEqual(len(response_data['objects']), 0) def test_obj_get_ind_tag_id(self): ind_to_tag = IndicatorToTag.objects.create(indicator_id=self.ind.id, indicator_tag_id=self.ind_tag.id) data = { 'indicator_tag_id': self.ind_tag.id } resp = self.ts.get(self, '/api/v1/indicator_to_tag/', data=data) response_data = self.deserialize(resp) self.assertEqual(len(response_data['objects']), 1) self.assertEqual(response_data['objects'][0][ 'id'], IndicatorToTag.objects.all()[0].id) def test_obj_get_all(self): self.ind_tag_2 = IndicatorTag.objects.create(tag_name='another tag') ind_to_tag = IndicatorToTag.objects.create(indicator_id=self.ind.id, indicator_tag_id=self.ind_tag.id) ind_to_tag_2 = IndicatorToTag.objects.create(indicator_id=self.ind.id, indicator_tag_id=self.ind_tag_2.id) resp = self.ts.get(self, '/api/v1/indicator_to_tag/') response_data = self.deserialize(resp) self.assertHttpOK(resp) self.assertEqual(len(response_data['objects']), 2) def test_obj_delete(self): ind_to_tag = IndicatorToTag.objects.create(indicator_id=self.ind.id, indicator_tag_id=self.ind_tag.id) self.assertEqual(IndicatorToTag.objects.count(), 1) delete_url = '/api/v1/indicator_to_tag/%s/' % str(ind_to_tag.id) self.ts.delete(self, delete_url) self.assertEqual(IndicatorToTag.objects.count(), 0)
def setUp(self): super(DocumentResourceTest, self).setUp() self.ts = TestSetupHelpers() self.create_metadata()
class DocumentResourceTest(RhizomeApiTestCase): def setUp(self): super(DocumentResourceTest, self).setUp() self.ts = TestSetupHelpers() self.create_metadata() def test_obj_create(self): path = os.path.join(os.path.dirname(__file__), '_data/eoc_post_campaign.csv') file_obj = open(path).read() encoded_data = base64.b64encode(file_obj) post_data = {'docfile': encoded_data, 'file_type': 'campaign', 'doc_title': 'eoc_post_campaign.csv'} resp = self.ts.post(self, '/api/v1/source_doc/', post_data) self.deserialize(resp) self.assertHttpCreated(resp) def test_xlsx_transform(self): path = os.path.join(os.path.dirname(__file__), '_data/eoc_post_campaign.xlsx') file_obj = open(path).read() encoded_data = base64.b64encode(file_obj) post_data = {'docfile': encoded_data, 'file_type': 'campaign', 'doc_title': 'eoc_post_campaign.xlsx'} resp = self.ts.post(self, '/api/v1/source_doc/', post_data) self.assertHttpCreated(resp) resp_data = self.deserialize(resp) get_data = {'document_id':resp_data['id']} ## this right here emulates what happens when the user clicks the ## `refresh master` button resp = self.ts.get(self, '/api/v1/transform_upload/', get_data) the_value_from_the_database = DataPointComputed.objects.get( campaign_id=self.mapped_campaign_id, indicator_id=self.mapped_indicator_with_data, location_id=self.mapped_location_id ).value some_cell_value_from_the_file = 0.082670906 # FIXME find this from the data frame by selecting the cell where we # have mapped the data.. self.assertEqual(some_cell_value_from_the_file, \ the_value_from_the_database) def test_upload_empty_csv(self): file_name = '_data/empty_csv.csv' path = os.path.join(os.path.dirname(__file__), file_name) file_obj = open(path).read() encoded_data = base64.b64encode(file_obj) post_data = {'docfile': encoded_data, 'file_type': 'campaign', \ 'doc_title': 'empty_csv.csv'} resp = self.ts.post(self, '/api/v1/source_doc/', post_data) self.assertHttpApplicationError(resp) def test_upload_empty_excel(self): file_name = '_data/empty_xlsx.xlsx' path = os.path.join(os.path.dirname(__file__), file_name) file_obj = open(path).read() encoded_data = base64.b64encode(file_obj) post_data = {'docfile': encoded_data,'file_type': 'campaign',\ 'doc_title': 'empty_xlsx.xlsx'} resp = self.ts.post(self, '/api/v1/source_doc/', post_data) self.assertHttpApplicationError(resp) def test_post_no_doc_title(self): path = os.path.join(os.path.dirname(__file__), '_data/eoc_post_campaign.csv') file_obj = open(path).read() encoded_data = base64.b64encode(file_obj) post_data = {'docfile': encoded_data,'file_type': 'campaign'} resp = self.ts.post(self, '/api/v1/source_doc/', post_data) self.assertHttpApplicationError(resp) def create_metadata(self): ''' Creating the Indicator, location, Campaign, meta data needed for the system to aggregate / caclulate. ''' top_lvl_tag = IndicatorTag.objects.create(id=1, tag_name='Polio') campaign_df = read_csv('rhizome/tests/_data/campaigns.csv') campaign_df['start_date'] = to_datetime(campaign_df['start_date']) campaign_df['end_date'] = to_datetime(campaign_df['end_date']) location_df = read_csv('rhizome/tests/_data/locations.csv') indicator_df = read_csv('rhizome/tests/_data/indicators.csv') campaign_type = CampaignType.objects.create(id=1, name="test") locations = self.model_df_to_data(location_df, Location) campaigns = self.model_df_to_data(campaign_df, Campaign) indicators = self.model_df_to_data(indicator_df, Indicator) self.user_id = User.objects.create_user( 'test', '*****@*****.**', 'test').id self.mapped_location_id = locations[0].id loc_map = SourceObjectMap.objects.create( source_object_code='AF001039003000000000', content_type='location', mapped_by_id=self.user_id, master_object_id=self.mapped_location_id ) source_campaign_string = '2016 March NID OPV' self.mapped_campaign_id = campaigns[0].id campaign_map = SourceObjectMap.objects.create( source_object_code=source_campaign_string, content_type='campaign', mapped_by_id=self.user_id, master_object_id=self.mapped_campaign_id ) self.mapped_indicator_id_0 = indicators[0].id indicator_map = SourceObjectMap.objects.create( source_object_code='Percent missed children_PCA', content_type='indicator', mapped_by_id=self.user_id, master_object_id=self.mapped_indicator_id_0 ) self.mapped_indicator_with_data = locations[2].id indicator_map = SourceObjectMap.objects.create( source_object_code='Percent missed due to other reasons', content_type='indicator', mapped_by_id=self.user_id, master_object_id=self.mapped_indicator_with_data ) ltc = LocationTreeCache() ltc.main() def model_df_to_data(self, model_df, model): meta_ids = [] non_null_df = model_df.where((notnull(model_df)), None) list_of_dicts = non_null_df.transpose().to_dict() for row_ix, row_dict in list_of_dicts.iteritems(): row_id = model.objects.create(**row_dict) meta_ids.append(row_id) return meta_ids
class CampaignResourceTest(RhizomeApiTestCase): def setUp(self): ## instantiate the test client and all other methods ## super(CampaignResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Nigeria', location_name='Nigeria') self.sub_location = self.ts.create_arbitrary_location( self.lt.id, location_name='Kano', location_code='Kano', parent_location_id=self.top_lvl_location.id) self.it = IndicatorTag.objects.create(tag_name='Polio') self.ct = CampaignType.objects.create(name='NID') self.ct_2 = CampaignType.objects.create(name='SNID') self.can_see_campaign = self.ts.create_arbitrary_campaign( campaign_type_id=self.ct.id, location_id=self.top_lvl_location.id, indicator_tag_id=self.it.id, name="can_see" ) self.can_see_campaign_2 = self.ts.create_arbitrary_campaign( campaign_type_id=self.ct_2.id, location_id=self.top_lvl_location.id, indicator_tag_id=self.it.id, name="can_see2" ) # self.can_not_see_campaign = self.ts.create_arbitrary_campaign( # campaign_type_id=self.ct.id, # location_id=self.not_allowed_to_see_location.id, # indicator_tag_id=self.it.id, # ) # # ### set the user permission ### # LocationPermission.objects.create(user_id=self.ts.user.id, # top_lvl_location_id=self.top_lvl_location.id) self.ts.get_credentials(self) ltr = LocationTreeCache() ltr.main() def test_campaign_get(self): resp = self.ts.get(self, '/api/v1/campaign/') self.assertHttpOK(resp) response_data = self.deserialize(resp) self.assertEqual(len(response_data['objects']), 2) def test_campaign_get_id_list(self): campaign_id_list = [self.can_see_campaign.id, self.can_see_campaign_2.id] data = {'id__in': str(campaign_id_list).strip('[]')} resp = self.ts.get(self, '/api/v1/campaign/', data=data) response_data = self.deserialize(resp) self.assertHttpOK(resp) self.assertEqual(len(response_data['objects']), 2) def test_campaign_get_id_list_invalid(self): data = {'id__in': 12345} resp = self.ts.get(self, '/api/v1/campaign/', data=data) response_data = self.deserialize(resp) self.assertHttpOK(resp) self.assertEqual(len(response_data['objects']), 0) def test_get_detail(self): detailURL = '/api/v1/campaign/{0}/'.format(self.can_see_campaign.id) resp = self.ts.get(self, detailURL) self.assertHttpOK(resp) response_data = self.deserialize(resp) self.assertEqual(self.can_see_campaign.name, response_data['name']) def test_get_detail_invalid_id(self): detailURL = '/api/v1/campaign/12345/' resp = self.ts.get(self, detailURL) self.assertHttpApplicationError(resp) def test_post_campaign(self): data = { 'name': 'something', 'campaign_type_id': self.ct.id, 'start_date': '2016-05-01', 'end_date': '2016-05-01' } resp = self.ts.post(self, '/api/v1/campaign/', data=data) response_data = self.deserialize(resp) self.assertHttpCreated(resp) self.assertEqual(response_data['name'], 'something') def test_post_campaign_missing_field(self): data = { 'start_date': '2016-05-01', 'end_date': '2016-05-01' } resp = self.ts.post(self, '/api/v1/campaign/', data=data) self.assertHttpApplicationError(resp) def _post_campaign_invalid_id(self): data = { 'name': 'something', 'campaign_type_id': -1232323231, 'start_date': '2016-05-01', 'end_date': '2016-05-01' } resp = self.ts.post(self, '/api/v1/campaign/', data=data) self.assertHttpApplicationError(resp)
class DocDataPointResourceTest(RhizomeApiTestCase): def setUp(self): ## instantiate the test client and all other methods ## super(DocDataPointResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Nigeria', location_name='Nigeria') def test_doc_dp_get(self): doc = Document.objects.create(doc_title="test") camp_type = CampaignType.objects.create(name="test") ind_tag = IndicatorTag.objects.create(tag_name="tag") campaign = self.ts.create_arbitrary_campaign(camp_type.id, self.top_lvl_location.id, ind_tag.id) ind = self.ts.create_arbitrary_indicator() value = 1123 doc_dp = DocDataPoint.objects.create( document_id=doc.id, indicator_id=ind.id, location_id=self.top_lvl_location.id, campaign_id=campaign.id, value=value, source_submission_id=2, agg_on_location=False) data = {'document_id': doc.id} url = '/api/v1/doc_datapoint/' resp = self.ts.get(self, url, data=data) response_data = self.deserialize(resp) self.assertHttpOK(resp) self.assertEqual(response_data['objects'][0]['value'], value) def test_doc_dp_get_no_param(self): doc = Document.objects.create(doc_title="test") camp_type = CampaignType.objects.create(name="test") ind_tag = IndicatorTag.objects.create(tag_name="tag") campaign = self.ts.create_arbitrary_campaign(camp_type.id, self.top_lvl_location.id, ind_tag.id) ind = self.ts.create_arbitrary_indicator() value = 1123 doc_dp = DocDataPoint.objects.create( document_id=doc.id, indicator_id=ind.id, location_id=self.top_lvl_location.id, campaign_id=campaign.id, value=value, source_submission_id=2, agg_on_location=False) url = '/api/v1/doc_datapoint/' resp = self.ts.get(self, url) self.deserialize(resp) self.assertHttpApplicationError(resp) def test_doc_dp_get_invalid_document_id(self): ''' Here we pass a bogus document_id to the API and ensure we do not receive any objects back from this query. ''' doc = Document.objects.create(doc_title="test") camp_type = CampaignType.objects.create(name="test") ind_tag = IndicatorTag.objects.create(tag_name="tag") campaign = self.ts.create_arbitrary_campaign(camp_type.id, self.top_lvl_location.id, ind_tag.id) ind = self.ts.create_arbitrary_indicator() value = 1123 doc_dp = DocDataPoint.objects.create( document_id=doc.id, indicator_id=ind.id, location_id=self.top_lvl_location.id, campaign_id=campaign.id, value=value, source_submission_id=2, agg_on_location=False) data = {'document_id': 1234} url = '/api/v1/doc_datapoint/' resp = self.ts.get(self, url, data=data) response_data = self.deserialize(resp) self.assertEqual(len(response_data['objects']), 0)
class DocDetailResourceTest(RhizomeApiTestCase): def setUp(self): super(DocDetailResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location( self.lt.id, location_code='Nigeria', location_name='Nigeria') def test_post_doc_detail(self): doc = Document.objects.create(doc_title='test') doc_detail_type = DocDetailType.objects.create(name='test type') url = '/api/v1/doc_detail/' value = 1232 data = { 'document_id': doc.id, 'doc_detail_type_id': doc_detail_type.id, 'doc_detail_value': value } resp = self.ts.post(self, url, data=data) self.assertHttpCreated(resp) response_data = self.deserialize(resp) self.assertEqual(response_data['doc_detail_value'], value) def test_get_doc_detail_by_type(self): doc = Document.objects.create(doc_title='test') doc_detail_type = DocDetailType.objects.create(name='test type') doc_detail = DocumentDetail.objects.create(document_id=doc.id, doc_detail_type_id=doc_detail_type.id, doc_detail_value=1 ) url = '/api/v1/doc_detail/' data = {'doc_detail_type': doc_detail_type.name} resp = self.ts.get(self, url, data=data) self.assertHttpOK(resp) response_data = self.deserialize(resp) self.assertEqual(len(response_data['objects']), 1) self.assertEqual(response_data['objects'][0]['id'], doc_detail.id) def test_get_doc_detail_by_id(self): doc = Document.objects.create(doc_title='test') doc_detail_type = DocDetailType.objects.create(name='test type') doc_detail = DocumentDetail.objects.create(document_id=doc.id, doc_detail_type_id=doc_detail_type.id, doc_detail_value=1 ) url = '/api/v1/doc_detail/' data = {'document_id': doc.id} resp = self.ts.get(self, url, data=data) self.assertHttpOK(resp) response_data = self.deserialize(resp) self.assertEqual(len(response_data['objects']), 1) self.assertEqual(response_data['objects'][0]['id'], doc_detail.id) def test_get_all_doc_detail(self): doc = Document.objects.create(doc_title='test') doc_2 = Document.objects.create(doc_title='test2') doc_detail_type = DocDetailType.objects.create(name='test type') doc_detail_1 = DocumentDetail.objects.create(document_id=doc.id, doc_detail_type_id=doc_detail_type.id, doc_detail_value=1 ) doc_detail_2 = DocumentDetail.objects.create(document_id=doc_2.id, doc_detail_type_id=doc_detail_type.id, doc_detail_value=1 ) url = '/api/v1/doc_detail/' resp = self.ts.get(self, url) self.assertHttpOK(resp) response_data = self.deserialize(resp) self.assertEqual(len(response_data['objects']), 2)
class DateDataPointResourceTest(RhizomeApiTestCase): # python manage.py test rhizome.tests.test_api_datapoint_groupby_date --settings=rhizome.settings.test def setUp(self): super(DateDataPointResourceTest, self).setUp() # Create a user. self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username,\ '*****@*****.**', self.password) self.lt = LocationType.objects.create(name='Country', admin_level=0) self.province_lt = LocationType.objects.create(name='Province'\ ,admin_level = 1) self.district_lt = LocationType.objects.create(name='District'\ ,admin_level = 2) self.ind = Indicator.objects.create(name='Polio Cases', short_name='Polio Cases', data_format='date_int') self.top_lvl_location = Location.objects.create( name='Afghanistan', location_code='Afghanistan', id=1234, location_type_id=self.lt.id, ) self.some_province = Location.objects.create( name='Province', location_code='Province', id=432, parent_location_id=self.top_lvl_location.id, location_type_id=self.province_lt.id) self.some_district = Location.objects.create( name='Achin', location_code='Achin', id=4321, parent_location_id=self.some_province.id, location_type_id=self.district_lt.id) ltc = LocationTreeCache() ltc.main() LocationPermission.objects.create(user_id = self.user.id,\ top_lvl_location_id = self.top_lvl_location.id) self.get_credentials() self.create_polio_cases() self.ts = TestSetupHelpers() self.doc_id = Document.objects.create(doc_title='Data Entry').id def create_polio_cases(self): df = pd.read_csv('rhizome/tests/_data/AfgPolioCases.csv') for ix, row in df.iterrows(): DataPoint.objects.create( location_id = self.some_district.id, indicator_id = self.ind.id, data_date = datetime.strptime(row.data_date, '%d-%m-%y'), value = 1, source_submission_id = 1, unique_index = str(self.some_district.id) + str(self.ind.id) +\ str(row.data_date) ) def get_credentials(self): result = self.api_client.client.login(username=self.username, password=self.password) return result def test_get_list(self): # python manage.py test rhizome.tests.test_api_datapoint_groupby_date.DateDataPointResourceTest.test_get_list --settings=rhizome.settings.test get = { 'group_by_time': 'year', 'indicator__in': self.ind.id, 'start_date': '2013-01-01', 'end_date': '2016-12-01', 'location_id': self.top_lvl_location.id, 'location_depth': 1 } resp = self.api_client.get('/api/v1/date_datapoint/', \ format='json', data=get, authentication=self.get_credentials()) response_data = self.deserialize(resp) self.assertHttpOK(resp) objects = response_data['objects'] meta = response_data['meta'] ## does the 'meta' object have what the FE needs self.assertEqual(self.ind.id, int(meta['indicator_ids'][0])) # self.assertEqual(self.top_lvl_location.id, int(meta['location_ids'][0])) ## WE SHOULD REMOVE THIS LOGIC FROM FE -- DATES ARE SEPARATE FROM CAMPAIGNS self.assertEqual(set(meta['time_groupings']), set([2014, 2015, 2016])) self.assertEqual(3, len(objects)) # one for each year # case_dict = {} for obj in objects: case_dict[obj['time_grouping']] = float(obj['value']) self.assertEqual(28.00, case_dict[2014]) self.assertEqual(20.00, case_dict[2015]) self.assertEqual(3.0, case_dict[2016]) # basic test to just get a datapoint at a location for which we have data def test_get_list_no_recursion(self): # python manage.py test rhizome.tests.test_api_datapoint_groupby_date.DateDataPointResourceTest.test_get_list_no_recursion --settings=rhizome.settings.test location_id = 4321 get = { 'group_by_time': 'all_time', 'indicator__in': self.ind.id, 'start_date': '2013-01-01', 'end_date': '2016-01-01', 'location_id': location_id, 'location_depth': 0 } resp = self.api_client\ .get('/api/v1/date_datapoint/', data = get, format = 'json', authentication = self.get_credentials()) self.assertHttpOK(resp) response_data = self.deserialize(resp) dps_all_time = DataPoint.objects.filter(indicator_id=self.ind.id) total_all_time = 0 for dp in dps_all_time: total_all_time += dp.value self.assertEqual(len(response_data['objects']), 1) self.assertEqual(int(response_data['objects'][0]['location_id']), location_id) # not sure if this is a bug or what, but start and end date seem to be irrelevant when using group_by_time def test_get_list_diff_start_end_dates(self): get = { 'group_by_time': 'year', 'indicator__in': self.ind.id, 'start_date': '2013-01-01', 'end_date': '2016-01-01', 'location_id__in': self.top_lvl_location.id, 'location_depth': 1 } resp = self.api_client.get('/api/v1/date_datapoint/', data=get, format='json', authentication=self.get_credentials()) self.assertHttpOK(resp) response_data = self.deserialize(resp) objects_1 = response_data['objects'] get_2 = { 'group_by_time': 'year', 'indicator__in': self.ind.id, 'start_date': '2016-01-01', 'end_date': '2016-01-01', 'location_id__in': self.top_lvl_location.id, 'location_depth': 1 } resp_2 = self.api_client.get('/api/v1/date_datapoint/',\ data = get_2, format='json',\ authentication=self.get_credentials()) self.assertHttpOK(resp_2) response_data_2 = self.deserialize(resp_2) objects_2 = response_data_2['objects'] self.assertEqual(len(objects_1), len(objects_2)) def test_get_list_quarter_and_all_time(self): get = { 'group_by_time': 'quarter', 'indicator__in': self.ind.id, 'start_date': '2013-01-01', 'end_date': '2016-07-01', 'location_id': self.top_lvl_location.id, 'location_depth': 1 } resp = self.api_client.get('/api/v1/date_datapoint/', \ data = get , format='json', authentication=self.get_credentials()) response_data = self.deserialize(resp) self.assertHttpOK(resp) dps_q1_2014 = DataPoint.objects.filter( data_date__range=('2014-01-01', '2014-03-31'),\ indicator = self.ind.id ) total = 0 for dp in dps_q1_2014: total += dp.value # find the total for q1 2014 q1_found = False for indicator in response_data['objects']: campaign = indicator['time_grouping'] if campaign == '20141': value = float(indicator['value']) self.assertEqual(value, total) q1_found = True self.assertTrue(q1_found) get_2 = { 'group_by_time': 'all_time', 'indicator__in': self.ind.id, 'start_date': '2013-01-01', 'end_date': '2016-07-01', 'location_id': self.top_lvl_location.id, 'location_depth': 1 } resp = self.api_client.get('/api/v1/date_datapoint/', \ data = get_2, format='json',\ authentication=self.get_credentials()) response_data = self.deserialize(resp) dps_all_time = DataPoint.objects.filter(indicator_id=self.ind.id) total_all_time = 0 for dp in dps_all_time: total_all_time += dp.value self.assertEqual(len(response_data['objects']), 1) self.assertEqual(float(response_data['objects'][0]['value']), total_all_time) # provide a non-existent id def test_get_list_bogus_id(self): get = { 'group_by_time': 'quarter', 'indicator__in': 3223, 'start_date': '2013-01-01', 'end_date': '2016-01-01', 'location_id__in': self.top_lvl_location.id, 'location_depth': 1 } resp = self.api_client.get('/api/v1/date_datapoint/', \ data = get, format='json', authentication=self.get_credentials()) self.assertHttpOK(resp) response_data = self.deserialize(resp) self.assertEqual(len(response_data['objects']), 0) def test_get_list_wrong_grouping(self): ''' What happens when we request an unsupported time grouping ''' # python manage.py test rhizome.tests.test_api_datapoint_groupby_date.DateDataPointResourceTest.test_get_list_wrong_grouping --settings=rhizome.settings.test get = { 'group_by_time': 'xxx', 'indicator__in': self.ind.id, 'start_date': '2013-01-01', 'end_date': '2016-01-01', 'location_id': self.top_lvl_location.id, 'location_depth': 1 } resp = self.api_client.get('/api/v1/date_datapoint/',data = get,\ format='json', authentication=self.get_credentials()) self.deserialize(resp) self.assertHttpApplicationError(resp) def _show_missing_data(self): ''' This test is not in the suite because for date_datapoint results, the back end should not be in charge of creating every possible datapoint wiht a null value in order to handle discontinuity. show_all_data should not be a parameter and we should remove this and handle the fallout in the front end charting library. ''' # python manage.py test rhizome.tests.test_api_datapoint_groupby_date.DateDataPointResourceTest.test_show_missing_data --settings=rhizome.settings.test rando_ind = Indicator.objects.create(name='some other damn indicator', short_name='we don\'t care!', data_format='int') rando_ind_2 = Indicator.objects.create(name='some other indicator', short_name='we don care!', data_format='int') # ind_list = [rando_ind.id, rando_ind_2.id] ind_list = '{0},{1}'.format(rando_ind.id, rando_ind_2.id) get = { 'group_by_time': 'year', 'indicator__in': ind_list, 'start_date': '2013-01-01', 'end_date': '2016-01-01', 'location_id__in': self.top_lvl_location.id, 'location_depth': 0, 'show_missing_data': 1 } resp = self.api_client.get('/api/v1/date_datapoint/', \ data = get , format='json',\ authentication=self.get_credentials()) response_data = self.deserialize(resp) self.assertHttpOK(resp) self.assertEqual(len(response_data['objects']), 6) ## should be one object for the location, for each Indicator ## for each location and each time grouping. ## 3 yrs * 2 indicators * one location = 6 ## if location_depth = 1, the number would have to take into ## account the number of sub locations one step under the parent def test_patch_date_datapoint(self): ''' create a datapoint with the ORM, submit a PATCH request and see if the value changed. If the user tries to change anything exept the value, there should be an error. ''' dp_to_patch = DataPoint.objects.all()[0] patch_data = {'value': 101.01} dp_url = '/api/v1/date_datapoint/%s/' % dp_to_patch.id ## submit the patch and make sure it has the proper response code resp = self.ts.patch(self, dp_url, data=patch_data) self.assertHttpAccepted(resp) ## now get the dp and see if the value has been is updated ## dp_to_patch = DataPoint.objects.get(id=dp_to_patch.id) self.assertEqual(dp_to_patch.value, patch_data['value']) def test_post_date_datapoint(self): ''' post a record to the datapoint table ''' indicator_id = Indicator.objects.all()[0].id location_id = Location.objects.all()[0].id val = 10.0 data = { 'indicator_id': indicator_id, 'data_date': '2016-01-01', 'location_id': location_id, 'value': val } resp = self.ts.post(self, '/api/v1/date_datapoint/', data) self.assertHttpCreated(resp) response_data = self.deserialize(resp) self.assertEqual(response_data['value'], val) another_date = '2016-02-02' data['data_date'] = another_date resp = self.ts.post(self, '/api/v1/date_datapoint/', data) self.assertHttpCreated(resp) response_data = self.deserialize(resp) self.assertEqual(response_data['data_date'], another_date) def test_post_date_datapoint_missing_data(self): ''' if we do not have all hte keys we need, throw an error ''' data = {'value': 10} resp = self.ts.post(self, '/api/v1/date_datapoint/', data) self.assertHttpApplicationError(resp) def test_post_date_datapoint_invalid_data(self): ''' The indicator, and campaign dont exists, the api should tell us ''' data = { # 'document_id': doc_id, 'indicator_id': 4324, 'time_grouping': 32132123, 'location_id': 4321, 'value': 10 } resp = self.ts.post(self, '/api/v1/date_datapoint/', data) self.assertHttpApplicationError(resp) response_data = self.deserialize(resp) def test_delete_date_datapoint(self): ''' create a datapoint, then delete it, make sure that it is no longer there. ''' dp = DataPoint.objects.all()[0] delete_url = '/api/v1/date_datapoint/%d/' % dp.id resp = self.ts.delete(self, delete_url) ## now make sure that it is not there # dpc_query = DataPoint.objects.filter(id=dp.id) self.assertEqual(len(dpc_query), 0) def test_get_date_datapoint_by_id(self): ''' Here we get one object from the API and ensure it has the proper data from when we inserted it. ''' dp_obj = DataPoint.objects.all()[0] resp = self.ts.get(self, '/api/v1/date_datapoint/%s/' % dp_obj.id) self.assertHttpOK(resp) response_data = self.deserialize(resp) self.assertEqual(response_data['value'], dp_obj.value)
class DocTransformResourceTest(RhizomeApiTestCase): # ./manage.py test rhizome.tests.test_api_doc_transform.DocTransformResourceTest.test_doc_transform --settings=rhizome.settings.test def setUp(self): super(DocTransformResourceTest, self).setUp() self.ts = TestSetupHelpers() self.ts.load_some_metadata() ltr = LocationTreeCache() ltr.main() self.mapped_location_id = self.ts.locations[0].id self.mapped_location_id_2 = self.ts.locations[1].id self.loc_map = SourceObjectMap.objects.create( source_object_code="AF001039003000000000", content_type="location", mapped_by_id=self.ts.user.id, master_object_id=self.mapped_location_id, ) source_campaign_string = "2016 March NID OPV" self.mapped_campaign_id = self.ts.campaigns[0].id self.campaign_map = SourceObjectMap.objects.create( source_object_code=source_campaign_string, content_type="campaign", mapped_by_id=self.ts.user.id, master_object_id=self.mapped_campaign_id, ) self.mapped_indicator_with_data = self.ts.locations[2].id self.indicator_map = SourceObjectMap.objects.create( source_object_code="Percent missed due to other reasons", content_type="indicator", mapped_by_id=self.ts.user.id, master_object_id=self.mapped_indicator_with_data, ) def test_doc_transform(self): # ./manage.py test rhizome.tests.test_api_doc_transform.DocTransformResourceTest.test_doc_transform --settings=rhizome.settings.test doc = self.ts.create_arbitrary_document(document_docfile="eoc_post_campaign.csv", file_type="campaign") get_data = {"document_id": doc.id} resp = self.ts.get(self, "/api/v1/transform_upload/", get_data) ss_list = SourceSubmission.objects.filter(document_id=doc.id).values_list("id", flat=True) self.assertHttpOK(resp) self.assertEqual(len(self.deserialize(resp)["objects"]), 1) self.assertEqual(DataPointComputed.objects.all()[0].value, 0.082670906) def test_data_date_transform(self): DataPoint.objects.all().delete() loc_map = SourceObjectMap.objects.create( source_object_code="AF001054001000000000", content_type="location", mapped_by_id=self.ts.user.id, master_object_id=self.mapped_location_id, ) self.mapped_indicator_with_data = self.ts.indicators[2].id self.indicator_map = SourceObjectMap.objects.create( source_object_code="polio_case", content_type="indicator", mapped_by_id=self.ts.user.id, master_object_id=self.mapped_indicator_with_data, ) doc = self.ts.create_arbitrary_document("AfgPolioCases.csv", file_type="date") get_data = {"document_id": doc.id, "file_type": "date_file"} resp = self.ts.get(self, "/api/v1/transform_upload/", get_data) self.assertHttpOK(resp) self.assertEqual(len(self.deserialize(resp)["objects"]), 1) # check out the date format in the test data -- `17-7-2015` data_date = datetime(2015, 07, 17, 0, 0) single_dp = DataPoint.objects.filter( location_id=self.mapped_location_id, indicator=self.mapped_indicator_with_data, data_date=data_date ) self.assertEqual(len(single_dp), 1) self.assertEqual(1, single_dp[0].value) all_achin_dps = DataPoint.objects.filter( location_id=self.mapped_location_id, indicator=self.mapped_indicator_with_data, data_date__gt="2000-01-01", data_date__lt="2020-01-01", ) self.assertEqual(len(all_achin_dps), 6) def test_doc_transform_with_zeros(self): doc = self.ts.create_arbitrary_document(document_docfile="zero_val_test.csv", file_type="campaign") get_data = {"document_id": doc.id} resp = self.ts.get(self, "/api/v1/transform_upload/", get_data) self.assertHttpOK(resp) self.assertEqual(len(self.deserialize(resp)["objects"]), 1) self.assertEqual(DataPointComputed.objects.all()[0].value, 0.0) def test_doc_transform_no_document_id(self): resp = self.ts.get(self, "/api/v1/transform_upload/") self.assertHttpApplicationError(resp) # datapoint should be overwritten for unique location/campaign/indicator def test_duplicate_datapoint_campaign(self): # upload document and run transform doc = self.ts.create_arbitrary_document(document_docfile="eoc_post_campaign.csv", file_type="campaign") get_data = {"document_id": doc.id} resp = self.ts.get(self, "/api/v1/transform_upload/", get_data) self.assertHttpOK(resp) self.assertEqual(DataPointComputed.objects.all()[0].value, 0.082670906) # upload and transform again: doc_2 = self.ts.create_arbitrary_document( document_docfile="eoc_post_campaign_2.csv", doc_title="eoc_post_campaign_2.csv", file_type="campaign" ) get_data_2 = {"document_id": doc_2.id} resp_2 = self.ts.get(self, "/api/v1/transform_upload/", get_data_2) self.assertHttpOK(resp_2) # the datapoint should be overwritten self.assertEqual(DataPoint.objects.count(), 1) # make sure we have the new value self.assertEqual(DataPointComputed.objects.all()[0].value, 0.9) # datapoint should be overwritten for unique location/data_date/indicator def test_duplicate_datapoint_data_date(self): # create required metadata loc_map = SourceObjectMap.objects.create( source_object_code="AF001054001000000000", content_type="location", mapped_by_id=self.ts.user.id, master_object_id=self.mapped_location_id, ) self.mapped_indicator_with_data = self.ts.indicators[2].id self.indicator_map = SourceObjectMap.objects.create( source_object_code="polio_case", content_type="indicator", mapped_by_id=self.ts.user.id, master_object_id=self.mapped_indicator_with_data, ) doc = self.ts.create_arbitrary_document( document_docfile="AfgPolioCases.csv", doc_title="AfgPolioCases.csv", file_type="date" ) get_data = {"document_id": doc.id} resp = self.ts.get(self, "/api/v1/transform_upload/", get_data) self.assertHttpOK(resp) cases = DataPoint.objects.filter(location_id=self.mapped_location_id, indicator=self.mapped_indicator_with_data) self.assertEqual(len(cases), 6) # do it again, the case count should be 6 not 12 doc = self.ts.create_arbitrary_document( document_docfile="AfgPolioCases_2.csv", doc_title="AfgPolioCases_2.csv", file_type="date" ) get_data = {"document_id": doc.id} resp = self.ts.get(self, "/api/v1/transform_upload/", get_data) cases_2 = DataPoint.objects.filter( location_id=self.mapped_location_id, indicator=self.mapped_indicator_with_data ) self.assertEqual(len(cases_2), 6) sum_of_cases = sum([dp.value for dp in cases_2]) self.assertEqual(6, sum_of_cases) def _class_indicator(self): # create required metadata self.mapped_indicator_with_data = self.ts.indicators[2].id loc_map = SourceObjectMap.objects.create( source_object_code="AF001039006000000000", content_type="location", mapped_by_id=self.ts.user.id, master_object_id=self.mapped_location_id_2, ) self.indicator_map = SourceObjectMap.objects.create( source_object_code="LQAS", content_type="indicator", mapped_by_id=self.ts.user.id, master_object_id=self.mapped_indicator_with_data, ) # IndicatorClassMap.objects.create( # indicator_id=self.mapped_indicator_with_data, # string_value='pass', # is_display=True # ) doc = self.ts.create_arbitrary_document( file_type="campaign", document_docfile="lqas_test.csv", doc_title="lqas_test.csv" ) get_data = {"document_id": doc.id} resp = self.ts.get(self, "/api/v1/transform_upload/", get_data) self.deserialize(resp) dp_count = DataPoint.objects.count() self.assertEqual(dp_count, 1)
class IndicatorTagResourceTest(RhizomeApiTestCase): def setUp(self): super(IndicatorTagResourceTest, self).setUp() self.ts = TestSetupHelpers() self.lt = self.ts.create_arbitrary_location_type() self.top_lvl_location = self.ts.create_arbitrary_location(self.lt.id) def test_get_indicator_tag_id(self): tag_name = 'tag1' ind_tag_0 = IndicatorTag.objects.create(tag_name=tag_name) data = {'id': ind_tag_0.id} resp = self.ts.get(self, '/api/v1/indicator_tag/', data=data) response_data = self.deserialize(resp) self.assertHttpOK(resp) self.assertEqual(response_data['objects'][0]['tag_name'], tag_name) def test_get_indicator_tag_show_leaf(self): tag_name_0 = 'tag1' ind_tag_0 = IndicatorTag.objects.create(tag_name=tag_name_0) tag_name_1 = 'tag2' ind_tag_1 = IndicatorTag.objects.create(tag_name=tag_name_1, parent_tag_id=ind_tag_0.id) data = {'show_leaf': 1} resp = self.ts.get(self, '/api/v1/indicator_tag/', data=data) response_data = self.deserialize(resp) self.assertHttpOK(resp) self.assertEqual(len(response_data['objects']), 1) self.assertEqual(response_data['objects'][0]['tag_name'], tag_name_1) def test_create_indicator_tag(self): tag_name_1 = 'test1' data_1 = {'tag_name': tag_name_1} resp = self.ts.post(self, '/api/v1/indicator_tag/', data=data_1) response_data = self.deserialize(resp) self.assertHttpCreated(resp) self.assertEqual(response_data['tag_name'], tag_name_1) tag_1_id = int(response_data['id']) # now test creating another tag, with tag_1 as its parent tag_name_2 = 'test2' data_2 = {'tag_name': tag_name_2, 'parent_tag_id': tag_1_id} resp = self.ts.post(self, '/api/v1/indicator_tag/', data=data_2) response_data = self.deserialize(resp) self.assertHttpCreated(resp) self.assertEqual(response_data['tag_name'], tag_name_2) self.assertEqual(response_data['parent_tag_id'], tag_1_id) def test_create_indicator_tag_no_vals(self): resp = self.ts.post(self, '/api/v1/indicator_tag/') self.deserialize(resp) self.assertHttpApplicationError(resp) def test_get_indicator_tag_no_params(self): tag_name_0 = 'tag1' ind_tag_0 = IndicatorTag.objects.create(tag_name=tag_name_0) tag_name_1 = 'tag2' ind_tag_1 = IndicatorTag.objects.create(tag_name=tag_name_1, parent_tag_id=ind_tag_0.id) resp = self.ts.get(self, '/api/v1/indicator_tag/') response_data = self.deserialize(resp) self.assertHttpOK(resp) self.assertEqual(len(response_data['objects']), 2) def test_update_tag(self): IndicatorTag.objects.all().delete() tag = IndicatorTag.objects.create( id=None, tag_name='Test Tag Name', ) self.assertEqual(IndicatorTag.objects.count(), 1) new_tag_name = "New Tag Name" patch_data = {"tag_name": new_tag_name} resp = self.api_client.patch('/api/v1/indicator_tag/%s/' % tag.id\ , format='json', data=patch_data\ , authentication=self.ts.get_credentials(self)) response_data = self.deserialize(resp) self.assertHttpAccepted(resp) self.assertEqual(tag.id, response_data['id']) self.assertEqual(IndicatorTag.objects.count(), 1) self.assertEqual(new_tag_name, response_data['tag_name']) def test_remove_tag(self): indicatior = Indicator.objects.create(\ short_name='Test Indicator', name='Test Indicator for the Tag', data_format='int', description='Test Indicator for the Tag Description') tag = IndicatorTag.objects.create(tag_name='Test tag') IndicatorToTag.objects.all().delete() indicatior_tag = IndicatorToTag.objects.create( indicator_id=indicatior.id, indicator_tag_id=tag.id) self.assertEqual(IndicatorToTag.objects.count(), 1) delete_url = '/api/v1/indicator_to_tag/%s/' % str(indicatior_tag.id) self.api_client.delete(delete_url, format='json', data={}, authentication=self.ts.get_credentials(self)) self.assertEqual(IndicatorToTag.objects.count(), 0)