7 import simplejson as json
9 from django.test import TestCase
11 from htsworkflow.frontend.samples.models import \
17 from htsworkflow.frontend.samples.views import \
21 from htsworkflow.frontend.auth import apidata
22 from htsworkflow.util.conversion import unicode_or_none
25 class LibraryTestCase(TestCase):
26 fixtures = ['test_samples.json']
31 def testOrganism(self):
32 self.assertEquals(self.library_10001.organism(), 'human')
34 def testAffiliations(self):
35 self.library_10001.affiliations.add(self.affiliation_alice)
36 self.library_10002.affiliations.add(
37 self.affiliation_alice,
40 self.failUnless(len(self.library_10001.affiliations.all()), 1)
41 self.failUnless(self.library_10001.affiliation(), 'Alice')
43 self.failUnless(len(self.library_10002.affiliations.all()), 2)
44 self.failUnless(self.library_10001.affiliation(), 'Alice, Bob')
47 class SampleWebTestCase(TestCase):
49 Test returning data from our database in rest like ways.
50 (like returning json objects)
52 fixtures = ['test_samples.json']
54 def test_library_info(self):
55 for lib in Library.objects.all():
56 lib_dict = library_dict(lib.id)
57 url = '/samples/library/%s/json' % (lib.id,)
58 lib_response = self.client.get(url, apidata)
59 self.failUnlessEqual(lib_response.status_code, 200)
60 lib_json = json.loads(lib_response.content)
62 for d in [lib_dict, lib_json]:
63 # amplified_from_sample is a link to the library table,
64 # I want to use the "id" for the data lookups not
65 # the embedded primary key.
66 # It gets slightly confusing on how to implement sending the right id
67 # since amplified_from_sample can be null
68 #self.failUnlessEqual(d['amplified_from_sample'], lib.amplified_from_sample)
69 self.failUnlessEqual(d['antibody_id'], lib.antibody_id)
70 self.failUnlessEqual(d['cell_line_id'], lib.cell_line_id)
71 self.failUnlessEqual(d['cell_line'], unicode_or_none(lib.cell_line))
72 self.failUnlessEqual(d['experiment_type'], lib.experiment_type.name)
73 self.failUnlessEqual(d['experiment_type_id'], lib.experiment_type_id)
74 self.failUnlessEqual(d['gel_cut_size'], lib.gel_cut_size)
75 self.failUnlessEqual(d['hidden'], lib.hidden)
76 self.failUnlessEqual(d['id'], lib.id)
77 self.failUnlessEqual(d['insert_size'], lib.insert_size)
78 self.failUnlessEqual(d['library_name'], lib.library_name)
79 self.failUnlessEqual(d['library_species'], lib.library_species.scientific_name)
80 self.failUnlessEqual(d['library_species_id'], lib.library_species_id)
81 self.failUnlessEqual(d['library_type_id'], lib.library_type_id)
82 if lib.library_type_id is not None:
83 self.failUnlessEqual(d['library_type'], lib.library_type.name)
85 self.failUnlessEqual(d['library_type'], None)
86 self.failUnlessEqual(d['made_for'], lib.made_for)
87 self.failUnlessEqual(d['made_by'], lib.made_by)
88 self.failUnlessEqual(d['notes'], lib.notes)
89 self.failUnlessEqual(d['replicate'], lib.replicate)
90 self.failUnlessEqual(d['stopping_point'], lib.stopping_point)
91 self.failUnlessEqual(d['successful_pM'], lib.successful_pM)
92 self.failUnlessEqual(d['undiluted_concentration'],
93 unicode(lib.undiluted_concentration))
96 # test a case where there is no known status
97 lane_set = {u'status': u'Unknown',
102 u'flowcell': u'303TUAAXX',
103 u'status_code': None}
104 self.failUnlessEqual(len(d['lane_set']), 1)
105 self.failUnlessEqual(d['lane_set'][0], lane_set)
106 elif lib.id == '11016':
107 # test a case where there is a status
108 lane_set = {u'status': 'Good',
113 u'flowcell': u'303TUAAXX',
115 self.failUnlessEqual(len(d['lane_set']), 1)
116 self.failUnlessEqual(d['lane_set'][0], lane_set)
119 def test_invalid_library_json(self):
121 Make sure we get a 404 if we request an invalid library id
123 response = self.client.get('/samples/library/nottheone/json', apidata)
124 self.failUnlessEqual(response.status_code, 404)
127 def test_invalid_library(self):
128 response = self.client.get('/library/nottheone/')
129 self.failUnlessEqual(response.status_code, 404)
132 def test_library_no_key(self):
134 Make sure we get a 302 if we're not logged in
136 response = self.client.get('/samples/library/10981/json')
137 self.failUnlessEqual(response.status_code, 403)
138 response = self.client.get('/samples/library/10981/json', apidata)
139 self.failUnlessEqual(response.status_code, 200)
141 def test_library_rdf(self):
143 from htsworkflow.util.rdfhelp import get_model, \
145 load_string_into_model, \
150 response = self.client.get('/library/10981/')
151 self.assertEqual(response.status_code, 200)
152 load_string_into_model(model, 'rdfa', response.content)
154 body = """prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
155 prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#>
157 select ?library ?name ?library_id ?gel_cut ?made_by
159 ?library a libns:library ;
161 libns:library_id ?library_id ;
162 libns:gel_cut ?gel_cut ;
163 libns:made_by ?made_by
165 query = RDF.SPARQLQuery(body)
166 for r in query.execute(model):
167 self.assertEqual(fromTypedNode(r['library_id']), u'10981')
168 self.assertEqual(fromTypedNode(r['name']),
169 u'Paired End Multiplexed Sp-BAC')
170 self.assertEqual(fromTypedNode(r['gel_cut']), 400)
171 self.assertEqual(fromTypedNode(r['made_by']), u'Igor')
173 def test_library_index_rdfa(self):
174 from htsworkflow.util.rdfhelp import \
175 add_default_schemas, get_model, load_string_into_model
177 from htsworkflow.util.rdfinfer import Infer
180 add_default_schemas(model)
181 inference = Infer(model)
183 response = self.client.get('/library/')
184 self.assertEqual(response.status_code, 200)
185 load_string_into_model(model, 'rdfa', response.content)
187 errmsgs = list(inference.run_validation())
188 self.assertEqual(len(errmsgs), 2)
189 # didn't feel like giving the index page a type since all
190 # its doing is showing a list of things.
192 self.assertEqual(err, 'Missing type for: http://localhost/')
194 body = """prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
195 prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#>
197 select ?library ?library_id ?name ?species
199 ?library a libns:Library .
200 OPTIONAL { ?library libns:library_id ?library_id . }
201 OPTIONAL { ?library libns:species ?species . }
202 OPTIONAL { ?library libns:name ?name . }
204 bindings = set(['library', 'library_id', 'name', 'species'])
205 query = RDF.SPARQLQuery(body)
207 for r in query.execute(model):
209 for name, value in r.items():
210 self.assertTrue(name in bindings)
211 self.assertTrue(value is not None)
213 self.assertEqual(count, len(Library.objects.filter(hidden=False)))
215 # The django test runner flushes the database between test suites not cases,
216 # so to be more compatible with running via nose we flush the database tables
217 # of interest before creating our sample data.
219 obj.species_human = Species.objects.get(pk=8)
220 obj.experiment_rna_seq = ExperimentType.objects.get(pk=4)
221 obj.affiliation_alice = Affiliation.objects.get(pk=1)
222 obj.affiliation_bob = Affiliation.objects.get(pk=2)
224 Library.objects.all().delete()
225 obj.library_10001 = Library(
227 library_name = 'C2C12 named poorly',
228 library_species = obj.species_human,
229 experiment_type = obj.experiment_rna_seq,
230 creation_date = datetime.datetime.now(),
231 made_for = 'scientist unit 2007',
232 made_by = 'microfludics system 7321',
233 stopping_point = '2A',
234 undiluted_concentration = '5.01',
237 obj.library_10001.save()
238 obj.library_10002 = Library(
240 library_name = 'Worm named poorly',
241 library_species = obj.species_human,
242 experiment_type = obj.experiment_rna_seq,
243 creation_date = datetime.datetime.now(),
244 made_for = 'scientist unit 2007',
245 made_by = 'microfludics system 7321',
246 stopping_point = '2A',
247 undiluted_concentration = '5.01',
250 obj.library_10002.save()
256 rdfNS = RDF.NS("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
257 xsdNS = RDF.NS("http://www.w3.org/2001/XMLSchema#")
258 libNS = RDF.NS("http://jumpgate.caltech.edu/wiki/LibraryOntology#")
259 except ImportError,e:
263 class TestRDFaLibrary(TestCase):
264 fixtures = ['test_samples.json']
266 def test_parse_rdfa(self):
267 model = get_rdf_memory_model()
268 parser = RDF.Parser(name='rdfa')
269 url = '/library/10981/'
270 lib_response = self.client.get(url)
271 self.failIfEqual(len(lib_response.content), 0)
273 parser.parse_string_into_model(model,
274 lib_response.content,
275 'http://localhost'+url)
276 # http://jumpgate.caltech.edu/wiki/LibraryOntology#affiliation>
277 self.check_literal_object(model, ['Bob'], p=libNS['affiliation'])
278 self.check_literal_object(model, ['Multiplexed'], p=libNS['experiment_type'])
279 self.check_literal_object(model, ['400'], p=libNS['gel_cut'])
280 self.check_literal_object(model, ['Igor'], p=libNS['made_by'])
281 self.check_literal_object(model, ['Paired End Multiplexed Sp-BAC'], p=libNS['name'])
282 self.check_literal_object(model, ['Drosophila melanogaster'], p=libNS['species'])
284 self.check_uri_object(model,
285 [u'http://localhost/lane/1193'],
288 fc_uri = RDF.Uri('http://localhost/flowcell/303TUAAXX/')
289 self.check_literal_object(model,
291 s=fc_uri, p=libNS['flowcell_id'])
293 def check_literal_object(self, model, values, s=None, p=None, o=None):
294 statements = list(model.find_statements(
295 RDF.Statement(s,p,o)))
296 self.failUnlessEqual(len(statements), len(values),
297 "Couln't find %s %s %s" % (s,p,o))
299 self.failUnless(s.object.literal_value['string'] in values)
302 def check_uri_object(self, model, values, s=None, p=None, o=None):
303 statements = list(model.find_statements(
304 RDF.Statement(s,p,o)))
305 self.failUnlessEqual(len(statements), len(values),
306 "Couln't find %s %s %s" % (s,p,o))
308 self.failUnless(unicode(s.object.uri) in values)
312 def get_rdf_memory_model():
313 storage = RDF.MemoryStorage()
314 model = RDF.Model(storage)