file.py 7.03 KB
Newer Older
1
2
3
4
5
6
#!/usr/bin/python

import unittest
from gluon.globals import Request, Session, Storage, Response
from gluon.contrib.test_helpers import form_postvars
from gluon import current
7
import base64
8
9
10
11
12
13
14
15
16
17
18
19
20
21

class FileController(unittest.TestCase):
        
    def __init__(self, p):
        global auth, session, request
        unittest.TestCase.__init__(self, p)
        
    def setUp(self):
        # Load the to-be-tested file
        execfile("applications/vidjil/controllers/file.py", globals())
        # set up default session/request/auth/...
        global response, session, request, auth
        session = Session()
        request = Request({})
22
        auth = VidjilAuth(globals(), db)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
        auth.login_bare("test@vidjil.org", "1234")
        
        # rewrite info / error functions 
        # for some reasons we lost them between the testRunner and the testCase but we need them to avoid error so ...
        def f(a):
            pass
        log.info = f
        log.error = f
        log.debug = f
        
        # for defs
        current.db = db
        current.auth = auth
        
37
38
39
40
41
42
43
44
45
46
47
48

    def createDumbSequenceFile(self):
        return db.sequence_file.insert(sampling_date="1903-02-02",
                                       info="plop",
                                       pcr="plop",
                                       sequencer="plop",
                                       producer="plop",
                                       patient_id=fake_patient_id,
                                       filename="babibou",
                                       provider=user_id,
                                       data_file =  db.sequence_file.data_file.store(open("../../doc/analysis-example.vidjil", 'rb'), "babibou"))

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    def testAdd(self):      
        request.vars['id'] = fake_patient_id
        
        resp = add()
        self.assertTrue(resp.has_key('message'), "add() has returned an incomplete response")
        
        
    def testAddForm(self):      
        class emptyClass( object ):
            pass
        
        plop = emptyClass()
        setattr(plop, 'file',  open("../../doc/analysis-example.vidjil", 'rb'))
        setattr(plop, 'filename', 'plopapou')

        request.vars['sampling_date'] = "1992-02-02"
        request.vars['file_info'] = "plop"
        request.vars['pcr'] = "plop"
        request.vars['sequencer'] = "plop"
        request.vars['producer'] = "plop"
marc's avatar
marc committed
69
70
        request.vars['patient_id'] = "plapipou("+str(fake_patient_id)+")"
        request.vars['run_id'] = ""
71
72
73
        request.vars['filename'] = "plopapi"
        
        resp = add_form()
marc's avatar
marc committed
74
        self.assertNotEqual(resp.find('"redirect":"sample_set/index"'), -1, "add_form() failed")
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    
    
    def testEdit(self):
        request.vars['patient_id'] = fake_patient_id
        
        resp = edit()
        self.assertTrue(resp.has_key('message'), "edit() has returned an incomplete response")
        
        
    def testEditForm(self):
        request.vars['id'] = fake_file_id
        request.vars['filename'] = "plopapi"
        
        request.vars['sampling_date'] = "1992-02-02"
        request.vars['file_info'] = "plop"
        request.vars['pcr'] = "plop"
        request.vars['sequencer'] = "plop"
        request.vars['producer']="plop"
marc's avatar
marc committed
93
94
        request.vars['patient_id']="plapipou("+str(fake_patient_id)+")"
        request.vars['run_id']=""
95
	request.vars['pre_process'] = fake_pre_process_id
96
97
98
        
        
        resp = edit_form()
Marc Duez's avatar
Marc Duez committed
99
        self.assertEqual(resp.find('"message":"plopapi: metadata saved"'), -1, "edit_form() failed")
100
101
102
103
104
105
106
107
108
109
110
111
112
113
       
       
    def testUpload(self):
        class emptyClass( object ):
            pass
        
        plop = emptyClass()
        setattr(plop, 'file',  open("../../doc/analysis-example.vidjil", 'rb'))
        setattr(plop, 'filename', 'plopapi')
    
        request.vars['file'] = plop
        request.vars['id'] = fake_file_id
    
        resp = upload()
114
        self.assertNotEqual(resp.find('upload finished'), -1, "testUpload() failed")
115
        
116
    def testConfirmFail(self):
117
        resp = confirm()
118
119
120
        self.assertTrue(resp.find('requested file doesn\'t exist') > -1, "confirm() should fail because file is not in DB")

    def testConfirmSuccess(self):
121
        test_file_id = self.createDumbSequenceFile()
122
123
        request.vars['id'] = test_file_id

124
125
        request.vars['redirect_sample_set_id'] = fake_sample_set_id

126
127
        resp = confirm()
        self.assertTrue(resp.has_key('message'), "confirm() fails to confirm deletion of a file")
128
129
    
    
Marc Duez's avatar
Marc Duez committed
130
    def testDelete(self):
131
        test_file_id = self.createDumbSequenceFile()
132
133
134
135

        result_id = db.results_file.insert(sequence_file_id = test_file_id,
                                           config_id = fake_config_id,
                                           run_date = '2015-04-23 00:00:00')
Marc Duez's avatar
Marc Duez committed
136
        
137
        db.sample_set_membership.insert(sample_set_id = fake_sample_set_id, sequence_file_id = test_file_id)
Marc Duez's avatar
Marc Duez committed
138
139
140
        self.assertTrue(db.sequence_file[test_file_id].filename == "babibou" , "file have been added")
        
        request.vars['id'] = test_file_id
141
        request.vars['redirect_sample_set_id'] = fake_sample_set_id
Marc Duez's avatar
Marc Duez committed
142
143
        
        resp = delete()
144
145
        self.assertTrue(db.sequence_file[test_file_id].data_file == None , "file only should have been deleted")
        self.assertTrue(db.results_file[result_id] <> None, "result file should not have been deleted")
Marc Duez's avatar
Marc Duez committed
146
        
147
148
149
150
151
152

        request.vars['delete_results'] = 'True'
        resp = delete()
        self.assertTrue(db.sequence_file[test_file_id] == None, "sequence entry in DB should have been deleted")
        self.assertTrue(db.results_file[result_id] == None, "result file should have been deleted")

Marc Duez's avatar
Marc Duez committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    def testSequencerList(self):
        
        resp = sequencer_list()
        self.assertNotEqual(resp.find('"sequencer":['), -1, "sequencer_list() doesn't return a valid json")
        
            
    def testPcrList(self):
        
        resp = pcr_list()
        self.assertNotEqual(resp.find('"pcr":['), -1, "pcr_list() doesn't return a valid json")
        
            
    def testProducerList(self):
        
        resp = producer_list()
        self.assertNotEqual(resp.find('"producer":['), -1, "producer_list() doesn't return a valid json")
            
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    def testUpdateNameOfSequenceFile(self):
        sequence_id = self.createDumbSequenceFile()
        data_file = db.sequence_file[sequence_id].data_file

        update_name_of_sequence_file(sequence_id, 'toto.txt', 'LICENSE')

        current_sequence = db.sequence_file[sequence_id]
        self.assertEquals(current_sequence.size_file, os.path.getsize('LICENSE'))
        self.assertEquals(current_sequence.data_file, 'LICENSE')
        self.assertEquals(current_sequence.filename, 'toto.txt')

    def testGetNewUploaddedFilename(self):
        sequence_id = self.createDumbSequenceFile()
        data_file = db.sequence_file[sequence_id].data_file

        filename = get_new_uploaded_filename(data_file, "truc.def")

        self.assertEquals(filename[-4:], ".def")
        self.assertTrue(filename.find(base64.b16encode('truc.def').lower() + ".def") > -1)