Implement a encoded search to json-ld wrapper.
[htsworkflow.git] / htsworkflow / submission / test / test_encoded.py
index e5b270795fcb1338746fd0647936051c657d9ab7..675d944a4bc0bb33e1e1b78be7ba4608da64d511 100644 (file)
@@ -3,7 +3,10 @@ import os
 from pprint import pprint
 from unittest2 import TestCase, TestSuite, defaultTestLoader, skip
 
-from htsworkflow.submission.encoded import ENCODED
+from htsworkflow.submission.encoded import (ENCODED,
+     ENCODED_CONTEXT,
+     ENCODED_NAMESPACES
+)
 
 class TestEncoded(TestCase):
     def test_prepare_url(self):
@@ -56,6 +59,21 @@ class TestEncoded(TestCase):
         encode.validate(obj)
         self.assertTrue('@id' in obj)
 
+    def test_create_context(self):
+        linked_id = {'@type': '@id'}
+        library = { '@id': '/libraries/1234', '@type': ['library', 'item'] }
+
+        encode = ENCODED('test.encodedcc.org')
+        url = encode.prepare_url(library['@id'])
+        context = encode.create_jsonld_context(library, url)
+        self.assertEqual(context['@vocab'], 'http://test.encodedcc.org/profiles/library.json#')
+        self.assertEqual(context['award'], linked_id )
+        self._verify_context(context, 'library')
+        # namespaces not added yet.
+        self.assertRaises(AssertionError, self._verify_namespaces, context)
+        encode.add_jsonld_namespaces(context)
+        self._verify_namespaces(context)
+
     def test_add_context(self):
         """Checking to make sure nested @base and @vocab urls are set correctly
         """
@@ -93,11 +111,72 @@ class TestEncoded(TestCase):
 
         url = encode.prepare_url('/libraries/ENCLB044ZZZ/?format=json&embed=False')
         schema_url = encode.get_schema_url(obj)
-        encode.add_jsonld_context(obj, encode.context, url)
+        encode.add_jsonld_context(obj, url)
 
         self.assertEqual(obj['biosample']['@context']['@base'], bio_base)
         self.assertEqual(obj['@context']['@vocab'], schema_url)
+        self._verify_context(obj['@context'], 'library')
+        self._verify_namespaces(obj['@context'])
+        self._verify_context(obj['biosample']['@context'], 'biosample')
+        self.assertEqual(obj['@context']['rdf'], 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
+        self.assertEqual(obj['@context']['OBO'], 'http://purl.obolibrary.org/obo/')
+
+
+    def test_convert_search_to_jsonld(self):
+        example = {'count': {'biosamples': 2},
+                   'portal_title': 'ENCODE',
+                   'title': 'Search',
+                   'notification': 'Success',
+                   'filters': [],
+                   '@id': '/search/?searchTerm=wold',
+                   '@type': ['search'],
+                   'facets': [],
+                    '@graph': [{
+                    u'@id': u'/biosamples/ENCBS125ENC/',
+                    u'@type': [u'biosample', u'item'],
+                    u'accession': u'ENCBS125ENC',
+                    u'award.rfa': u'ENCODE2-Mouse',
+                    u'biosample_term_name': u'myocyte',
+                    u'biosample_type': u'in vitro differentiated cells',
+                    u'characterizations.length': [],
+                    u'constructs.length': [],
+                    u'lab.title': u'Barbara Wold, Caltech',
+                    u'life_stage': u'unknown',
+                    u'organism.name': u'mouse',
+                    u'source.title': u'Barbara Wold',
+                    u'status': u'CURRENT',
+                    u'treatments.length': []},
+                    {u'@id': u'/biosamples/ENCBS126ENC/',
+                    u'@type': [u'biosample', u'item'],
+                    u'accession': u'ENCBS126ENC',
+                    u'award.rfa': u'ENCODE2-Mouse',
+                    u'biosample_term_name': u'myocyte',
+                    u'biosample_type': u'in vitro differentiated cells',
+                    u'characterizations.length': [],
+                    u'constructs.length': [],
+                    u'lab.title': u'Barbara Wold, Caltech',
+                    u'life_stage': u'unknown',
+                    u'organism.name': u'mouse',
+                    u'source.title': u'Barbara Wold',
+                    u'status': u'CURRENT',
+                    u'treatments.length': []},
+                    ]}
+
+        encode = ENCODED('test.encodedcc.org')
+        result = encode.convert_search_to_jsonld(example)
+        for obj in result['@graph']:
+            self.assertNotIn('award.rfa', obj)
+
+    def _verify_context(self, context, obj_type):
+        for context_key in [None, obj_type]:
+            for k in ENCODED_CONTEXT[context_key]:
+                self.assertIn(k, context)
+                self.assertEqual(ENCODED_CONTEXT[context_key][k], context[k])
 
+    def _verify_namespaces(self, context):
+        for k in ENCODED_NAMESPACES:
+            self.assertIn(k, context)
+            self.assertEqual(ENCODED_NAMESPACES[k], context[k])
 
 def suite():
     suite = TestSuite()