def test_add_several_organizations(self): """ Test that the query_count of bulk_add_organizations does not increase when given more organizations. """ existing_org = api.add_organization( self.make_organization_data("existing_org")) api.remove_organization( api.add_organization( self.make_organization_data("org_to_reactivate"))["id"]) # 1 query to load list of existing orgs, # 1 query to filter for only inactive existing orgs, # 1 query for activate-existing, and 1 query for create-new. with self.assertNumQueries(4): api.bulk_add_organizations([ existing_org, existing_org, existing_org, self.make_organization_data("org_to_reactivate"), self.make_organization_data("new_org_1"), self.make_organization_data("new_org_2"), self.make_organization_data("new_org_3"), self.make_organization_data("new_org_4"), self.make_organization_data("new_org_5"), self.make_organization_data("new_org_6"), self.make_organization_data("new_org_7"), self.make_organization_data("new_org_8"), self.make_organization_data("new_org_9"), self.make_organization_data("new_org_9"), # Redundant. self.make_organization_data("new_org_9"), # Redundant. ]) assert len(api.get_organizations()) == 11
def test_add_organization_inactive_to_active(self): """ Unit Test: test_add_organization_inactive_organization_present""" organization_data = { 'name': 'local_organizationßßß', 'description': 'Local Organization Descriptionßßß' } organization = api.add_organization(organization_data) self.assertGreater(organization['id'], 0) api.remove_organization(organization['id']) with self.assertNumQueries(3): organization = api.add_organization(organization_data)
def test_add_organization_inactive_to_active(self): """ Unit Test: test_add_organization_inactive_organization_present""" organization_data = { "name": "local_organizationßßß", "description": "Local Organization Descriptionßßß", } organization = api.add_organization(organization_data) self.assertGreater(organization["id"], 0) api.remove_organization(organization["id"]) with self.assertNumQueries(3): organization = api.add_organization(organization_data)
def test_remove_organization_bogus_organization(self): """ Unit Test: test_remove_organization_bogus_organization """ with self.assertNumQueries(3): api.remove_organization(self.test_organization['id']) with self.assertRaises(exceptions.InvalidOrganizationException): api.get_organization(self.test_organization['id']) # Do it again with the valid id to hit the exception workflow with self.assertNumQueries(2): api.remove_organization(self.test_organization['id']) with self.assertRaises(exceptions.InvalidOrganizationException): api.get_organization(self.test_organization['id'])
def test_add_organization_inactive_to_active(self): """ Unit Test: test_add_organization_inactive_to_active""" organization_data = { 'name': 'local_organizationßßß', 'description': 'Local Organization Descriptionßßß' } organization = api.add_organization(organization_data) self.assertGreater(organization['id'], 0) api.remove_organization(organization['id']) with self.assertNumQueries(5): organization = api.add_organization(organization_data) # Assert that the organization is active by making sure we can load it. loaded_organization = api.get_organization(organization['id']) self.assertEqual(loaded_organization['name'], organization_data['name'])
def test_remove_organization(self): """ Unit Test: test_remove_organization """ with self.assertNumQueries(3): api.remove_organization(self.test_organization['id']) with self.assertRaises(exceptions.InvalidOrganizationException): api.get_organization(self.test_organization['id'])
def test_edge_cases(self, mock_log_info): """ Test that bulk_add_organizations handles a few edge cases as expected. """ # Add three orgs, and remove all but the first. # Use capitalized name to confirm case insensitivity when checking # for existing orgs. api.add_organization(self.make_organization_data("EXISTING_ORG")) api.remove_organization( api.add_organization( self.make_organization_data("org_to_reactivate"))["id"]) api.remove_organization( api.add_organization( self.make_organization_data("org_to_leave_inactive"))["id"]) # 1 query to load list of existing orgs, # 1 query to filter for only inactive existing orgs, # 1 query for create, and 1 query for update. with self.assertNumQueries(4): api.bulk_add_organizations([ # New organization. self.make_organization_data("org_X"), # Modify existing active organization; should be no-op. { **self.make_organization_data("existing_org"), "description": "this name should be ignored" }, # Deleted organizations are still stored in the DB as "inactive". # Bulk-adding should reactivate it. self.make_organization_data("org_to_reactivate"), # Another new organizaiton. self.make_organization_data("org_Y"), # Another org with same short name (case-insensitively) # as first new organization; should be ignored. { **self.make_organization_data("ORG_x"), "name": "this name should be ignored" } ]) # There should exist the already-existing org, the org that existed as inactive # but is not activated, and the two new orgs. # This should not include `org_to_leave_inactive`. organizations = api.get_organizations() assert {organization["short_name"] for organization in organizations } == {"EXISTING_ORG", "org_to_reactivate", "org_X", "org_Y"} # Organization dicts with already-taken short_names shouldn't have modified # the existing orgs. assert "this name should be ignored" not in { organization["name"] for organization in organizations } # Based on logging messages, make sure we dropped the appropriate # organization dict from the bulk-add batch. logging_of_drop_from_batch = mock_log_info.call_args_list[0][0] assert logging_of_drop_from_batch[1]["short_name"] == ( # We dropped this org data: self.make_organization_data("ORG_x")["short_name"]) assert logging_of_drop_from_batch[2]["short_name"] == ( # in favor of this org data, which came earlier in the batch. self.make_organization_data("org_X")["short_name"]) # Based on logging messages, make sure the expected breakdown of # created vs. reactivated vs. not touched # is true for the organizations passed to `bulk_add_organizations`. logged_orgs_to_reactivate = mock_log_info.call_args_list[1][0][2] assert set(logged_orgs_to_reactivate) == {"org_to_reactivate"} logged_orgs_to_create = mock_log_info.call_args_list[2][0][2] assert set(logged_orgs_to_create) == {"org_X", "org_Y"}