def experiment_session_state_check(self):
        '''
        Test if the experiment session database is as it should be. This is
        important to catch errors in the state before they propagate.

        - The subject should have at most one live experiment session. (If
          there is one, it need not be in this browser session.)
        - If an experiment allows more than one attempt, all but the last
          attempt should be 'completed'. 
        
        '''
        # FIXME. Shouldn't this go in the sessions.models manager?
        _live_sessions_ErrMsg = strings.fill('''There should be only one live
                                             session per subject.  There are %d
                                             listed for subject %s. ''')

        experiment_sessions = self.get_all_my_experiment_sessions() 
        
        len_live_sessions = sum([exp_session.is_live 
                                 for exp_session in experiment_sessions])

        # Make the error msg.
        live_sessions_ErrMsg\
            = _live_sessions_ErrMsg % (len_live_sessions, self.subject)

        # Number of live sessions less than 1 assertion.
        assert len_live_sessions <= 1, live_sessions_ErrMsg

        _experiment_status_ErrMsg = strings.fill('''If the subject has more
                                                 than one experiment sessions
                                                 for the present experiment,
                                                 all but the most recent
                                                 attempts should be
                                                 "completed". For experiment %s
                                                 for subject %s, the statuses
                                                 are %s.''')

        statuses = [session.status for session in
                    self.get_my_this_experiment_sessions()]

        experiment_status_ErrMsg\
        = _experiment_status_ErrMsg % (self.experiment_name, self.subject,
                                       statuses)

        # All but last session is completed assertion.
        my_this_sessions = list(self.get_my_this_experiment_sessions())
        if len(my_this_sessions) > 1:
            assert all(
                [my_this_session.is_completed
                 for my_this_session in my_this_sessions[:-1]
                 ]
            ), experiment_status_ErrMsg
Exemple #2
0
    def _make_text_recall_memory_test(self, name, memorandum):

        slide = TextRecallMemoryTest.new(
                    name=name,
                    option_length=self.parameters['recall']['option_length'],
                    title=memorandum['text_title'],
                    start_text_msg=self.start_msg['start_text_msg'],
                    text=strings.fill(memorandum['text']),
                    start_memorytest_msg=self.start_msg['start_memorytest_msg'],
                    reading_times=self.text_reading_times,
                    **self.game_parameters
                )

        return slide
Exemple #3
0
    def _make_text_recognition_memory_test(self, name, memorandum):

        slide = TextRecognitionMemoryTest.new(
                    name=name,
                    title=memorandum['text_title'],
                    text=strings.fill(memorandum['text']),
                    inwords=memorandum['inwords'].split(','),
                    outwords=memorandum['outwords'].split(','),
                    start_text_msg=self.start_msg['start_text_msg'],
                    start_memorytest_msg=self.start_msg['start_memorytest_msg'],
                    test_times=self.wordlist_recognition_test_times,
                    reading_times=self.text_reading_times,
                    **self.game_parameters
                )

        return slide
def flag_stale_live_sessions(live_session_keep_alive_duration=None):

    '''
    If last_activity on each live_session is older than
    live_session_keep_alive_duration, then set live_session.keep_alive to
    False.
    '''

    logger.debug('Flag any stale live sessions.')

    if live_session_keep_alive_duration is None:
        live_session_keep_alive_duration = conf.live_session_keep_alive_duration 

    for live_session in LiveExperimentSession.objects.filter(alive=True):

        last_activity_or_date_created\
            = live_session.last_activity or live_session.date_created

        try:
            d = datetime.datetime.now() - last_activity_or_date_created
            if d.total_seconds() > live_session_keep_alive_duration: 
                live_session.keep_alive = False
                live_session.save()
                logger.info(
                    'Flagging live_session %s for deletion.' % live_session.uid
                )

        except TypeError as e:

            message = strings.fill(''' Could not calculate time since
                                   live_session last_activity or date_created.
                                   ''')

            message = '\n'.join([message, 'Error message: ' + e.message])

            logger.warning(message)
    def subtest_next_slide_page(self, slide_number):

        next_slide_xpath =  "//div[@id='next_slide']"
        
        next_slide_message_box\
            = next_slide_xpath + "/div[@class='MessageBox']"

        end_of_slide_form\
            = next_slide_xpath + "/div[@id='EndOfSlideForm']"

        continue_button\
            = end_of_slide_form + "/div[@id='ContinueButton']"

        pause_button\
            = end_of_slide_form + "/div[@id='PauseButton']"

        stop_button\
            = end_of_slide_form + "/div[@id='StopButton']"

        always_visible_elements =  (next_slide_xpath, 
                                    next_slide_message_box, 
                                    end_of_slide_form)

        visible_elements = [(continue_button, pause_button), 
                            (continue_button, pause_button),
                            (stop_button,) ]

        invisible_elements = [
            (stop_button,), 
            (stop_button,), 
            (continue_button, pause_button)
        ]

        self.assertTrue(
            all_visible(self.driver, 
                        always_visible_elements + visible_elements[slide_number])
        )

        self.assertTrue(
            none_visible(self.driver, invisible_elements[slide_number])
        )


        template\
            = loader.get_template('presenter/live_experiment_launcher.html')
        
        slide_context\
            = [dict(slides_done=1, 
                    experiment_name = 'brisbane',
                  slides_remaining=2, 
                  button=button), 
               dict(slides_done=2,
                    experiment_name = 'brisbane',
                    slides_remaining=1,
                    button=button), 
               dict(slides_done=3,
                    experiment_name = 'brisbane',
                    slides_remaining=0,
                    button=button)]

        context = Context(slide_context[slide_number])

        rendered_template = template.render(context)
            
        self.assertEqual(
            fill(get_inner_html(rendered_template, 'MessageBox')),
            fill(self.driver.find_element_by_xpath(next_slide_message_box)\
                 .get_attribute('innerHTML').strip())
        )

#
#        message_texts = (
#        [
#            'You have finished Part 1 of the experiment "brisbane".',
#            'There are 2 parts remaining.',
#            'To continue the experiment now, '\
#            +'you can press the "{0}" button.'.format(button.next),
#            'To continue later, '\
#            +'you can press the "{0}" button.'.format(button.pause)
#        ],
#        [
#            'You have finished Part 2 of the experiment "brisbane".',
#            'There is 1 part remaining.',
#            'To continue the experiment now, '\
#            +'you can press the "{0}" button.'.format(button.next),
#            'To continue later, '\
#            +'you can press the "{0}" button.'.format(button.pause)
#        ],
#        [
#            'You have finished Part 3 of the experiment "brisbane".',
#            'The experiment is now over.'
#        ])


#        for text in message_texts[slide_number]:
#            self.assertIn(text,
#                          self.driver.find_element_by_xpath(next_slide_message_box).text)
#

        if slide_number < 2:
            return 'continue', self.driver.find_element_by_xpath(continue_button)
        else: 

            return 'stop', self.driver.find_element_by_xpath(stop_button)