sample_set.py 4.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/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

class Sample_setController(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/sample_set.py", globals())
        # set up default session/request/auth/...
        global response, session, request, auth
        session = Session()
        request = Request({})
        auth = VidjilAuth(globals(), db)
22
        auth.login_bare("test@vidjil.org", "123456")
23 24 25
        
        # rewrite info / error functions 
        # for some reasons we lost them between the testRunner and the testCase but we need them to avoid error so ...
26
        def f(a, **kwargs):
27 28 29 30 31 32 33 34 35
            pass
        log.info = f
        log.error = f
        log.debug = f
        
        # for defs
        current.db = db
        current.auth = auth
        
36 37

    def testAll(self):
Mathieu Giraud's avatar
Mathieu Giraud committed
38
        request.vars["type"] = defs.SET_TYPE_GENERIC
39 40 41
        request.vars["id"] = fake_patient_id

        resp = all()
42 43
        self.assertTrue(resp.has_key('query'), "all() has returned an incomplete response" )

44 45 46 47 48 49 50 51 52 53
    def testIndex(self):
        request.vars["id"] = fake_sample_set_id
        
        resp = index()
        self.assertTrue(resp.has_key('query'), "info() has returned an incomplete response")
        
        
    def testCustom(self):
        resp = custom()
        self.assertTrue(resp.has_key('query'), "custom() has returned an incomplete response")
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

    def test1Permission(self):
        sample_set_id = db.patient[permission_patient].sample_set_id
        request.vars["id"] = sample_set_id

        resp = permission()
        self.assertTrue(resp.has_key('query'), "permission() has returned an incomplete response")


    def test2ChangePermission(self):
        patient = db.patient[permission_patient]
        sample_set_id = patient.sample_set_id
        request.vars["sample_set_id"] = sample_set_id
        request.vars["group_id"] = fake_group_id

        resp = change_permission()
        self.assertFalse(auth.get_group_access('patient', patient.id, fake_group_id), "fail to remove permission")

        resp = change_permission()
        self.assertTrue(auth.get_group_access('patient', patient.id, fake_group_id), "fail to add permission")
74 75

    def testForm(self):
76
        request.vars["type"] = "patient"
77 78 79 80 81
        resp = form()
        self.assertTrue(resp.has_key('message'), "add() has returned an incomplete response")


    def test1Add(self):
82
        import json
83 84 85 86 87
        patient = {
            "first_name" : "bob",
            "last_name" : "bob",
            "birth" : "2011-11-11",
            "info" : "test patient kZtYnOipmAzZ",
88 89
            "id_label" : "bob",
            "sample_set_id": ""
90 91 92
        }
        data = {'patient':[patient], 'group': fake_group_id}

93
        request.vars['data'] = json.dumps(data)
94 95 96 97

        name = "%s %s" % (request.vars["first_name"], request.vars["last_name"])

        resp = submit()
98
        self.assertNotEqual(resp.find('successfully added/edited set(s)'), -1, "add patient failled")
99 100 101 102 103 104 105 106

    def testEdit(self):
        request.vars["id"] = fake_patient_id

        resp = form()
        self.assertTrue(resp.has_key('message'), "edit() has returned an incomplete response")

    def testEditForm(self):
107
        import json
108
        pat = db.patient[fake_patient_id]
109
        patient = {
110
            "id" : pat.id,
111 112 113
            "first_name" : "bab",
            "last_name" : "bab",
            "birth" : "2010-10-10",
114
            "info" : "bab #ALL ",
Mikaël Salson's avatar
Mikaël Salson committed
115
            "id_label" : "bab",
116
            "sample_set_id": pat.sample_set_id
117 118
        }
        data = {'patient': [patient]}
119
        request.vars['data'] = json.dumps(data)
120 121

        resp = submit()
122
        self.assertNotEqual(resp.find('successfully added/edited set(s)"'), -1, "edit patient failed")
123 124 125 126 127 128 129 130 131 132 133 134 135 136

    def testConfirm(self):
        request.vars["id"] = fake_sample_set_id

        resp = confirm()
        self.assertTrue(resp.has_key('message'), "confirm() has returned an incomplete response")


    def test4Delete(self):
        patient = db( db.patient.info == "test patient kZtYnOipmAzZ").select()[0]
        request.vars["id"] = patient.sample_set_id

        resp = delete()
        self.assertNotEqual(resp.find('sample set ('+str(patient.sample_set_id)+') deleted'), -1, "delete sample_set failed")