<commit_before>import json
import mock
import pytest
from datetime import datetime
from dallinger import models


@pytest.mark.usefixtures("experiment_dir")
@pytest.mark.slow
class TestAppConfiguration(object):
    def test_config_gets_loaded_before_first_request(self, webapp):
        from dallinger.config import get_config

        conf = get_config()
        conf.clear()
        webapp.get("/")
        assert conf.ready

    def test_debug_mode_puts_flask_in_debug_mode(self, webapp, active_config):
        webapp.application.debug = False
        from dallinger.experiment_server.gunicorn import StandaloneServer

        StandaloneServer().load()
        assert webapp.application.debug

    def test_production_mode_leaves_flask_in_production_mode(
        self, webapp, active_config
    ):
        active_config.extend({"mode": u"production"})
        webapp.application.debug = False
        from dallinger.experiment_server.gunicorn import StandaloneServer

        StandaloneServer().load()
        assert not webapp.application.debug

    def test_gunicorn_worker_config(self, webapp, active_config):
        with mock.patch("multiprocessing.cpu_count") as cpu_count:
            active_config.extend({"threads": u"auto"})
            cpu_count.return_value = 2
            from dallinger.experiment_server.gunicorn import StandaloneServer

            server = StandaloneServer()
            assert server.options["workers"] == u"4"
            cpu_count.return_value = 4
            server.load_user_config()
            assert server.options["workers"] == u"7"
            active_config.extend({"worker_multiplier": 1.0})
            server.load_user_config()
            assert server.options["workers"] == u"5"
            active_config.extend({"threads": u"2"})
            server.load_user_config()
            assert server.options["workers"] == u"2"


@pytest.mark.usefixtures("experiment_dir")
@pytest.mark.slow
class TestAdvertisement(object):
    def test_returns_error_without_hitId_and_assignmentId(self, webapp):
        resp = webapp.get("/ad")
        assert resp.status_code == 500
        assert b"hit_assign_worker_id_not_set_in_mturk" in resp.data

    def test_with_no_worker_id_and_nonexistent_hit_and_assignment(self, webapp):
        resp = webapp.get("/ad?hitId=foo&assignmentId=bar")
        assert b"Thanks for accepting this HIT." in resp.data
<commit_msg>Fix isolation issue in StandaloneServer tests<commit_after>import json
import mock
import pytest
from datetime import datetime
from dallinger import models


@pytest.mark.usefixtures("experiment_dir")
@pytest.mark.slow
class TestAppConfiguration(object):
    def test_config_gets_loaded_before_first_request(self, webapp):
        from dallinger.config import get_config

        conf = get_config()
        conf.clear()
        webapp.get("/")
        assert conf.ready

    def test_debug_mode_puts_flask_in_debug_mode(self, webapp, active_config):
        webapp.application.debug = False
        from dallinger.experiment_server.gunicorn import StandaloneServer

        with mock.patch("sys.argv", ["gunicorn"]):
            StandaloneServer().load()
        assert webapp.application.debug

    def test_production_mode_leaves_flask_in_production_mode(
        self, webapp, active_config
    ):
        active_config.extend({"mode": u"production"})
        webapp.application.debug = False
        from dallinger.experiment_server.gunicorn import StandaloneServer

        with mock.patch("sys.argv", ["gunicorn"]):
            StandaloneServer().load()
        assert not webapp.application.debug

    def test_gunicorn_worker_config(self, webapp, active_config):
        with mock.patch("multiprocessing.cpu_count") as cpu_count:
            active_config.extend({"threads": u"auto"})
            cpu_count.return_value = 2
            from dallinger.experiment_server.gunicorn import StandaloneServer

            with mock.patch("sys.argv", ["gunicorn"]):
                server = StandaloneServer()
                assert server.options["workers"] == u"4"
                cpu_count.return_value = 4
                server.load_user_config()
                assert server.options["workers"] == u"7"
                active_config.extend({"worker_multiplier": 1.0})
                server.load_user_config()
                assert server.options["workers"] == u"5"
                active_config.extend({"threads": u"2"})
                server.load_user_config()
                assert server.options["workers"] == u"2"


@pytest.mark.usefixtures("experiment_dir")
@pytest.mark.slow
class TestAdvertisement(object):
    def test_returns_error_without_hitId_and_assignmentId(self, webapp):
        resp = webapp.get("/ad")
        assert resp.status_code == 500
        assert b"hit_assign_worker_id_not_set_in_mturk" in resp.data

    def test_with_no_worker_id_and_nonexistent_hit_and_assignment(self, webapp):
        resp = webapp.get("/ad?hitId=foo&assignmentId=bar")
        assert b"Thanks for accepting this HIT." in resp.data

{'fraction_non_alphanumeric': 0.09514370664023786, 'fraction_numerical': 0.004757185332011893, 'mean_word_length': 3.053012048192771, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5144470', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1461, 'n_words': 272}