Exemple #1
0
def session_bot_runner_factory(session):
    bots = []
    for participant in session.get_participants():
        bot = ParticipantBot(participant)
        bots.append(bot)

    return SessionBotRunner(bots)
Exemple #2
0
    def test_bot_bad_post(self):
        """
        Test that posting bad data without using SubmitInvalid
        will raise an error.
        """

        session = otree.session.create_session(
            session_config_name='bots_bad_post',
            num_participants=1,
            use_cli_bots=True,
            bot_case_number=0,
        )

        bots = []
        for participant in session.get_participants():
            bot = ParticipantBot(participant)
            bots.append(bot)
            bot.open_start_url()

        bot_runner = SessionBotRunner(bots, session.code)

        with self.assertRaises(AssertionError):
            # need to disable log output, because this triggers an exception
            # that is logged to stdout
            logging.disable(logging.CRITICAL)
            bot_runner.play_until_end()
            logging.disable(logging.NOTSET)
Exemple #3
0
def test_bots(session_config_name, num_participants, preserve_data):
    config_name = session_config_name
    session_config = otree.session.SESSION_CONFIGS_DICT[config_name]

    # num_bots is deprecated, because the old default of 12 or 6 was too
    # much, and it doesn't make sense to
    if num_participants is None:
        num_participants = session_config['num_demo_participants']

    num_bot_cases = session_config.get_num_bot_cases()
    for case_number in range(num_bot_cases):
        if num_bot_cases > 1:
            logger.info("Creating '{}' session (test case {})".format(
                config_name, case_number))
        else:
            logger.info("Creating '{}' session".format(config_name))

        session = otree.session.create_session(
            session_config_name=config_name,
            num_participants=num_participants,
            use_cli_bots=True,
            label='{} [bots]'.format(config_name),
            bot_case_number=case_number)
        bots = []

        for participant in session.get_participants():
            bot = ParticipantBot(participant)
            bots.append(bot)
            bot.open_start_url()

        bot_runner = SessionBotRunner(bots, session.code)
        bot_runner.play_until_end()
Exemple #4
0
    def test_bots_check_html(self):

        session = otree.session.create_session(
            session_config_name='bots_check_html',
            num_participants=1,
            use_cli_bots=True,
            bot_case_number=0,
        )

        bots = []

        with self.settings(BOTS_CHECK_HTML=True):
            from django.conf import settings
            self.assertEqual(settings.BOTS_CHECK_HTML, True)
            for participant in session.get_participants():
                bot = ParticipantBot(participant)
                bots.append(bot)
                bot.open_start_url()

            bot_runner = SessionBotRunner(bots, session.code)

            try:
                bot_runner.play_until_end()
            except AssertionError as exc:
                # AssertionError should say something about check_html
                raises_correct_message = 'check_html' in str(exc)
            else:
                raises_correct_message = False
            if not raises_correct_message:
                raise AssertionError('bots check_html not working properly')
Exemple #5
0
def session_bot_runner_factory(session) -> SessionBotRunner:
    bots = []
    for participant in session.get_participants():
        bot = ParticipantBot(participant)
        bots.append(bot)

    return SessionBotRunner(bots)
Exemple #6
0
def test_bots(session_config_name, num_participants, run_export):
    config_name = session_config_name
    session_config = otree.session.SESSION_CONFIGS_DICT[config_name]

    # num_bots is deprecated, because the old default of 12 or 6 was too
    # much, and it doesn't make sense to
    if num_participants is None:
        num_participants = session_config['num_demo_participants']

    num_bot_cases = session_config.get_num_bot_cases()
    for case_number in range(num_bot_cases):
        if num_bot_cases > 1:
            logger.info("Creating '{}' session (test case {})".format(
                config_name, case_number))
        else:
            logger.info("Creating '{}' session".format(config_name))

        session = otree.session.create_session(
            session_config_name=config_name,
            num_participants=num_participants,
            use_cli_bots=True,
            bot_case_number=case_number
        )
        bots = []

        for participant in session.get_participants():
            bot = ParticipantBot(participant)
            bots.append(bot)
            bot.open_start_url()

        bot_runner = SessionBotRunner(bots, session.code)
        bot_runner.play_until_end()
    if run_export:
        # bug: if the user tests multiple session configs,
        # the data will only be exported for the last session config.
        # this is because the DB is cleared after each test case.
        # fix later if this becomes high priority
        export_path = pytest.config.option.export_path

        now = datetime.datetime.now()

        if export_path == 'auto_name':
            export_path = now.strftime('_bots_%b%d_%Hh%Mm%S.%f')[:-5] + 's'

        if os.path.isdir(export_path):
            msg = "Directory '{}' already exists".format(export_path)
            raise IOError(msg)

        os.makedirs(export_path)

        for app in settings.INSTALLED_OTREE_APPS:
            model_module = otree.common_internal.get_models_module(app)
            if model_module.Player.objects.exists():
                fname = "{}.csv".format(app)
                fpath = os.path.join(export_path, fname)
                with codecs.open(fpath, "w", encoding="utf8") as fp:
                    otree.export.export_csv(app, fp)

        logger.info('Exported CSV to folder "{}"'.format(export_path))
Exemple #7
0
    def test_all_blocks_found(self):
        session = otree.session.create_session(
            session_config_name='templates_app',
            num_participants=1,
            use_cli_bots=True,
            bot_case_number=0,
        )

        participant = session.get_participants()[0]
        bot = ParticipantBot(participant, load_player_bots=False)
        bot.open_start_url()
        for block_name in [
            'scripts', 'app_scripts', 'global_scripts',
            'styles', 'app_styles', 'global_styles',
        ]:
            self.assertIn('my_{}'.format(block_name), bot.html)
Exemple #8
0
    def test_bots_check_html(self):

        session = otree.session.create_session(
            session_config_name='bots_check_html',
            num_participants=3,
        )

        p1, p2, p3 = session.get_participants()

        with self.settings(BOTS_CHECK_HTML=True):
            with self.assertRaises(MissingHtmlFormFieldError):
                ParticipantBot(p1)._play_individually()

            with self.assertRaises(MissingHtmlButtonError):
                ParticipantBot(p2)._play_individually()

            # should run without problems
            ParticipantBot(p3)._play_individually()
Exemple #9
0
    def test_bot_runs(self):

        session = otree.session.create_session(
            session_config_name='bots',
            num_participants=2,
            use_cli_bots=True,
            bot_case_number=0,
        )

        bots = []
        for participant in session.get_participants():
            bot = ParticipantBot(participant)
            bots.append(bot)
            bot.open_start_url()

        bot_runner = SessionBotRunner(bots, session.code)

        with self.assertRaises(ZeroDivisionError):
            bot_runner.play_until_end()
Exemple #10
0
    def test_bots_check_html(self):

        session = otree.session.create_session(
            session_config_name='bots_check_html',
            num_participants=3,
            use_cli_bots=True,
            bot_case_number=0,
        )

        p1, p2, p3 = session.get_participants()

        with self.settings(BOTS_CHECK_HTML=True):
            with self.assertRaises(MissingHtmlFormFieldError):
                ParticipantBot(p1)._play_individually()

            with self.assertRaises(MissingHtmlButtonError):
                ParticipantBot(p2)._play_individually()

            # should run without problems
            ParticipantBot(p3)._play_individually()