d85a46672ebc4e8902bce1a53fad6078b604ec34
[htsworkflow.git] / htsworkflow / frontend / samples / tests.py
1 import datetime
2 import unittest
3
4 try:
5     import json
6 except ImportError, e:
7     import simplejson as json
8     
9 from django.test import TestCase
10
11 from htsworkflow.frontend.samples.models import \
12         Affiliation, \
13         ExperimentType, \
14         Species, \
15         Library
16
17 from htsworkflow.frontend.samples.views import \
18      library_dict, \
19      library_json
20
21 from htsworkflow.frontend.auth import apidata
22
23 # The django test runner flushes the database between test suites not cases,
24 # so to be more compatible with running via nose we flush the database tables
25 # of interest before creating our sample data.
26 def create_db(obj):
27     Species.objects.all().delete()
28     obj.species_human = Species(
29         scientific_name = 'Homo Sapeins',
30         common_name = 'human',
31     )
32     obj.species_human.save()
33     obj.species_worm = Species(
34         scientific_name = 'C. Elegans',
35         common_name = 'worm',
36     )
37     obj.species_worm.save()
38     obj.species_phix = Species(
39         scientific_name = 'PhiX',
40         common_name = 'PhiX'
41     )
42     obj.species_phix.save()
43
44     ExperimentType.objects.all().delete()
45     obj.experiment_de_novo = ExperimentType(
46         name = 'De Novo',
47     )
48     obj.experiment_de_novo.save()
49     obj.experiment_chip_seq = ExperimentType(
50         name = 'ChIP-Seq'
51     )
52     obj.experiment_chip_seq.save()
53     obj.experiment_rna_seq = ExperimentType(
54         name = 'RNA-Seq'
55     )
56     obj.experiment_rna_seq.save()
57
58     Affiliation.objects.all().delete()
59     obj.affiliation_alice = Affiliation(
60         name = 'Alice',
61         contact = 'Lab Boss',
62         email = 'alice@some.where.else.'
63     )
64     obj.affiliation_alice.save()
65     obj.affiliation_bob = Affiliation(
66         name = 'Bob',
67         contact = 'Other Lab Boss',
68         email = 'bob@some.where.else',
69     )
70     obj.affiliation_bob.save()
71
72     Library.objects.all().delete()
73     obj.library_10001 = Library(
74         library_id = 10001,
75         library_name = 'C2C12 named poorly',
76         library_species = obj.species_human,
77         experiment_type = obj.experiment_rna_seq,
78         creation_date = datetime.datetime.now(),
79         made_for = 'scientist unit 2007',
80         made_by = 'microfludics system 7321',
81         stopping_point = '2A',
82         undiluted_concentration = '5.01',
83     )
84     obj.library_10001.save()
85     obj.library_10002 = Library(
86         library_id = 10002,
87         library_name = 'Worm named poorly',
88         library_species = obj.species_human,
89         experiment_type = obj.experiment_rna_seq,
90         creation_date = datetime.datetime.now(),
91         made_for = 'scientist unit 2007',
92         made_by = 'microfludics system 7321',
93         stopping_point = '2A',
94         undiluted_concentration = '5.01',
95     )
96     obj.library_10002.save()
97  
98 class LibraryTestCase(TestCase):
99     def setUp(self):
100         create_db(self)
101                
102     def testOrganism(self):
103         self.assertEquals(self.library_10001.organism(), 'human')
104
105     def testAffiliations(self):
106         self.library_10001.affiliations.add(self.affiliation_alice)
107         self.library_10002.affiliations.add(
108                 self.affiliation_alice, 
109                 self.affiliation_bob
110         )
111         self.failUnless(len(self.library_10001.affiliations.all()), 1)
112         self.failUnless(self.library_10001.affiliation(), 'Alice')
113
114         self.failUnless(len(self.library_10002.affiliations.all()), 2)
115         self.failUnless(self.library_10001.affiliation(), 'Alice, Bob')
116
117 class SampleWebTestCase(TestCase):
118     """
119     Test returning data from our database in rest like ways.
120     (like returning json objects)
121     """
122     fixtures = ['test_samples.json']
123
124     def test_library_info(self):
125
126         for lib in Library.objects.all():
127             lib_dict = library_dict(lib.library_id)
128             url = '/samples/library/%s/json' % (lib.library_id,)
129             lib_response = self.client.get(url, apidata)
130             self.failUnlessEqual(lib_response.status_code, 200)
131             lib_json = json.loads(lib_response.content)
132
133             for d in [lib_dict, lib_json]:
134                 # amplified_from_sample is a link to the library table,
135                 # I want to use the "library_id" for the data lookups not
136                 # the embedded primary key.
137                 # It gets slightly confusing on how to implement sending the right id
138                 # since amplified_from_sample can be null
139                 #self.failUnlessEqual(d['amplified_from_sample'], lib.amplified_from_sample)
140                 self.failUnlessEqual(d['antibody_id'], lib.antibody_id)
141                 self.failUnlessEqual(d['avg_lib_size'], lib.avg_lib_size)
142                 self.failUnlessEqual(d['cell_line'], lib.cell_line.cellline_name)
143                 self.failUnlessEqual(d['cell_line_id'], lib.cell_line_id)
144                 self.failUnlessEqual(d['experiment_type'], lib.experiment_type.name)
145                 self.failUnlessEqual(d['experiment_type_id'], lib.experiment_type_id)
146                 self.failUnlessEqual(d['id'], lib.id)
147                 self.failUnlessEqual(d['library_id'], lib.library_id)
148                 self.failUnlessEqual(d['library_name'], lib.library_name)
149                 self.failUnlessEqual(d['library_species'], lib.library_species.scientific_name)
150                 self.failUnlessEqual(d['library_species_id'], lib.library_species_id)
151                 self.failUnlessEqual(d['library_type_id'], lib.library_type_id)
152                 if lib.library_type_id is not None:
153                     self.failUnlessEqual(d['library_type'], lib.library_type.name)
154                 else:
155                     self.failUnlessEqual(d['library_type'], None)
156                     self.failUnlessEqual(d['made_for'], lib.made_for)
157                     self.failUnlessEqual(d['made_by'], lib.made_by)
158                     self.failUnlessEqual(d['notes'], lib.notes)
159                     self.failUnlessEqual(d['replicate'], lib.replicate)
160                     self.failUnlessEqual(d['stopping_point'], lib.stopping_point)
161                     self.failUnlessEqual(d['successful_pM'], lib.successful_pM)
162                     self.failUnlessEqual(d['undiluted_concentration'],
163                                          unicode(lib.undiluted_concentration))                                 
164     def test_invalid_library(self):
165         """
166         Make sure we get a 404 if we request an invalid library id
167         """
168         response = self.client.get('/samples/library/nottheone/json', apidata)
169         self.failUnlessEqual(response.status_code, 404)
170
171             
172     def test_library_no_key(self):
173         """
174         Make sure we get a 302 if we're not logged in
175         """
176         response = self.client.get('/samples/library/10981/json')
177         self.failUnlessEqual(response.status_code, 403)
178         response = self.client.get('/samples/library/10981/json', apidata)
179         self.failUnlessEqual(response.status_code, 200)