class CollectionVersionListViewTest(OCLAPITestCase): def setUp(self): super().setUp() self.user = UserProfileFactory() self.token = self.user.get_token() self.collection = UserCollectionFactory(mnemonic='coll', user=self.user) self.concept = ConceptFactory() self.reference = CollectionReference(expression=self.concept.uri) self.reference.full_clean() self.reference.save() self.collection.references.add(self.reference) self.collection.concepts.set(self.reference.concepts) self.assertEqual(self.collection.references.count(), 1) self.assertEqual(self.collection.concepts.count(), 1) def test_get_200(self): response = self.client.get('/collections/coll/versions/?verbose=true', HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['version'], 'HEAD') UserCollectionFactory(mnemonic=self.collection.mnemonic, user=self.user, version='v1', released=True) response = self.client.get('/collections/coll/versions/?released=true', HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['version'], 'v1') @patch('core.collections.views.export_collection') def test_post_201(self, export_collection_mock): export_collection_mock.delay = Mock() response = self.client.post('/collections/coll/versions/', dict(id='v1', description='version1'), HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 201) self.assertEqual(response.data['version'], 'v1') self.assertEqual(self.collection.versions.count(), 2) last_created_version = self.collection.versions.order_by( 'created_at').last() self.assertEqual(last_created_version.version, 'v1') self.assertEqual(last_created_version.description, 'version1') self.assertEqual(last_created_version.concepts.count(), 1) self.assertEqual(last_created_version.references.count(), 1) self.assertEqual(last_created_version, self.collection.get_latest_version()) export_collection_mock.delay.assert_called_once_with( str(last_created_version.id))
def test_get_200(self): coll = OrganizationCollectionFactory(mnemonic='coll1') response = self.client.get('/collections/', format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['short_code'], 'coll1') self.assertEqual(response.data[0]['id'], 'coll1') self.assertEqual(response.data[0]['url'], coll.uri) response = self.client.get('/orgs/{}/collections/?verbose=true'.format( coll.parent.mnemonic), format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['short_code'], 'coll1') self.assertEqual(response.data[0]['id'], 'coll1') self.assertEqual(response.data[0]['url'], coll.uri) concept = ConceptFactory() reference = CollectionReference(expression=concept.uri) reference.full_clean() reference.save() coll.references.add(reference) coll.concepts.set(reference.concepts) response = self.client.get( '/orgs/{}/collections/?contains={}&includeReferences=true'.format( coll.parent.mnemonic, concept.uri), format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1)
def test_reference_as_concept_version(self): concept = ConceptFactory() expression = concept.uri reference = CollectionReference(expression=expression) reference.full_clean() self.assertEqual(len(reference.concepts), 1) self.assertTrue(isinstance(reference.concepts[0], Concept)) self.assertEqual(reference.concepts[0].id, concept.id)
def test_get_200(self): coll = OrganizationCollectionFactory(mnemonic='coll1') response = self.client.get( '/collections/', format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['short_code'], 'coll1') self.assertEqual(response.data[0]['id'], 'coll1') self.assertEqual(response.data[0]['url'], coll.uri) response = self.client.get( '/orgs/{}/collections/?verbose=true'.format(coll.parent.mnemonic), format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['short_code'], 'coll1') self.assertEqual(response.data[0]['id'], 'coll1') self.assertEqual(response.data[0]['url'], coll.uri) for attr in ['active_concepts', 'active_mappings', 'versions', 'summary']: self.assertFalse(attr in response.data[0]) concept = ConceptFactory() reference = CollectionReference(expression=concept.uri) reference.full_clean() reference.save() coll.references.add(reference) coll.concepts.set(reference.concepts) response = self.client.get( '/orgs/{}/collections/?contains={}&includeReferences=true'.format( coll.parent.mnemonic, concept.get_latest_version().uri ), format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) response = self.client.get( '/orgs/{}/collections/?verbose=true&includeSummary=true'.format(coll.parent.mnemonic), format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['short_code'], 'coll1') self.assertEqual(response.data[0]['id'], 'coll1') self.assertEqual(response.data[0]['url'], coll.uri) self.assertEqual(response.data[0]['summary'], dict(versions=1, active_concepts=1, active_mappings=0))
def test_invalid_expression(self): reference = CollectionReference(expression='') with self.assertRaises(ValidationError) as ex: reference.full_clean() self.assertEqual( ex.exception.message_dict, { 'expression': ['This field cannot be blank.'], 'detail': ['Expression specified is not valid.'] })
class CollectionReferencesViewTest(OCLAPITestCase): def setUp(self): super().setUp() self.user = UserProfileFactory() self.token = self.user.get_token() self.collection = UserCollectionFactory(mnemonic='coll', user=self.user) self.concept = ConceptFactory() self.reference = CollectionReference(expression=self.concept.uri) self.reference.full_clean() self.reference.save() self.collection.references.add(self.reference) self.collection.concepts.set(self.reference.concepts) self.assertEqual(self.collection.references.count(), 1) self.assertEqual(self.collection.concepts.count(), 1) def test_get_404(self): response = self.client.get('/collections/foobar/references/', format='json') self.assertEqual(response.status_code, 404) def test_get_200(self): response = self.client.get('/collections/coll/references/', format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['expression'], self.concept.get_latest_version().uri) self.assertEqual(response.data[0]['reference_type'], 'concepts') response = self.client.get( '/collections/coll/references/?q={}&search_sort=desc'.format( self.concept.get_latest_version().uri), format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['expression'], self.concept.get_latest_version().uri) self.assertEqual(response.data[0]['reference_type'], 'concepts') response = self.client.get( '/collections/coll/references/?q=/concepts/&search_sort=desc', format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) response = self.client.get( '/collections/coll/references/?q=/mappings/&search_sort=desc', format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 0) def test_delete_400(self): response = self.client.delete('/collections/coll/references/', HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 400) def test_delete_204_random(self): response = self.client.delete('/collections/coll/references/', dict(expressions=['/foo/']), HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 204) self.assertEqual(self.collection.references.count(), 1) self.assertEqual(self.collection.concepts.count(), 1) def test_delete_204_all_expressions(self): response = self.client.delete('/collections/coll/references/', dict(expressions='*'), HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 204) self.assertEqual(self.collection.references.count(), 0) self.assertEqual(self.collection.concepts.count(), 0) def test_delete_204_specific_expression(self): response = self.client.delete( '/collections/coll/references/', dict(expressions=[self.concept.get_latest_version().uri]), HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 204) self.assertEqual(self.collection.references.count(), 0) self.assertEqual(self.collection.concepts.count(), 0) concept = ConceptFactory() latest_version = concept.get_latest_version() MappingFactory(from_concept=latest_version, parent=concept.parent) response = self.client.put( '/collections/coll/references/?cascade=sourcemappings', dict(data=dict(mappings=[latest_version.uri])), HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 200) self.collection.refresh_from_db() self.assertEqual(self.collection.references.count(), 2) self.assertEqual(self.collection.concepts.count(), 1) self.assertEqual(self.collection.mappings.count(), 1) response = self.client.delete( '/collections/coll/references/?cascade=sourcemappings', dict(expressions=[latest_version.uri]), HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 204) self.collection.refresh_from_db() self.assertEqual(self.collection.references.count(), 0) self.assertEqual(self.collection.concepts.count(), 0) self.assertEqual(self.collection.mappings.count(), 0) @patch('core.collections.views.add_references') def test_put_202_all(self, add_references_mock): add_references_mock.delay = Mock() response = self.client.put('/collections/coll/references/', dict(data=dict(concepts='*')), HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 202) self.assertEqual(response.data, []) add_references_mock.delay.assert_called_once_with( self.user, dict(concepts='*'), self.collection, 'http://testserver', False) def test_put_200_specific_expression(self): response = self.client.put( '/collections/coll/references/', dict(data=dict(concepts=[self.concept.uri])), HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(response.data, [ dict( added=False, expression=self.concept.uri, message=[ 'Concept or Mapping reference name must be unique in a collection.' ]) ]) concept2 = ConceptFactory() response = self.client.put('/collections/coll/references/', dict(data=dict(concepts=[concept2.uri])), HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 200) self.collection.refresh_from_db() self.assertEqual(self.collection.references.count(), 2) self.assertEqual(self.collection.concepts.count(), 2) self.assertEqual(self.collection.active_concepts, 2) self.assertEqual(self.collection.active_mappings, 0) self.assertTrue( self.collection.references.filter( expression=concept2.get_latest_version().uri).exists()) self.assertEqual(response.data, [ dict( added=True, expression=concept2.get_latest_version().uri, message= 'Added the latest versions of concept to the collection. Future updates will not be added' ' automatically.') ]) mapping = MappingFactory(from_concept=concept2, to_concept=self.concept, parent=self.concept.parent) response = self.client.put('/collections/coll/references/', dict(data=dict(mappings=[mapping.uri])), HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 200) self.collection.refresh_from_db() self.assertEqual(self.collection.references.count(), 3) self.assertEqual(self.collection.concepts.count(), 2) self.assertEqual(self.collection.mappings.count(), 1) self.assertEqual(self.collection.active_concepts, 2) self.assertEqual(self.collection.active_mappings, 1) self.assertTrue( self.collection.references.filter( expression=mapping.get_latest_version().uri).exists()) self.assertEqual(response.data, [ dict( added=True, expression=mapping.get_latest_version().uri, message= 'Added the latest versions of mapping to the collection. Future updates will not be added' ' automatically.') ]) concept3 = ConceptFactory() latest_version = concept3.get_latest_version() mapping2 = MappingFactory(from_concept=latest_version, parent=concept3.parent) response = self.client.put( '/collections/coll/references/?cascade=sourcemappings', dict(data=dict(concepts=[latest_version.uri])), HTTP_AUTHORIZATION='Token ' + self.token, format='json') self.assertEqual(response.status_code, 200) self.collection.refresh_from_db() self.assertEqual(self.collection.references.count(), 5) self.assertEqual(self.collection.concepts.count(), 3) self.assertEqual(self.collection.mappings.count(), 2) self.assertEqual(self.collection.active_concepts, 3) self.assertEqual(self.collection.active_mappings, 2) self.assertTrue( self.collection.references.filter( expression=mapping2.get_latest_version().uri).exists()) self.assertTrue( self.collection.references.filter( expression=latest_version.uri).exists())