Exemplo n.º 1
0
 def test_sequence_sections_exclusive(self):
     '''test function  sequenceSections(sections)'''
     
     def check_sequence(dic):
         '''return true if all values>0'''
         for i in dic.values():
             if i==0:
                 return False
         return True
         
     # create a user
     u = User(nickname = 'john', email = '*****@*****.**', 
         role = ROLE_RESEARCHER)
     db.session.add(u)    
     survey = Survey(title = "test",researcher = u)
     s1 = Section (title = "1",description = "a",
         sequence = 1, percent = 1, survey = survey)
     s11 = Section (title = "1.1", description = "a",
         sequence = 1, percent = 0.5, parent = s1)
     s12 = Section (title = "1.1", description = "a",
         sequence = 1, percent = 0.5, parent = s1)
     s111 = Section (title = "1.1", description = "a",
         sequence = 1, percent = 0.5, parent = s11)
     s112 = Section (title = "1.1", description = "a",
         sequence = 1, percent = 0.5, parent = s11)
     db.session.add(survey)
     db.session.add(s1)
     db.session.add(s11)
     db.session.add(s12)
     db.session.add(s111)
     db.session.add(s112)
     db.session.commit()
     # key1 0.25 %
     key1 = (s1.id,s11.id,s111.id)
     # key2 0.25 %
     key2 =  (s1.id,s11.id,s112.id)
     # key3 0.50 %
     key3 = (s1.id,s12.id)
     dic ={key1:0,key2:0,key3:0}
     n = 100
     error = 0.2
     for i in range(n):
         l = tuple(Section.sequenceSections(survey.sections))
         if dic.has_key(l):
             dic[l] = dic[l]+1
         else:
             self.assertTrue(False,"unexpected value")
             break
     self.assertTrue(check_sequence(dic))
     range1 = range(int(round(n*.25 - error*n)),int(round(n*.25 + error*n)))
     range2 = range(int(round(n*.5 - error*n)),int(round(n*.5 + error*n)))
     self.assertIn(dic[key1],range1)
     self.assertIn(dic[key2],range1)
     self.assertIn(dic[key3],range2)
Exemplo n.º 2
0
    def test_sequence_sections_order(self):
        '''test function  sequenceSections(sections)'''
        
        def check_sequence(dic):
            '''return true if all values>0'''
            for i in dic.values():
                if i==0:
                    return False
            return True

        u = User(nickname = 'john', email = '*****@*****.**', 
            role = ROLE_RESEARCHER)
        db.session.add(u)
        survey = Survey(title = "test",researcher = u)
        s1 = Section (title = "1",description = "a",
            sequence = 1, percent = 1, survey = survey)
        s2 = Section (title = "1",description = "a",
            sequence = 2, percent = 1, survey = survey)
        s3 = Section (title = "1",description = "a",
            sequence = 2, percent = 1, survey = survey)
        s4 = Section (title = "1",description = "a",
            sequence = 2, percent = 1, survey = survey)
        s5 = Section (title = "1",description = "a",
            sequence = 3, percent = 1, survey = survey)
        s6 = Section (title = "1",description = "a",
            sequence = 3, percent = 1, survey = survey)
        db.session.add(survey)
        db.session.add(s1)
        db.session.add(s2)
        db.session.add(s3)
        db.session.add(s4)
        db.session.add(s5)
        db.session.add(s6)
        db.session.commit()
        s1 = s1.id
        s2 = s2.id
        s3 = s3.id
        s4 = s4.id
        s5 = s5.id
        s6 = s6.id
        key1 = (s1,s2,s3,s4,s5,s6)
        key2 = (s1,s2,s3,s4,s6,s5)
        key3 = (s1,s2,s4,s3,s5,s6)
        key4 = (s1,s2,s4,s3,s6,s5)
        key5 = (s1,s3,s2,s4,s5,s6)
        key6 = (s1,s3,s2,s4,s6,s5)
        key7 = (s1,s3,s4,s2,s5,s6)
        key8 = (s1,s3,s4,s2,s6,s5)
        key9 = (s1,s4,s2,s3,s5,s6)
        key10 = (s1,s4,s2,s3,s6,s5)
        key11 = (s1,s4,s3,s2,s5,s6)
        key12 = (s1,s4,s3,s2,s6,s5)
        dic = {key1:0,key2:0,key3:0,key4:0,key5:0,key6:0,key7:0,
            key8:0,key9:0,key10:0,key11:0,key12:0}
        n = 100
        i=0
        while not check_sequence(dic) and i<n:
            l = tuple(Section.sequenceSections(survey.sections))
            if dic.has_key(l):
                dic[l] = dic[l]+1
            else:
                self.assertTrue(False,"unexpected value")
        i=i+1
        self.assertTrue(check_sequence(dic))