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):
56 for lib in Library.objects.all():
57 lib_dict = library_dict(lib.id)
58 url = '/samples/library/%s/json' % (lib.id,)
59 lib_response = self.client.get(url, apidata)
60 self.failUnlessEqual(lib_response.status_code, 200)
61 lib_json = json.loads(lib_response.content)
63 for d in [lib_dict, lib_json]:
64 # amplified_from_sample is a link to the library table,
65 # I want to use the "id" for the data lookups not
66 # the embedded primary key.
67 # It gets slightly confusing on how to implement sending the right id
68 # since amplified_from_sample can be null
69 #self.failUnlessEqual(d['amplified_from_sample'], lib.amplified_from_sample)
70 self.failUnlessEqual(d['antibody_id'], lib.antibody_id)
71 self.failUnlessEqual(d['cell_line_id'], lib.cell_line_id)
72 self.failUnlessEqual(d['cell_line'], unicode_or_none(lib.cell_line))
73 self.failUnlessEqual(d['experiment_type'], lib.experiment_type.name)
74 self.failUnlessEqual(d['experiment_type_id'], lib.experiment_type_id)
75 self.failUnlessEqual(d['gel_cut_size'], lib.gel_cut_size)
76 self.failUnlessEqual(d['hidden'], lib.hidden)
77 self.failUnlessEqual(d['id'], lib.id)
78 self.failUnlessEqual(d['insert_size'], lib.insert_size)
79 self.failUnlessEqual(d['library_name'], lib.library_name)
80 self.failUnlessEqual(d['library_species'], lib.library_species.scientific_name)
81 self.failUnlessEqual(d['library_species_id'], lib.library_species_id)
82 self.failUnlessEqual(d['library_type_id'], lib.library_type_id)
83 if lib.library_type_id is not None:
84 self.failUnlessEqual(d['library_type'], lib.library_type.name)
86 self.failUnlessEqual(d['library_type'], None)
87 self.failUnlessEqual(d['made_for'], lib.made_for)
88 self.failUnlessEqual(d['made_by'], lib.made_by)
89 self.failUnlessEqual(d['notes'], lib.notes)
90 self.failUnlessEqual(d['replicate'], lib.replicate)
91 self.failUnlessEqual(d['stopping_point'], lib.stopping_point)
92 self.failUnlessEqual(d['successful_pM'], lib.successful_pM)
93 self.failUnlessEqual(d['undiluted_concentration'],
94 unicode(lib.undiluted_concentration))
97 # test a case where there is no known status
98 lane_set = {u'status': u'Unknown', u'paired_end': True, u'read_length': 75, u'lane_number': 1, u'flowcell': u'303TUAAXX', u'status_code': None}
99 self.failUnlessEqual(len(d['lane_set']), 1)
100 self.failUnlessEqual(d['lane_set'][0], lane_set)
101 elif lib.id == '11016':
102 # test a case where there is a status
103 lane_set = {u'status': 'Good', u'paired_end': True, u'read_length': 75, u'lane_number': 5, u'flowcell': u'303TUAAXX', u'status_code': 2}
104 self.failUnlessEqual(len(d['lane_set']), 1)
105 self.failUnlessEqual(d['lane_set'][0], lane_set)
108 def test_invalid_library_json(self):
110 Make sure we get a 404 if we request an invalid library id
112 response = self.client.get('/samples/library/nottheone/json', apidata)
113 self.failUnlessEqual(response.status_code, 404)
116 def test_invalid_library(self):
117 response = self.client.get('/library/nottheone/')
118 self.failUnlessEqual(response.status_code, 404)
121 def test_library_no_key(self):
123 Make sure we get a 302 if we're not logged in
125 response = self.client.get('/samples/library/10981/json')
126 self.failUnlessEqual(response.status_code, 403)
127 response = self.client.get('/samples/library/10981/json', apidata)
128 self.failUnlessEqual(response.status_code, 200)
131 # The django test runner flushes the database between test suites not cases,
132 # so to be more compatible with running via nose we flush the database tables
133 # of interest before creating our sample data.
135 obj.species_human = Species.objects.get(pk=8)
136 obj.experiment_rna_seq = ExperimentType.objects.get(pk=4)
137 obj.affiliation_alice = Affiliation.objects.get(pk=1)
138 obj.affiliation_bob = Affiliation.objects.get(pk=2)
140 Library.objects.all().delete()
141 obj.library_10001 = Library(
143 library_name = 'C2C12 named poorly',
144 library_species = obj.species_human,
145 experiment_type = obj.experiment_rna_seq,
146 creation_date = datetime.datetime.now(),
147 made_for = 'scientist unit 2007',
148 made_by = 'microfludics system 7321',
149 stopping_point = '2A',
150 undiluted_concentration = '5.01',
153 obj.library_10001.save()
154 obj.library_10002 = Library(
156 library_name = 'Worm named poorly',
157 library_species = obj.species_human,
158 experiment_type = obj.experiment_rna_seq,
159 creation_date = datetime.datetime.now(),
160 made_for = 'scientist unit 2007',
161 made_by = 'microfludics system 7321',
162 stopping_point = '2A',
163 undiluted_concentration = '5.01',
166 obj.library_10002.save()
172 rdfNS = RDF.NS("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
173 xsdNS = RDF.NS("http://www.w3.org/2001/XMLSchema#")
174 libNS = RDF.NS("http://jumpgate.caltech.edu/wiki/LibraryOntology#")
175 except ImportError,e:
179 class TestRDFaLibrary(TestCase):
180 fixtures = ['test_samples.json']
182 def test_parse_rdfa(self):
183 model = get_rdf_memory_model()
184 parser = RDF.Parser(name='rdfa')
185 url = '/library/10981/'
186 lib_response = self.client.get(url)
187 self.failIfEqual(len(lib_response.content), 0)
189 parser.parse_string_into_model(model,
190 lib_response.content,
191 'http://localhost'+url)
192 # http://jumpgate.caltech.edu/wiki/LibraryOntology#affiliation>
193 self.check_literal_object(model, ['Bob'], p=libNS['affiliation'])
194 self.check_literal_object(model, ['Multiplexed'], p=libNS['experiment_type'])
195 self.check_literal_object(model, ['400'], p=libNS['gel_cut'])
196 self.check_literal_object(model, ['Igor'], p=libNS['made_by'])
197 self.check_literal_object(model, ['Paired End Multiplexed Sp-BAC'], p=libNS['name'])
198 self.check_literal_object(model, ['Drosophila melanogaster'], p=libNS['species'])
200 self.check_uri_object(model,
201 [u'http://localhost/lane/1193'],
204 self.check_literal_object(model,
206 s=RDF.Uri('http://localhost/flowcell/303TUAAXX/'))
208 def check_literal_object(self, model, values, s=None, p=None, o=None):
209 statements = list(model.find_statements(
210 RDF.Statement(s,p,o)))
211 self.failUnlessEqual(len(statements), len(values),
212 "Couln't find %s %s %s" % (s,p,o))
214 self.failUnless(s.object.literal_value['string'] in values)
217 def check_uri_object(self, model, values, s=None, p=None, o=None):
218 statements = list(model.find_statements(
219 RDF.Statement(s,p,o)))
220 self.failUnlessEqual(len(statements), len(values),
221 "Couln't find %s %s %s" % (s,p,o))
223 self.failUnless(unicode(s.object.uri) in values)
227 def get_rdf_memory_model():
228 storage = RDF.MemoryStorage()
229 model = RDF.Model(storage)