<commit_before>import unittest

from mock import patch

from pyface.ui.qt4.util.modal_dialog_tester import ModalDialogTester
from traits.api import Bool
from traits.testing.api import UnittestTools

from simphony.cuds.abc_modeling_engine import ABCModelingEngine
from simphony_mayavi.plugins.add_engine_panel import AddEnginePanel
from simphony_mayavi.plugins.engine_wrappers.api import ABCEngineFactory
from simphony_mayavi.plugins.engine_manager import EngineManager

from simphony_mayavi.tests.testing_utils import (
    run_and_check_dialog_was_opened,
    press_button_by_label,
    DummyEngine)


class SimpleEngineFactory(ABCEngineFactory):

    def create(self):
        return DummyEngine()


class NeedDefineEngineFactory(ABCEngineFactory):

    some_flag = Bool(True)

    def create(self):
        return DummyEngine()


class BadEngineFactory(ABCEngineFactory):

    def create(self):
        raise Exception("This is a bad engine factory")


MOCKED_ENGINE_FACTORIES = {"good_factory": SimpleEngineFactory(),
                           "need_def_factory": NeedDefineEngineFactory(),
                           "bad_factory": BadEngineFactory()}


class TestAddEnginePanel(UnittestTools, unittest.TestCase):

    @patch("simphony_mayavi.plugins.add_engine_panel.DEFAULT_ENGINE_FACTORIES",
           MOCKED_ENGINE_FACTORIES)
    def test_create_from_factory_need_more_definitions(self):
        panel = AddEnginePanel(engine_manager=EngineManager())

        def choose_factory():
            panel.factory_name = "need_def_factory"
            return True

        tester = ModalDialogTester(choose_factory)

        with self.assertTraitChanges(panel, "new_engine"):
            run_and_check_dialog_was_opened(self, tester, True)

        # ensure the new engine is defined
        self.assertIsInstance(panel.new_engine, ABCModelingEngine)

    @patch("simphony_mayavi.plugins.add_engine_panel.DEFAULT_ENGINE_FACTORIES",
           MOCKED_ENGINE_FACTORIES)
    def test_dialog_create_from_factory_with_bad_factory(self):
        panel = AddEnginePanel(engine_manager=EngineManager())

        def choose_factory():
            panel.factory_name = "bad_factory"
            return True

        tester = ModalDialogTester(choose_factory)
<commit_msg>skip tests that requires qt4<commit_after>import unittest

from mock import patch

from pyface.ui.qt4.util.modal_dialog_tester import ModalDialogTester
from traits.etsconfig.api import ETSConfig
from traits.api import Bool
from traits.testing.api import UnittestTools

from simphony.cuds.abc_modeling_engine import ABCModelingEngine
from simphony_mayavi.plugins.add_engine_panel import AddEnginePanel
from simphony_mayavi.plugins.engine_wrappers.api import ABCEngineFactory
from simphony_mayavi.plugins.engine_manager import EngineManager

from simphony_mayavi.tests.testing_utils import (
    run_and_check_dialog_was_opened,
    press_button_by_label,
    DummyEngine)


class SimpleEngineFactory(ABCEngineFactory):

    def create(self):
        return DummyEngine()


class NeedDefineEngineFactory(ABCEngineFactory):

    some_flag = Bool(True)

    def create(self):
        return DummyEngine()


class BadEngineFactory(ABCEngineFactory):

    def create(self):
        raise Exception("This is a bad engine factory")


MOCKED_ENGINE_FACTORIES = {"good_factory": SimpleEngineFactory(),
                           "need_def_factory": NeedDefineEngineFactory(),
                           "bad_factory": BadEngineFactory()}


class TestAddEnginePanel(UnittestTools, unittest.TestCase):

    @unittest.skipIf(ETSConfig.toolkit != "qt4",
                     "this testcase requires backend == qt4")
    @patch("simphony_mayavi.plugins.add_engine_panel.DEFAULT_ENGINE_FACTORIES",
           MOCKED_ENGINE_FACTORIES)
    def test_create_from_factory_need_more_definitions(self):
        panel = AddEnginePanel(engine_manager=EngineManager())

        def choose_factory():
            panel.factory_name = "need_def_factory"
            return True

        tester = ModalDialogTester(choose_factory)

        with self.assertTraitChanges(panel, "new_engine"):
            run_and_check_dialog_was_opened(self, tester, True)

        # ensure the new engine is defined
        self.assertIsInstance(panel.new_engine, ABCModelingEngine)

    @unittest.skipIf(ETSConfig.toolkit != "qt4",
                     "this testcase requires backend == qt4")
    @patch("simphony_mayavi.plugins.add_engine_panel.DEFAULT_ENGINE_FACTORIES",
           MOCKED_ENGINE_FACTORIES)
    def test_dialog_create_from_factory_with_bad_factory(self):
        panel = AddEnginePanel(engine_manager=EngineManager())

        def choose_factory():
            panel.factory_name = "bad_factory"
            return True

        tester = ModalDialogTester(choose_factory)

{'fraction_non_alphanumeric': 0.06940980881130507, 'fraction_numerical': 0.0014546965918536992, 'mean_word_length': 3.808191808191808, 'pattern_counts': {'":': 6, '<': 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': '1213080', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1374, 'n_words': 256}