<commit_before>import os
from tempfile import TemporaryDirectory

import yaml

from pytest import fixture
from smif.controller import Controller


@fixture(scope='function')
def setup_project_folder():
    """Sets up a temporary folder with the required project folder structure

        /assets/
        /assets/assets1.yaml
        /config/
        /config/model.yaml
        /config/timesteps.yaml
        /planning/
        /planning/pre-specified.yaml

    """

    project_folder = TemporaryDirectory()
    folder_list = ['assets', 'config', 'planning']
    for folder in folder_list:
        os.mkdir(os.path.join(project_folder.name, folder))

    filename = os.path.join(project_folder.name, 'config', 'model.yaml')

    file_contents = {'sector_models': ['water_supply'],
                     'timesteps': ['timesteps.yaml'],
                     'assets': ['assets1.yaml'],
                     'planning': {'rule_based': {'use': False,
                                                 'files': None},
                                  'optimisation': {'use': False,
                                                   'files': None},
                                  'pre_spec': {'use': True,
                                               'files': ['pre-specified.yaml']
                                               }
                                  }
                     }

    with open(filename, 'w') as config_file:
        yaml.dump(file_contents, config_file)

    filename = os.path.join(project_folder.name, 'config', 'timesteps.yaml')
    with open(filename, 'w') as config_file:
        timesteps_contents = [2010, 2011, 2012]
        yaml.dump(timesteps_contents, config_file)

    filename = os.path.join(project_folder.name, 'assets', 'assets1.yaml')
    with open(filename, 'w') as config_file:
        timesteps_contents = ['asset_a', 'asset_b', 'asset_c']
        yaml.dump(timesteps_contents, config_file)

    return project_folder


class TestController():

    def test_model_list(self, setup_project_folder):

        with setup_project_folder as folder_name:
            cont = Controller(folder_name)

        expected = ['water_supply']
        actual = cont.model_list
        assert actual == expected

    def test_timesteps(self, setup_project_folder):

        with setup_project_folder as folder_name:
            cont = Controller(folder_name)

        expected = [2010, 2011, 2012]
        actual = cont._timesteps
        assert actual == expected

    def test_assets(self, setup_project_folder):

        with setup_project_folder as folder_name:
            cont = Controller(folder_name)

        expected = ['asset_a', 'asset_b', 'asset_c']
        actual = cont._assets
        assert actual == expected<commit_msg>Added tests for running models, project folder setup<commit_after>import os
from tempfile import TemporaryDirectory

import yaml

from pytest import fixture
from smif.controller import Controller


@fixture(scope='function')
def setup_project_folder():
    """Sets up a temporary folder with the required project folder structure

        /models
        /models/water_supply/
        /models/water_supply/run.py
        /models/water_supply/assets/assets1.yaml
        /config/
        /config/model.yaml
        /config/timesteps.yaml
        /planning/
        /planning/pre-specified.yaml

    """

    project_folder = TemporaryDirectory()
    folder_list = ['config', 'planning', 'models']
    for folder in folder_list:
        os.mkdir(os.path.join(project_folder.name, folder))
    os.mkdir(os.path.join(project_folder.name, 'models', 'water_supply'))
    os.mkdir(os.path.join(project_folder.name, 'models', 'water_supply',
                          'assets'))

    filename = os.path.join(project_folder.name, 'config', 'model.yaml')

    file_contents = {'sector_models': ['water_supply'],
                     'timesteps': ['timesteps.yaml'],
                     'assets': ['assets1.yaml'],
                     'planning': {'rule_based': {'use': False,
                                                 'files': None},
                                  'optimisation': {'use': False,
                                                   'files': None},
                                  'pre_spec': {'use': True,
                                               'files': ['pre-specified.yaml']
                                               }
                                  }
                     }

    with open(filename, 'w') as config_file:
        yaml.dump(file_contents, config_file)

    filename = os.path.join(project_folder.name, 'config', 'timesteps.yaml')
    with open(filename, 'w') as config_file:
        timesteps_contents = [2010, 2011, 2012]
        yaml.dump(timesteps_contents, config_file)

    filename = os.path.join(project_folder.name,
                            'models',
                            'water_supply',
                            'assets',
                            'assets1.yaml')
    with open(filename, 'w') as config_file:
        timesteps_contents = ['water_asset_a',
                              'water_asset_b',
                              'water_asset_c']
        yaml.dump(timesteps_contents, config_file)

    # Write a run.py file for the water_supply model
    filename = os.path.join(project_folder.name,
                            'models',
                            'water_supply',
                            'run.py')
    with open(filename, 'w') as run_file:
        contents = """from unittest.mock import MagicMock
from smif.sectormodel import SectorModel
import time

if __name__ == '__main__':
    model = SectorModel('water_supply')
    model.simulate = MagicMock(return_value=3)
    model.simulate()
    time.sleep(1) # delays for 1 seconds
"""
        for line in contents:
            run_file.write(line)

    return project_folder


class TestController():

    def test_model_list(self, setup_project_folder):

        with setup_project_folder as folder_name:
            cont = Controller(folder_name)

        expected = ['water_supply']
        actual = cont.model_list
        assert actual == expected

    def test_timesteps(self, setup_project_folder):

        with setup_project_folder as folder_name:
            cont = Controller(folder_name)

        expected = [2010, 2011, 2012]
        actual = cont._timesteps
        assert actual == expected

    def test_assets(self, setup_project_folder):

        with setup_project_folder as folder_name:
            cont = Controller(folder_name)

        expected = ['water_asset_a', 'water_asset_b', 'water_asset_c']
        actual = cont._all_assets
        assert actual == expected


class TestRunModel():

    def test_run_sector_model(self, setup_project_folder):
        with setup_project_folder as folder_name:
            cont = Controller(folder_name)

            assert os.path.exists(os.path.join(folder_name,
                                               'models',
                                               'water_supply',
                                               'run.py'))

            cont.run_sector_model('water_supply')
{'fraction_non_alphanumeric': 0.09878779434304026, 'fraction_numerical': 0.007942037062839627, 'mean_word_length': 1.7962602259446825, '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': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2099574', 'n_tokens_mistral': 1969, 'n_tokens_neox': 1844, 'n_words': 419}