Don't throw an error if library.cell_line is None.
[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 from htsworkflow.util.conversion import unicode_or_none
23
24 # The django test runner flushes the database between test suites not cases,
25 # so to be more compatible with running via nose we flush the database tables
26 # of interest before creating our sample data.
27 def create_db(obj):
28     Species.objects.all().delete()
29     obj.species_human = Species(
30         scientific_name = 'Homo Sapeins',
31         common_name = 'human',
32     )
33     obj.species_human.save()
34     obj.species_worm = Species(
35         scientific_name = 'C. Elegans',
36         common_name = 'worm',
37     )
38     obj.species_worm.save()
39     obj.species_phix = Species(
40         scientific_name = 'PhiX',
41         common_name = 'PhiX'
42     )
43     obj.species_phix.save()
44
45     ExperimentType.objects.all().delete()
46     obj.experiment_de_novo = ExperimentType(
47         name = 'De Novo',
48     )
49     obj.experiment_de_novo.save()
50     obj.experiment_chip_seq = ExperimentType(
51         name = 'ChIP-Seq'
52     )
53     obj.experiment_chip_seq.save()
54     obj.experiment_rna_seq = ExperimentType(
55         name = 'RNA-Seq'
56     )
57     obj.experiment_rna_seq.save()
58
59     Affiliation.objects.all().delete()
60     obj.affiliation_alice = Affiliation(
61         name = 'Alice',
62         contact = 'Lab Boss',
63         email = 'alice@some.where.else.'
64     )
65     obj.affiliation_alice.save()
66     obj.affiliation_bob = Affiliation(
67         name = 'Bob',
68         contact = 'Other Lab Boss',
69         email = 'bob@some.where.else',
70     )
71     obj.affiliation_bob.save()
72
73     Library.objects.all().delete()
74     obj.library_10001 = Library(
75         id = "10001",
76         library_name = 'C2C12 named poorly',
77         library_species = obj.species_human,
78         experiment_type = obj.experiment_rna_seq,
79         creation_date = datetime.datetime.now(),
80         made_for = 'scientist unit 2007',
81         made_by = 'microfludics system 7321',
82         stopping_point = '2A',
83         undiluted_concentration = '5.01',
84     )
85     obj.library_10001.save()
86     obj.library_10002 = Library(
87         id = "10002",
88         library_name = 'Worm named poorly',
89         library_species = obj.species_human,
90         experiment_type = obj.experiment_rna_seq,
91         creation_date = datetime.datetime.now(),
92         made_for = 'scientist unit 2007',
93         made_by = 'microfludics system 7321',
94         stopping_point = '2A',
95         undiluted_concentration = '5.01',
96     )
97     obj.library_10002.save()
98  
99 class LibraryTestCase(TestCase):
100     def setUp(self):
101         create_db(self)
102                
103     def testOrganism(self):
104         self.assertEquals(self.library_10001.organism(), 'human')
105
106     def testAffiliations(self):
107         self.library_10001.affiliations.add(self.affiliation_alice)
108         self.library_10002.affiliations.add(
109                 self.affiliation_alice, 
110                 self.affiliation_bob
111         )
112         self.failUnless(len(self.library_10001.affiliations.all()), 1)
113         self.failUnless(self.library_10001.affiliation(), 'Alice')
114
115         self.failUnless(len(self.library_10002.affiliations.all()), 2)
116         self.failUnless(self.library_10001.affiliation(), 'Alice, Bob')
117
118 class SampleWebTestCase(TestCase):
119     """
120     Test returning data from our database in rest like ways.
121     (like returning json objects)
122     """
123     fixtures = ['test_samples.json']
124
125     def test_library_info(self):
126
127         for lib in Library.objects.all():
128             lib_dict = library_dict(lib.id)
129             url = '/samples/library/%s/json' % (lib.id,)
130             lib_response = self.client.get(url, apidata)
131             self.failUnlessEqual(lib_response.status_code, 200)
132             lib_json = json.loads(lib_response.content)
133
134             for d in [lib_dict, lib_json]:
135                 # amplified_from_sample is a link to the library table,
136                 # I want to use the "id" for the data lookups not
137                 # the embedded primary key.
138                 # It gets slightly confusing on how to implement sending the right id
139                 # since amplified_from_sample can be null
140                 #self.failUnlessEqual(d['amplified_from_sample'], lib.amplified_from_sample)
141                 self.failUnlessEqual(d['antibody_id'], lib.antibody_id)
142                 self.failUnlessEqual(d['avg_lib_size'], lib.avg_lib_size)
143                 self.failUnlessEqual(d['cell_line_id'], lib.cell_line_id)
144                 self.failUnlessEqual(d['cell_line'], unicode_or_none(lib.cell_line))
145                 self.failUnlessEqual(d['experiment_type'], lib.experiment_type.name)
146                 self.failUnlessEqual(d['experiment_type_id'], lib.experiment_type_id)
147                 self.failUnlessEqual(d['id'], lib.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)