<commit_before>import pytest
import os

from UM.Settings.MachineDefinition import MachineDefinition

class TestMachineManager():
    def test_emptyManager(self, machine_manager):
        assert machine_manager.getApplicationName() == "test"
        assert machine_manager.getMachineDefinitions() == []
        assert machine_manager.getAllMachineVariants("none") == []
        assert machine_manager.findMachineDefinition("none") is None
        assert machine_manager.getActiveMachineInstance() is None
        assert machine_manager.getProfiles() == []
        assert machine_manager.getProfileReaders() == {}.items()
        assert machine_manager.getProfileWriters() == {}.items()


    def test_addAndFindDefinition(self, machine_manager):
        definition = MachineDefinition(machine_manager, self._getDefinitionsFilePath("basic.json"))
        machine_manager.addMachineDefinition(definition)

        # No data is loaded, the definition should not have a name (and thus, no machine with test_basic should be found!)
        assert machine_manager.findMachineDefinition("test_basic") == None
        definition.loadMetaData()

        # Data is now loaded, so finding the definition should be possible
        assert machine_manager.findMachineDefinition("test_basic") == definition

        assert machine_manager.getMachineDefinitions() == [definition]
        assert machine_manager.getMachineDefinitions(include_variants = False) == [definition]

    def test_variants(self, machine_manager):
        definition_1 = MachineDefinition(machine_manager, self._getDefinitionsFilePath("variant_1.json"))
        definition_1.loadMetaData()
        machine_manager.addMachineDefinition(definition_1)
        definition_2 = MachineDefinition(machine_manager, self._getDefinitionsFilePath("variant_2.json"))
        definition_2.loadMetaData()
        machine_manager.addMachineDefinition(definition_2)

        returned_definitions = machine_manager.getMachineDefinitions()
        assert definition_1 in returned_definitions and definition_2 in returned_definitions

        returned_definitions = machine_manager.getMachineDefinitions(include_variants = False)
        # Check if only one of the definitions is returned.
        assert (definition_1 in returned_definitions) != (definition_2 in returned_definitions)

        returned_definitions = machine_manager.getAllMachineVariants("variant")
        assert definition_1 in returned_definitions and definition_2 in returned_definitions

        assert machine_manager.findMachineDefinition("variant", "Variant test 1") == definition_1
        assert machine_manager.findMachineDefinition("variant", "Variant test 2") == definition_2
        assert machine_manager.findMachineDefinition("variant", "Not existing variant") is None



    def _getDefinitionsFilePath(self, file):
        return os.path.join(os.path.dirname(os.path.abspath(__file__)), "definitions", file)<commit_msg>Added instance tests to manager<commit_after>import pytest
import os

from UM.Settings.MachineDefinition import MachineDefinition
from UM.Settings.MachineInstance import MachineInstance

class TestMachineManager():
    def test_emptyManager(self, machine_manager):
        assert machine_manager.getApplicationName() == "test"
        assert machine_manager.getMachineDefinitions() == []
        assert machine_manager.getAllMachineVariants("none") == []
        assert machine_manager.findMachineDefinition("none") is None
        assert machine_manager.findMachineInstance("Not there") is None
        assert machine_manager.getActiveMachineInstance() is None
        assert machine_manager.getMachineInstances() == []
        assert machine_manager.getProfiles() == []
        assert machine_manager.getProfileReaders() == {}.items()
        assert machine_manager.getProfileWriters() == {}.items()


    def test_addAndFindDefinition(self, machine_manager):
        definition = MachineDefinition(machine_manager, self._getDefinitionsFilePath("basic.json"))
        machine_manager.addMachineDefinition(definition)

        # No data is loaded, the definition should not have a name (and thus, no machine with test_basic should be found!)
        assert machine_manager.findMachineDefinition("test_basic") == None
        definition.loadMetaData()

        # Data is now loaded, so finding the definition should be possible
        assert machine_manager.findMachineDefinition("test_basic") == definition

        assert machine_manager.getMachineDefinitions() == [definition]
        assert machine_manager.getMachineDefinitions(include_variants = False) == [definition]

    def test_variants(self, machine_manager):
        definition_1 = MachineDefinition(machine_manager, self._getDefinitionsFilePath("variant_1.json"))
        definition_1.loadMetaData()
        machine_manager.addMachineDefinition(definition_1)
        definition_2 = MachineDefinition(machine_manager, self._getDefinitionsFilePath("variant_2.json"))
        definition_2.loadMetaData()
        machine_manager.addMachineDefinition(definition_2)

        machine_instance = MachineInstance(machine_manager, definition = definition_1)
        machine_manager.addMachineInstance(machine_instance)
        machine_manager.setActiveMachineInstance(machine_instance)

        machine_manager.setActiveMachineVariant("Variant test 2")
        assert machine_manager.getActiveMachineInstance().getMachineDefinition() == definition_2

        returned_definitions = machine_manager.getMachineDefinitions()
        assert definition_1 in returned_definitions and definition_2 in returned_definitions

        returned_definitions = machine_manager.getMachineDefinitions(include_variants = False)
        # Check if only one of the definitions is returned.
        assert (definition_1 in returned_definitions) != (definition_2 in returned_definitions)

        returned_definitions = machine_manager.getAllMachineVariants("variant")
        assert definition_1 in returned_definitions and definition_2 in returned_definitions

        assert machine_manager.findMachineDefinition("variant", "Variant test 1") == definition_1
        assert machine_manager.findMachineDefinition("variant", "Variant test 2") == definition_2
        assert machine_manager.findMachineDefinition("variant", "Not existing variant") is None

    def test_instances(self, machine_manager):
        definition = MachineDefinition(machine_manager, self._getDefinitionsFilePath("basic.json"))
        definition.loadMetaData()
        machine_manager.addMachineDefinition(definition)

        machine_instance = MachineInstance(machine_manager, definition = definition, name = "Basic Test")
        machine_manager.addMachineInstance(machine_instance)
        assert machine_manager.getMachineInstances() == [machine_instance]
        assert machine_manager.findMachineInstance("Basic Test") == machine_instance
        machine_manager.setActiveMachineInstance(machine_instance)
        assert machine_manager.getActiveMachineInstance() == machine_instance



        #machine_manager.removeMachineInstance(machine_instance)
        #assert machine_manager.getMachineInstances() == []



    def _getProfileFilePath(self, file):
        return os.path.join(os.path.dirname(os.path.abspath(__file__)), "profiles", file)

    def _getDefinitionsFilePath(self, file):
        return os.path.join(os.path.dirname(os.path.abspath(__file__)), "definitions", file)

    def _getInstancesFilePath(self, file):
        return os.path.join(os.path.dirname(os.path.abspath(__file__)), "instances", file)
{'fraction_non_alphanumeric': 0.08073878627968338, 'fraction_numerical': 0.005145118733509235, 'mean_word_length': 4.529540481400438, 'pattern_counts': {'":': 0, '<': 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': '2254726', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1799, 'n_words': 402}