def test_add_references(self): collection = OrganizationCollectionFactory() self.assertEqual(collection.concepts.count(), 0) self.assertEqual(collection.references.count(), 0) self.assertEqual(collection.active_concepts, 0) source = OrganizationSourceFactory() concept = ConceptFactory(parent=source, sources=[source]) concept_expression = concept.uri collection.add_references([concept_expression]) collection.refresh_from_db() self.assertEqual(collection.concepts.count(), 1) self.assertEqual(collection.references.count(), 1) self.assertEqual(collection.references.first().expression, concept.get_latest_version().uri) self.assertEqual(collection.concepts.first(), concept.get_latest_version()) self.assertEqual(collection.active_concepts, 1) result = collection.add_references([concept_expression]) self.assertEqual( result, { concept_expression: [ 'Concept or Mapping reference name must be unique in a collection.' ] }) collection.refresh_from_db() self.assertEqual(collection.concepts.count(), 1) self.assertEqual(collection.references.count(), 1) self.assertEqual(collection.active_concepts, 1)
def test_export_collection(self, s3_mock): # pylint: disable=too-many-locals s3_mock.url_for = Mock(return_value='https://s3-url') s3_mock.upload_file = Mock() source = OrganizationSourceFactory() concept1 = ConceptFactory(parent=source) concept2 = ConceptFactory(parent=source) mapping = MappingFactory(from_concept=concept2, to_concept=concept1, parent=source) collection = OrganizationCollectionFactory() collection.add_references([concept1.uri, concept2.uri, mapping.uri]) collection.refresh_from_db() export_collection(collection.id) # pylint: disable=no-value-for-parameter latest_temp_dir = get_latest_dir_in_path('/tmp/') zipped_file = zipfile.ZipFile(latest_temp_dir + '/export.zip') exported_data = json.loads( zipped_file.read('export.json').decode('utf-8')) self.assertEqual( exported_data, { **CollectionDetailSerializer(collection).data, 'concepts': ANY, 'mappings': ANY }) exported_concepts = exported_data['concepts'] expected_concepts = ConceptVersionDetailSerializer( [concept2.get_latest_version(), concept1.get_latest_version()], many=True).data self.assertEqual(len(exported_concepts), 2) self.assertIn(expected_concepts[0], exported_concepts) self.assertIn(expected_concepts[1], exported_concepts) exported_mappings = exported_data['mappings'] expected_mappings = MappingDetailSerializer( [mapping.get_latest_version()], many=True).data self.assertEqual(len(exported_mappings), 1) self.assertEqual(expected_mappings, exported_mappings) s3_upload_key = collection.export_path s3_mock.upload_file.assert_called_once_with(key=s3_upload_key, file_path=latest_temp_dir + '/export.zip', binary=True) s3_mock.url_for.assert_called_once_with(s3_upload_key) import shutil shutil.rmtree(latest_temp_dir)
def test_put_200(self): coll = OrganizationCollectionFactory(mnemonic='coll1', name='Collection') user = UserProfileFactory(organizations=[coll.organization]) self.assertEqual(coll.versions.count(), 1) response = self.client.put(coll.uri, dict(name='Collection1'), HTTP_AUTHORIZATION='Token ' + user.get_token(), format='json') self.assertEqual(response.status_code, 200) self.assertEqual(response.data['name'], 'Collection1') coll.refresh_from_db() self.assertEqual(coll.name, 'Collection1') self.assertEqual(coll.versions.count(), 1)
def test_org_active_inactive_should_affect_children(self): org = OrganizationFactory(is_active=True) source = OrganizationSourceFactory(organization=org, is_active=True) collection = OrganizationCollectionFactory(organization=org, is_active=True) org.is_active = False org.save() source.refresh_from_db() collection.refresh_from_db() self.assertFalse(org.is_active) self.assertFalse(source.is_active) self.assertFalse(collection.is_active) org.is_active = True org.save() source.refresh_from_db() collection.refresh_from_db() self.assertTrue(org.is_active) self.assertTrue(source.is_active) self.assertTrue(collection.is_active)
def test_user_active_inactive_should_affect_children(self): user = UserProfileFactory(is_active=True) source = OrganizationSourceFactory(user=user, is_active=True) collection = OrganizationCollectionFactory(user=user, is_active=True) user.is_active = False user.save() source.refresh_from_db() collection.refresh_from_db() self.assertFalse(user.is_active) self.assertFalse(source.is_active) self.assertFalse(collection.is_active) user.is_active = True user.save() source.refresh_from_db() collection.refresh_from_db() self.assertTrue(user.is_active) self.assertTrue(source.is_active) self.assertTrue(collection.is_active)