<commit_before># -*- coding: utf-8 -*-
import datetime
import io
import os

import boto3

from botocore.exceptions import ClientError

import freezegun

from moto import mock_s3

import pytest

from s4.clients import s3, SyncObject
from s4.utils import to_timestamp
import utils

class TestParseS3URI(object):
    def test_empty(self):
        assert s3.parse_s3_uri('') is None

    def test_incorrect_format(self):
        assert s3.parse_s3_uri('nos3infront/some/path') is None
        assert s3.parse_s3_uri(':/232red+-32') is None

    def test_no_path(self):
        assert s3.parse_s3_uri('s3://bucket') is None

    def test_correct_output(self):
        actual_output = s3.parse_s3_uri('s3://fruit.bowl/apples/and/oranges/')
        assert actual_output.bucket == 'fruit.bowl'
        assert actual_output.key == 'apples/and/oranges/'

class TestIsIgnoredFile(object):
    def test_empty(self):
        assert s3.is_ignored_key('foo/bar/baz', []) is False

    def test_subdirectory(self):
        assert s3.is_ignored_key('foo/.git/baz', ['.git']) is True

    def test_file(self):
        assert s3.is_ignored_key('foo/ignoreme', ['ignore*']) is True

class TestS3SyncClient(object):
    def test_get_client_name(self, s3_client):
        assert s3_client.get_client_name() == 's3'

    def test_repr(self):
        boto_client = boto3.client(
            's3',
            aws_access_key_id='',
            aws_secret_access_key='',
            aws_session_token='',
        )

        client = s3.S3SyncClient(boto_client, 'testbucket', 'foo/bar')
        assert repr(client) == 'S3SyncClient<testbucket, foo/bar>'

    @mock_s3
    def test_index_path(self):
        s3_client = boto3.client(
            's3',
            aws_access_key_id='',
            aws_secret_access_key='',
            aws_session_token='',
        )

        client = s3.S3SyncClient(s3_client, 'testbucket', 'foo/bar')
        assert client.get_uri() == 's3://testbucket/foo/bar/'
        assert client.get_uri('apples.txt') == 's3://testbucket/foo/bar/apples.txt'

    @mock_s3
    def test_index_path(self):
        s3_client = boto3.client(
            's3',
            aws_access_key_id='',
            aws_secret_access_key='',
            aws_session_token='',
        )

        client = s3.S3SyncClient(s3_client, 'testbucket', 'foo/bar')
        assert client.index_path() == 'foo/bar/.index'

    def test_put(self, s3_client):
        # given
        data = b'munchkin'

        # when
        input_object = SyncObject(io.BytesIO(data), len(data), 4000)
        s3_client.put('something/boardgame.rst', input_object)

        # then
        resp = s3_client.boto.get_object(
            Bucket=s3_client.bucket,
            Key=os.path.join(s3_client.prefix, 'something/boardgame.rst'),
        )
        assert resp['Body'].read() == data
        assert s3_client.get_remote_timestamp('something/boardgame.rst') == 4000

    def test_get(self, s3_client):
        # given
        data = b'#000000'
        frozen_time = datetime.datetime(2016, 10, 23, 10, 30, tzinfo=datetime.timezone.utc)
        with freezegun.freeze_time(frozen_time):
            s3_client.boto.put_object(
                Bucket=s3_client.bucket,
                Key=os.path.join(s3_client.prefix, 'black.color'),
                Body=data,
            )

        # when
        output_object = s3_client.get('black.color')

        # then
        assert output_object.fp.read() == data
        assert output_object.total_size == len(data)
        assert output_object.timestamp == to_timestamp(frozen_time)

    def test_get_non_existant(self, s3_client):
        assert s3_client.get('idontexist.md') is None

    def test_put_get(self, s3_client):
        # given
        data = b'woooooooooshhh'
        input_object = SyncObject(io.BytesIO(data), len(data), 4000)
        frozen_time = datetime.datetime(2016, 10, 23, 10, 30, tzinfo=datetime.timezone.utc)

        # when
        with freezegun.freeze_time(frozen_time):
            s3_client.put('something/woosh.gif', input_object)

        # then
        output_object = s3_client.get('something/woosh.gif')
        assert output_object.fp.read() == data
        assert output_object.timestamp == to_timestamp(frozen_time)

    def test_delete(self, s3_client):
        # given
        s3_client.boto.put_object(
            Bucket=s3_client.bucket,
            Key=os.path.join(s3_client.prefix, 'war.png'),
            Body='bang',
        )

        # when
        assert s3_client.delete('war.png') is True

        # then
        assert s3_client.get('war.png') is None
        with pytest.raises(ClientError) as exc:
            s3_client.boto.head_object(
                Bucket=s3_client.bucket,
                Key=os.path.join(s3_client.prefix, 'war.png')
            )
        assert exc.value.response['Error']['Code'] == '404'

    def test_delete_non_existant(self, s3_client):
        assert s3_client.delete('idontexist.png') is False

    def test_get_local_keys(self, s3_client):
        # given
        utils.set_s3_contents(s3_client, 'war.png')
        utils.set_s3_contents(s3_client, 'this/is/fine')
        utils.set_s3_contents(s3_client, '.index')
        utils.write_s3(
            s3_client.boto,
            s3_client.bucket,
            os.path.join('iamsomethingelse', 'ishouldnotshow.txt'),
        )

        # when
        actual_output = s3_client.get_local_keys()

        # then
        expected_output = ['war.png', 'this/is/fine']
        assert sorted(actual_output) == sorted(expected_output)

    def test_get_index_keys(self, s3_client):
        utils.set_s3_index(s3_client, {
            'cow': {
                'local_timestamp': 4000,
                'remote_timestamp': 3000,
            },
            'chicken': {
                'local_timestamp': 4000,
                'remote_timestamp': 3000,
            }
        })
        assert sorted(s3_client.get_index_keys()) == sorted(['cow', 'chicken'])

    def test_get_index_timestamps(self, s3_client):
        # given
        utils.set_s3_index(s3_client, {
            'hello': {
                'remote_timestamp': 1234,
                'local_timestamp': 1200,
            },
            'world': {
                'remote_timestamp': 5000,
            }
        })

        # then
        assert s3_client.get_remote_timestamp('hello') == 1234
        assert s3_client.get_index_local_timestamp('hello') == 1200

        assert s3_client.get_remote_timestamp('world') == 5000
        assert s3_client.get_index_local_timestamp('world') is None

    def test_get_all_index_timestamps(self, s3_client):
        # given
        utils.set_s3_index(s3_client, {
            'hello': {
                'local_timestamp': 1200,
            },
            'world': {
                'local_timestamp': 4000,
            }
        })

        # then
        expected_output = {
            'hello': 1200,
            'world': 4000,
        }
        actual_output = s3_client.get_all_index_local_timestamps()
        assert actual_output == expected_output

    def test_update_index(self, s3_client):
        # given
        utils.set_s3_index(s3_client, {
            'red': {
                'remote_timestamp': 1234,
                'local_timestamp': 1200,
            },
            'green': {
                'remote_timestamp': 5000,
            }
        })

        utils.set_s3_contents(s3_client, 'red', timestamp=5001)
        utils.set_s3_contents(s3_client, 'yellow', timestamp=1000)
        utils.set_s3_contents(s3_client, 'orange', timestamp=2000)

        # when
        s3_client.update_index()

        # then
        expected_index = {
            'red': {
                'remote_timestamp': 1234,
                'local_timestamp': 5001,
            },
            'green': {
                'remote_timestamp': 5000,
                'local_timestamp': None,
            },
            'yellow': {
                'remote_timestamp': None,
                'local_timestamp': 1000,
            },
            'orange': {
                'remote_timestamp': None,
                'local_timestamp': 2000,
            },
        }
        assert s3_client.index == expected_index

    def test_get_real_local_timestamp(self, s3_client):
        # given
        utils.set_s3_contents(s3_client, 'orange', timestamp=2000)

        # then
        assert s3_client.get_real_local_timestamp('orange') == 2000
        assert s3_client.get_real_local_timestamp('idontexist') is None

    def test_get_all_real_local_timestamps(self, s3_client):
        # given
        utils.set_s3_contents(s3_client, 'chocolate/oranges', timestamp=2600)
        utils.set_s3_contents(s3_client, 'gummy/bears', timestamp=2400)
        utils.set_s3_contents(s3_client, 'carrot_cake', timestamp=2000)
        utils.set_s3_contents(s3_client, '.index', timestamp=2043)
        s3_client.reload_index()

        # then
        expected_output = {
            'chocolate/oranges': 2600,
            'gummy/bears': 2400,
            'carrot_cake': 2000,
        }
        actual_output = s3_client.get_all_real_local_timestamps()
        assert actual_output == expected_output

    def test_set_index_timestamps(self, s3_client):
        # given
        utils.set_s3_index(s3_client, {
            'red': {
                'remote_timestamp': 1234,
                'local_timestamp': 1200,
            },
        })

        # when
        s3_client.set_index_local_timestamp('red', 3000)
        s3_client.set_remote_timestamp('red', 4000)
        s3_client.set_index_local_timestamp('green', 5000)
        s3_client.set_remote_timestamp('yellow', 6000)

        # then
        expected_index = {
            'red': {
                'local_timestamp': 3000,
                'remote_timestamp': 4000,
            },
            'green': {
                'local_timestamp': 5000,
            },
            'yellow': {
                'remote_timestamp': 6000,
            },
        }
        assert s3_client.index == expected_index

    def test_interrupted_put(self, s3_client):
        utils.set_s3_contents(s3_client, 'keychain', data='iamsomedatathatexists')

        sync_object = SyncObject(utils.InterruptedBytesIO(), 900000, 3000)

        with pytest.raises(ValueError):
            s3_client.put('keychain', sync_object)

        result = s3_client.get('keychain')
        assert result.fp.read() == b'iamsomedatathatexists'

    def test_ignore_files(self, s3_client):
        utils.set_s3_contents(
            s3_client, '.syncignore', timestamp=4000, data=(
                '*~\n'
                'trashdirectory\n'
                '*py[oc]\n'
                '.git\n'
            )
        )
        s3_client.reload_ignore_files()

        utils.set_s3_contents(s3_client, '.zshrc~')
        utils.set_s3_contents(s3_client, 'trashdirectory')
        utils.set_s3_contents(s3_client, 'foo/mobile.py', timestamp=1290)
        utils.set_s3_contents(s3_client, 'foo/mobile.pyc')
        utils.set_s3_contents(s3_client, 'foo/mobile.pyo')
        utils.set_s3_contents(s3_client, 'foo/.git/7f04e3b69963e5d51b479b30591ab1c2b6a3e7')
        utils.set_s3_contents(s3_client, '.zshrc', timestamp=9999)

        s3_client.update_index()

        expected_index = {
            '.syncignore': {
                'local_timestamp': 4000,
                'remote_timestamp': None,
            },
            '.zshrc': {
                'local_timestamp': 9999,
                'remote_timestamp': None,
            },
            'foo/mobile.py': {
                'local_timestamp': 1290,
                'remote_timestamp': None,
            }
        }
        assert s3_client.index == expected_index
<commit_msg>correct test name test_get_uri<commit_after># -*- coding: utf-8 -*-
import datetime
import io
import os

import boto3

from botocore.exceptions import ClientError

import freezegun

from moto import mock_s3

import pytest

from s4.clients import s3, SyncObject
from s4.utils import to_timestamp
import utils

class TestParseS3URI(object):
    def test_empty(self):
        assert s3.parse_s3_uri('') is None

    def test_incorrect_format(self):
        assert s3.parse_s3_uri('nos3infront/some/path') is None
        assert s3.parse_s3_uri(':/232red+-32') is None

    def test_no_path(self):
        assert s3.parse_s3_uri('s3://bucket') is None

    def test_correct_output(self):
        actual_output = s3.parse_s3_uri('s3://fruit.bowl/apples/and/oranges/')
        assert actual_output.bucket == 'fruit.bowl'
        assert actual_output.key == 'apples/and/oranges/'

class TestIsIgnoredFile(object):
    def test_empty(self):
        assert s3.is_ignored_key('foo/bar/baz', []) is False

    def test_subdirectory(self):
        assert s3.is_ignored_key('foo/.git/baz', ['.git']) is True

    def test_file(self):
        assert s3.is_ignored_key('foo/ignoreme', ['ignore*']) is True

class TestS3SyncClient(object):
    def test_get_client_name(self, s3_client):
        assert s3_client.get_client_name() == 's3'

    def test_repr(self):
        boto_client = boto3.client(
            's3',
            aws_access_key_id='',
            aws_secret_access_key='',
            aws_session_token='',
        )

        client = s3.S3SyncClient(boto_client, 'testbucket', 'foo/bar')
        assert repr(client) == 'S3SyncClient<testbucket, foo/bar>'

    @mock_s3
    def test_get_uri(self):
        s3_client = boto3.client(
            's3',
            aws_access_key_id='',
            aws_secret_access_key='',
            aws_session_token='',
        )

        client = s3.S3SyncClient(s3_client, 'testbucket', 'foo/bar')
        assert client.get_uri() == 's3://testbucket/foo/bar/'
        assert client.get_uri('apples.txt') == 's3://testbucket/foo/bar/apples.txt'

    @mock_s3
    def test_index_path(self):
        s3_client = boto3.client(
            's3',
            aws_access_key_id='',
            aws_secret_access_key='',
            aws_session_token='',
        )

        client = s3.S3SyncClient(s3_client, 'testbucket', 'foo/bar')
        assert client.index_path() == 'foo/bar/.index'

    def test_put(self, s3_client):
        # given
        data = b'munchkin'

        # when
        input_object = SyncObject(io.BytesIO(data), len(data), 4000)
        s3_client.put('something/boardgame.rst', input_object)

        # then
        resp = s3_client.boto.get_object(
            Bucket=s3_client.bucket,
            Key=os.path.join(s3_client.prefix, 'something/boardgame.rst'),
        )
        assert resp['Body'].read() == data
        assert s3_client.get_remote_timestamp('something/boardgame.rst') == 4000

    def test_get(self, s3_client):
        # given
        data = b'#000000'
        frozen_time = datetime.datetime(2016, 10, 23, 10, 30, tzinfo=datetime.timezone.utc)
        with freezegun.freeze_time(frozen_time):
            s3_client.boto.put_object(
                Bucket=s3_client.bucket,
                Key=os.path.join(s3_client.prefix, 'black.color'),
                Body=data,
            )

        # when
        output_object = s3_client.get('black.color')

        # then
        assert output_object.fp.read() == data
        assert output_object.total_size == len(data)
        assert output_object.timestamp == to_timestamp(frozen_time)

    def test_get_non_existant(self, s3_client):
        assert s3_client.get('idontexist.md') is None

    def test_put_get(self, s3_client):
        # given
        data = b'woooooooooshhh'
        input_object = SyncObject(io.BytesIO(data), len(data), 4000)
        frozen_time = datetime.datetime(2016, 10, 23, 10, 30, tzinfo=datetime.timezone.utc)

        # when
        with freezegun.freeze_time(frozen_time):
            s3_client.put('something/woosh.gif', input_object)

        # then
        output_object = s3_client.get('something/woosh.gif')
        assert output_object.fp.read() == data
        assert output_object.timestamp == to_timestamp(frozen_time)

    def test_delete(self, s3_client):
        # given
        s3_client.boto.put_object(
            Bucket=s3_client.bucket,
            Key=os.path.join(s3_client.prefix, 'war.png'),
            Body='bang',
        )

        # when
        assert s3_client.delete('war.png') is True

        # then
        assert s3_client.get('war.png') is None
        with pytest.raises(ClientError) as exc:
            s3_client.boto.head_object(
                Bucket=s3_client.bucket,
                Key=os.path.join(s3_client.prefix, 'war.png')
            )
        assert exc.value.response['Error']['Code'] == '404'

    def test_delete_non_existant(self, s3_client):
        assert s3_client.delete('idontexist.png') is False

    def test_get_local_keys(self, s3_client):
        # given
        utils.set_s3_contents(s3_client, 'war.png')
        utils.set_s3_contents(s3_client, 'this/is/fine')
        utils.set_s3_contents(s3_client, '.index')
        utils.write_s3(
            s3_client.boto,
            s3_client.bucket,
            os.path.join('iamsomethingelse', 'ishouldnotshow.txt'),
        )

        # when
        actual_output = s3_client.get_local_keys()

        # then
        expected_output = ['war.png', 'this/is/fine']
        assert sorted(actual_output) == sorted(expected_output)

    def test_get_index_keys(self, s3_client):
        utils.set_s3_index(s3_client, {
            'cow': {
                'local_timestamp': 4000,
                'remote_timestamp': 3000,
            },
            'chicken': {
                'local_timestamp': 4000,
                'remote_timestamp': 3000,
            }
        })
        assert sorted(s3_client.get_index_keys()) == sorted(['cow', 'chicken'])

    def test_get_index_timestamps(self, s3_client):
        # given
        utils.set_s3_index(s3_client, {
            'hello': {
                'remote_timestamp': 1234,
                'local_timestamp': 1200,
            },
            'world': {
                'remote_timestamp': 5000,
            }
        })

        # then
        assert s3_client.get_remote_timestamp('hello') == 1234
        assert s3_client.get_index_local_timestamp('hello') == 1200

        assert s3_client.get_remote_timestamp('world') == 5000
        assert s3_client.get_index_local_timestamp('world') is None

    def test_get_all_index_timestamps(self, s3_client):
        # given
        utils.set_s3_index(s3_client, {
            'hello': {
                'local_timestamp': 1200,
            },
            'world': {
                'local_timestamp': 4000,
            }
        })

        # then
        expected_output = {
            'hello': 1200,
            'world': 4000,
        }
        actual_output = s3_client.get_all_index_local_timestamps()
        assert actual_output == expected_output

    def test_update_index(self, s3_client):
        # given
        utils.set_s3_index(s3_client, {
            'red': {
                'remote_timestamp': 1234,
                'local_timestamp': 1200,
            },
            'green': {
                'remote_timestamp': 5000,
            }
        })

        utils.set_s3_contents(s3_client, 'red', timestamp=5001)
        utils.set_s3_contents(s3_client, 'yellow', timestamp=1000)
        utils.set_s3_contents(s3_client, 'orange', timestamp=2000)

        # when
        s3_client.update_index()

        # then
        expected_index = {
            'red': {
                'remote_timestamp': 1234,
                'local_timestamp': 5001,
            },
            'green': {
                'remote_timestamp': 5000,
                'local_timestamp': None,
            },
            'yellow': {
                'remote_timestamp': None,
                'local_timestamp': 1000,
            },
            'orange': {
                'remote_timestamp': None,
                'local_timestamp': 2000,
            },
        }
        assert s3_client.index == expected_index

    def test_get_real_local_timestamp(self, s3_client):
        # given
        utils.set_s3_contents(s3_client, 'orange', timestamp=2000)

        # then
        assert s3_client.get_real_local_timestamp('orange') == 2000
        assert s3_client.get_real_local_timestamp('idontexist') is None

    def test_get_all_real_local_timestamps(self, s3_client):
        # given
        utils.set_s3_contents(s3_client, 'chocolate/oranges', timestamp=2600)
        utils.set_s3_contents(s3_client, 'gummy/bears', timestamp=2400)
        utils.set_s3_contents(s3_client, 'carrot_cake', timestamp=2000)
        utils.set_s3_contents(s3_client, '.index', timestamp=2043)
        s3_client.reload_index()

        # then
        expected_output = {
            'chocolate/oranges': 2600,
            'gummy/bears': 2400,
            'carrot_cake': 2000,
        }
        actual_output = s3_client.get_all_real_local_timestamps()
        assert actual_output == expected_output

    def test_set_index_timestamps(self, s3_client):
        # given
        utils.set_s3_index(s3_client, {
            'red': {
                'remote_timestamp': 1234,
                'local_timestamp': 1200,
            },
        })

        # when
        s3_client.set_index_local_timestamp('red', 3000)
        s3_client.set_remote_timestamp('red', 4000)
        s3_client.set_index_local_timestamp('green', 5000)
        s3_client.set_remote_timestamp('yellow', 6000)

        # then
        expected_index = {
            'red': {
                'local_timestamp': 3000,
                'remote_timestamp': 4000,
            },
            'green': {
                'local_timestamp': 5000,
            },
            'yellow': {
                'remote_timestamp': 6000,
            },
        }
        assert s3_client.index == expected_index

    def test_interrupted_put(self, s3_client):
        utils.set_s3_contents(s3_client, 'keychain', data='iamsomedatathatexists')

        sync_object = SyncObject(utils.InterruptedBytesIO(), 900000, 3000)

        with pytest.raises(ValueError):
            s3_client.put('keychain', sync_object)

        result = s3_client.get('keychain')
        assert result.fp.read() == b'iamsomedatathatexists'

    def test_ignore_files(self, s3_client):
        utils.set_s3_contents(
            s3_client, '.syncignore', timestamp=4000, data=(
                '*~\n'
                'trashdirectory\n'
                '*py[oc]\n'
                '.git\n'
            )
        )
        s3_client.reload_ignore_files()

        utils.set_s3_contents(s3_client, '.zshrc~')
        utils.set_s3_contents(s3_client, 'trashdirectory')
        utils.set_s3_contents(s3_client, 'foo/mobile.py', timestamp=1290)
        utils.set_s3_contents(s3_client, 'foo/mobile.pyc')
        utils.set_s3_contents(s3_client, 'foo/mobile.pyo')
        utils.set_s3_contents(s3_client, 'foo/.git/7f04e3b69963e5d51b479b30591ab1c2b6a3e7')
        utils.set_s3_contents(s3_client, '.zshrc', timestamp=9999)

        s3_client.update_index()

        expected_index = {
            '.syncignore': {
                'local_timestamp': 4000,
                'remote_timestamp': None,
            },
            '.zshrc': {
                'local_timestamp': 9999,
                'remote_timestamp': None,
            },
            'foo/mobile.py': {
                'local_timestamp': 1290,
                'remote_timestamp': None,
            }
        }
        assert s3_client.index == expected_index
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

import os
import unittest

from opengem import test
from opengem import config

RISK_CONFIG_FILE = "risk-config.gem"
HAZARD_CONFIG_FILE = "hazard-config.gem"

def fname():
    """docstring for fname"""
    pass

class JobTestCase(unittest.TestCase):

    def setUp(self):
        self.job = config.Job.from_files(os.path.join(test.DATA_DIR,
                RISK_CONFIG_FILE), os.path.join(test.DATA_DIR,
                HAZARD_CONFIG_FILE))

    def test_can_create_a_job_from_config_files(self):
        self.assertEqual("ErfLogicTree.inp", self.job["erf_logic_tree_file"])
        self.assertEqual("GmpeLogicTree.inp", self.job["gmpe_logic_tree_file"])
        self.assertEqual("~/gem_output", self.job["output_dir"])
        self.assertEqual("exposure.xml", self.job["exposure"])
        self.assertEqual("vulnerability.xml", self.job["vulnerability"])
        self.assertEqual("loss_ratio_map.tiff", self.job["loss_ratio_map"])
        self.assertEqual("", self.job["filter_region"])
        self.assertEqual("", self.job["output_region"])
        self.assertEqual("hazard_curves.xml", self.job["hazard_curves"])
        self.assertEqual("loss_map.tiff", self.job["loss_map"])

    def test_a_job_has_an_identifier(self):
        self.assertEqual(1, config.Job({}, 1).id)

    def test_can_store_and_read_jobs_from_kvs(self):
        self.job.to_kvs()
        self.assertEqual(self.job, config.Job.from_kvs(self.job.id))

    def test_is_invalid_with_no_region_and_exposure(self):
        self.job = config.Job({})
        self.assertFalse(self._decorated_function()())

        # parameters defined, but empty
        self.job = config.Job({config.EXPOSURE: "", config.INPUT_REGION: ""})
        self.assertFalse(self._decorated_function()())

        # parameters defined
        self.job = config.Job({config.EXPOSURE: "exposure.xml"})
        self.assertTrue(self._decorated_function()())

        self.job = config.Job({config.INPUT_REGION: "region.file"})
        self.assertTrue(self._decorated_function()())

        self.job = config.Job({
                config.EXPOSURE: "exposure.xml",
                config.INPUT_REGION: "region.file"})

        self.assertTrue(self._decorated_function()())

    def _decorated_function(self):
        @self.job.validate
        def f():
            return True

        return f<commit_msg>Deleted unused function on config_unittest.py<commit_after># -*- coding: utf-8 -*-

import os
import unittest

from opengem import test
from opengem import config

RISK_CONFIG_FILE = "risk-config.gem"
HAZARD_CONFIG_FILE = "hazard-config.gem"

class JobTestCase(unittest.TestCase):

    def setUp(self):
        self.job = config.Job.from_files(os.path.join(test.DATA_DIR,
                RISK_CONFIG_FILE), os.path.join(test.DATA_DIR,
                HAZARD_CONFIG_FILE))

    def test_can_create_a_job_from_config_files(self):
        self.assertEqual("ErfLogicTree.inp", self.job["erf_logic_tree_file"])
        self.assertEqual("GmpeLogicTree.inp", self.job["gmpe_logic_tree_file"])
        self.assertEqual("~/gem_output", self.job["output_dir"])
        self.assertEqual("exposure.xml", self.job["exposure"])
        self.assertEqual("vulnerability.xml", self.job["vulnerability"])
        self.assertEqual("loss_ratio_map.tiff", self.job["loss_ratio_map"])
        self.assertEqual("", self.job["filter_region"])
        self.assertEqual("", self.job["output_region"])
        self.assertEqual("hazard_curves.xml", self.job["hazard_curves"])
        self.assertEqual("loss_map.tiff", self.job["loss_map"])

    def test_a_job_has_an_identifier(self):
        self.assertEqual(1, config.Job({}, 1).id)

    def test_can_store_and_read_jobs_from_kvs(self):
        self.job.to_kvs()
        self.assertEqual(self.job, config.Job.from_kvs(self.job.id))

    def test_is_invalid_with_no_region_and_exposure(self):
        self.job = config.Job({})
        self.assertFalse(self._decorated_function()())

        # parameters defined, but empty
        self.job = config.Job({config.EXPOSURE: "", config.INPUT_REGION: ""})
        self.assertFalse(self._decorated_function()())

        # parameters defined
        self.job = config.Job({config.EXPOSURE: "exposure.xml"})
        self.assertTrue(self._decorated_function()())

        self.job = config.Job({config.INPUT_REGION: "region.file"})
        self.assertTrue(self._decorated_function()())

        self.job = config.Job({
                config.EXPOSURE: "exposure.xml",
                config.INPUT_REGION: "region.file"})

        self.assertTrue(self._decorated_function()())

    def _decorated_function(self):
        @self.job.validate
        def f():
            return True

        return f<|endoftext|>


<commit_before># Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
from unittest import TestCase

import os
from timeout_decorator import timeout_decorator

from qrl.core import logger, config
from qrl.core.chain import Chain
from qrl.core.state import State

logger.initialize_default(force_console_output=True)

class TestChain(TestCase):
    def __init__(self, *args, **kwargs):
        super(TestChain, self).__init__(*args, **kwargs)
        test_dir = os.path.dirname(os.path.abspath(__file__))
        config.user.wallet_path = os.path.join(test_dir, 'known_data/testcase1')

    @timeout_decorator.timeout(10)
    def test_check_chain(self):
        with State() as state:
            self.assertIsNotNone(state)

            chain = Chain(state)
            self.assertIsNotNone(chain)
            # chain.my

            self.assertEqual(chain.mining_address,
                             'Q5897606f1c347afc1c099b08cd09d72626a6c4b503a3a1207e1b189c0a2bbab618f7')

            self.assertEqual(chain.my[0][0],
                             'Q5897606f1c347afc1c099b08cd09d72626a6c4b503a3a1207e1b189c0a2bbab618f7')

            # TODO: Add more check for data stability

<commit_msg>Increasing timeout time for travis<commit_after># Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
from unittest import TestCase

import os
from timeout_decorator import timeout_decorator

from qrl.core import logger, config
from qrl.core.chain import Chain
from qrl.core.state import State

logger.initialize_default(force_console_output=True)

class TestChain(TestCase):
    def __init__(self, *args, **kwargs):
        super(TestChain, self).__init__(*args, **kwargs)
        test_dir = os.path.dirname(os.path.abspath(__file__))
        config.user.wallet_path = os.path.join(test_dir, 'known_data/testcase1')

    @timeout_decorator.timeout(60)
    def test_check_chain(self):
        with State() as state:
            self.assertIsNotNone(state)

            chain = Chain(state)
            self.assertIsNotNone(chain)
            # chain.my

            self.assertEqual(chain.mining_address,
                             'Q5897606f1c347afc1c099b08cd09d72626a6c4b503a3a1207e1b189c0a2bbab618f7')

            self.assertEqual(chain.my[0][0],
                             'Q5897606f1c347afc1c099b08cd09d72626a6c4b503a3a1207e1b189c0a2bbab618f7')

            # TODO: Add more checks for data stability

<|endoftext|>


<commit_before>import asyncio
import os
import tempfile
import time
import shutil
from pathlib import Path
from typing import Text, Optional, Any, List
from unittest.mock import Mock

import pytest

import rasa
import rasa.core
import rasa.nlu
from rasa.importers.importer import TrainingDataImporter
from rasa.importers.rasa import RasaFileImporter
from rasa.constants import (
    DEFAULT_CONFIG_PATH,
    DEFAULT_DATA_PATH,
    DEFAULT_DOMAIN_PATH,
    DEFAULT_CORE_SUBDIRECTORY_NAME,
)
from rasa.core.domain import Domain, KEY_RESPONSES
from rasa.core.utils import get_dict_hash
from rasa import model
from rasa.model import (
    FINGERPRINT_CONFIG_KEY,
    FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY,
    FINGERPRINT_NLG_KEY,
    FINGERPRINT_FILE_PATH,
    FINGERPRINT_NLU_DATA_KEY,
    FINGERPRINT_RASA_VERSION_KEY,
    FINGERPRINT_STORIES_KEY,
    FINGERPRINT_TRAINED_AT_KEY,
    FINGERPRINT_CONFIG_CORE_KEY,
    FINGERPRINT_CONFIG_NLU_KEY,
    SECTION_CORE,
    SECTION_NLU,
    create_package_rasa,
    get_latest_model,
    get_model,
    get_model_subdirectories,
    model_fingerprint,
    Fingerprint,
    did_section_fingerprint_change,
    should_retrain,
    FingerprintComparisonResult,
)
from rasa.exceptions import ModelNotFound
from tests.core.conftest import DEFAULT_DOMAIN_PATH_WITH_MAPPING

def test_get_latest_model(trained_rasa_model: str):
    import shutil

    path_of_latest = os.path.join(os.path.dirname(trained_rasa_model), "latest.tar.gz")
    shutil.copy(trained_rasa_model, path_of_latest)

    model_directory = os.path.dirname(path_of_latest)

    assert get_latest_model(model_directory) == path_of_latest

def test_get_model_from_directory(trained_rasa_model: str):
    unpacked = get_model(trained_rasa_model)

    assert os.path.exists(os.path.join(unpacked, DEFAULT_CORE_SUBDIRECTORY_NAME))
    assert os.path.exists(os.path.join(unpacked, "nlu"))

def test_get_model_context_manager(trained_rasa_model: str):
    with get_model(trained_rasa_model) as unpacked:
        assert os.path.exists(unpacked)

    assert not os.path.exists(unpacked)

@pytest.mark.parametrize("model_path", ["foobar", "rasa", "README.md", None])
def test_get_model_exception(model_path):
    with pytest.raises(ModelNotFound):
        get_model(model_path)

def test_get_model_from_directory_with_subdirectories(
    trained_rasa_model: Text, tmp_path: Path
):
    unpacked = get_model(trained_rasa_model)
    unpacked_core, unpacked_nlu = get_model_subdirectories(unpacked)

    assert unpacked_core
    assert unpacked_nlu

    with pytest.raises(ModelNotFound):
        get_model_subdirectories(str(tmp_path))  # temp path should be empty

def test_get_model_from_directory_nlu_only(trained_rasa_model: Text):
    unpacked = get_model(trained_rasa_model)
    shutil.rmtree(os.path.join(unpacked, DEFAULT_CORE_SUBDIRECTORY_NAME))
    unpacked_core, unpacked_nlu = get_model_subdirectories(unpacked)

    assert not unpacked_core
    assert unpacked_nlu

def _fingerprint(
    config: Optional[Any] = None,
    config_nlu: Optional[Any] = None,
    config_core: Optional[Any] = None,
    domain: Optional[Any] = None,
    nlg: Optional[Any] = None,
    stories: Optional[Any] = None,
    nlu: Optional[Any] = None,
    rasa_version: Text = "1.0",
):
    return {
        FINGERPRINT_CONFIG_KEY: config if config is not None else ["test"],
        FINGERPRINT_CONFIG_CORE_KEY: config_core
        if config_core is not None
        else ["test"],
        FINGERPRINT_CONFIG_NLU_KEY: config_nlu if config_nlu is not None else ["test"],
        FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY: domain if domain is not None else ["test"],
        FINGERPRINT_NLG_KEY: nlg if nlg is not None else ["test"],
        FINGERPRINT_TRAINED_AT_KEY: time.time(),
        FINGERPRINT_RASA_VERSION_KEY: rasa_version,
        FINGERPRINT_STORIES_KEY: stories if stories is not None else ["test"],
        FINGERPRINT_NLU_DATA_KEY: nlu if nlu is not None else ["test"],
    }

def test_persist_and_load_fingerprint():
    from rasa.model import persist_fingerprint, fingerprint_from_path

    fingerprint = _fingerprint()
    output_directory = tempfile.mkdtemp()

    persist_fingerprint(output_directory, fingerprint)
    actual = fingerprint_from_path(output_directory)

    assert actual == fingerprint

@pytest.mark.parametrize(
    "fingerprint2, changed",
    [
        (_fingerprint(config=["other"]), True),
        (_fingerprint(config_core=["other"]), True),
        (_fingerprint(domain=["other"]), True),
        (_fingerprint(domain=Domain.empty()), True),
        (_fingerprint(stories=["test", "other"]), True),
        (_fingerprint(rasa_version="100"), True),
        (_fingerprint(config=["other"], domain=["other"]), True),
        (_fingerprint(nlg=["other"]), False),
        (_fingerprint(nlu=["test", "other"]), False),
        (_fingerprint(config_nlu=["other"]), False),
    ],
)
def test_core_fingerprint_changed(fingerprint2, changed):
    fingerprint1 = _fingerprint()
    assert (
        did_section_fingerprint_change(fingerprint1, fingerprint2, SECTION_CORE)
        is changed
    )

@pytest.mark.parametrize(
    "fingerprint2, changed",
    [
        (_fingerprint(config=["other"]), True),
        (_fingerprint(nlu=["test", "other"]), True),
        (_fingerprint(rasa_version="100"), True),
        (_fingerprint(rasa_version="100", config=["other"]), True),
        (_fingerprint(nlg=["other"]), False),
        (_fingerprint(config_core=["other"]), False),
        (_fingerprint(stories=["other"]), False),
    ],
)
def test_nlu_fingerprint_changed(fingerprint2, changed):
    fingerprint1 = _fingerprint()
    assert (
        did_section_fingerprint_change(fingerprint1, fingerprint2, SECTION_NLU)
        is changed
    )

def _project_files(
    project: Text,
    config_file: Text = DEFAULT_CONFIG_PATH,
    domain: Text = DEFAULT_DOMAIN_PATH,
    training_files: Text = DEFAULT_DATA_PATH,
) -> TrainingDataImporter:
    paths = {
        "config_file": config_file,
        "domain_path": domain,
        "training_data_paths": training_files,
    }
    paths = {
        k: v if v is None or Path(v).is_absolute() else os.path.join(project, v)
        for k, v in paths.items()
    }
    paths["training_data_paths"] = [paths["training_data_paths"]]

    return RasaFileImporter(**paths)

@pytest.mark.parametrize(
    "domain_path",
    [
        DEFAULT_DOMAIN_PATH,
        str((Path(".") / DEFAULT_DOMAIN_PATH_WITH_MAPPING).absolute()),
    ],
)
async def test_create_fingerprint_from_paths(project: Text, domain_path: Text):
    project_files = _project_files(project, domain=domain_path)

    assert await model_fingerprint(project_files)

async def test_fingerprinting_changed_response_text(project: Text):
    importer = _project_files(project)

    old_fingerprint = await model_fingerprint(importer)
    old_domain = await importer.get_domain()

    # Change NLG content but keep actions the same
    domain_with_changed_nlg = old_domain.as_dict()
    domain_with_changed_nlg[KEY_RESPONSES]["utter_greet"].append({"text": "hi"})
    domain_with_changed_nlg = Domain.from_dict(domain_with_changed_nlg)

    importer.get_domain = asyncio.coroutine(lambda: domain_with_changed_nlg)

    new_fingerprint = await model_fingerprint(importer)

    assert (
        old_fingerprint[FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY]
        == new_fingerprint[FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY]
    )
    assert old_fingerprint[FINGERPRINT_NLG_KEY] != new_fingerprint[FINGERPRINT_NLG_KEY]

async def test_fingerprinting_additional_action(project: Text):
    importer = _project_files(project)

    old_fingerprint = await model_fingerprint(importer)
    old_domain = await importer.get_domain()

    domain_with_new_action = old_domain.as_dict()
    domain_with_new_action[KEY_RESPONSES]["utter_new"] = [{"text": "hi"}]
    domain_with_new_action = Domain.from_dict(domain_with_new_action)

    importer.get_domain = asyncio.coroutine(lambda: domain_with_new_action)

    new_fingerprint = await model_fingerprint(importer)

    assert (
        old_fingerprint[FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY]
        != new_fingerprint[FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY]
    )
    assert old_fingerprint[FINGERPRINT_NLG_KEY] != new_fingerprint[FINGERPRINT_NLG_KEY]

@pytest.mark.parametrize("use_fingerprint", [True, False])
async def test_rasa_packaging(
    trained_rasa_model: Text, project: Text, use_fingerprint: bool, tmp_path: Path
):
    unpacked_model_path = get_model(trained_rasa_model)

    os.remove(os.path.join(unpacked_model_path, FINGERPRINT_FILE_PATH))
    if use_fingerprint:
        fingerprint = await model_fingerprint(_project_files(project))
    else:
        fingerprint = None

    output_path = str(tmp_path / "test.tar.gz")

    create_package_rasa(unpacked_model_path, output_path, fingerprint)

    unpacked = get_model(output_path)

    assert (
        os.path.exists(os.path.join(unpacked, FINGERPRINT_FILE_PATH)) == use_fingerprint
    )
    assert os.path.exists(os.path.join(unpacked, DEFAULT_CORE_SUBDIRECTORY_NAME))
    assert os.path.exists(os.path.join(unpacked, "nlu"))

    assert not os.path.exists(unpacked_model_path)

@pytest.mark.parametrize(
    "fingerprint",
    [
        {
            "new": _fingerprint(),
            "old": _fingerprint(stories=["others"]),
            "retrain_core": True,
            "retrain_nlu": False,
            "retrain_nlg": True,
        },
        {
            "new": _fingerprint(nlu=["others"]),
            "old": _fingerprint(),
            "retrain_core": False,
            "retrain_nlu": True,
            "retrain_nlg": False,
        },
        {
            "new": _fingerprint(config="others"),
            "old": _fingerprint(),
            "retrain_core": True,
            "retrain_nlu": True,
            "retrain_nlg": True,
        },
        {
            "new": _fingerprint(config_core="others"),
            "old": _fingerprint(),
            "retrain_core": True,
            "retrain_nlu": False,
            "retrain_nlg": True,
        },
        {
            "new": _fingerprint(),
            "old": _fingerprint(config_nlu="others"),
            "retrain_core": False,
            "retrain_nlu": True,
            "retrain_nlg": False,
        },
        {
            "new": _fingerprint(),
            "old": _fingerprint(),
            "retrain_core": False,
            "retrain_nlu": False,
            "retrain_nlg": False,
        },
        {
            "new": _fingerprint(),
            "old": _fingerprint(nlg=["others"]),
            "retrain_core": False,
            "retrain_nlu": False,
            "retrain_nlg": True,
        },
    ],
)
def test_should_retrain(
    trained_rasa_model: Text, fingerprint: Fingerprint, tmp_path: Path
):
    old_model = set_fingerprint(trained_rasa_model, fingerprint["old"], tmp_path)

    retrain = should_retrain(fingerprint["new"], old_model, str(tmp_path))

    assert retrain.should_retrain_core() == fingerprint["retrain_core"]
    assert retrain.should_retrain_nlg() == fingerprint["retrain_nlg"]
    assert retrain.should_retrain_nlu() == fingerprint["retrain_nlu"]

def set_fingerprint(
    trained_rasa_model: Text, fingerprint: Fingerprint, tmp_path: Path
) -> Text:
    unpacked_model_path = get_model(trained_rasa_model)

    os.remove(os.path.join(unpacked_model_path, FINGERPRINT_FILE_PATH))

    output_path = str(tmp_path / "test.tar.gz")

    create_package_rasa(unpacked_model_path, output_path, fingerprint)

    return output_path

@pytest.mark.parametrize(
    "comparison_result,retrain_all,retrain_core,retrain_nlg,retrain_nlu",
    [
        (FingerprintComparisonResult(force_training=True), True, True, True, True),
        (
            FingerprintComparisonResult(core=True, nlu=False, nlg=False),
            True,
            True,
            True,
            False,
        ),
        (
            FingerprintComparisonResult(core=False, nlu=True, nlg=False),
            True,
            False,
            False,
            True,
        ),
        (
            FingerprintComparisonResult(core=True, nlu=True, nlg=False),
            True,
            True,
            True,
            True,
        ),
    ],
)
def test_fingerprint_comparison_result(
    comparison_result: FingerprintComparisonResult,
    retrain_all: bool,
    retrain_core: bool,
    retrain_nlg: bool,
    retrain_nlu: bool,
):
    assert comparison_result.is_training_required() == retrain_all
    assert comparison_result.should_retrain_core() == retrain_core
    assert comparison_result.should_retrain_nlg() == retrain_nlg
    assert comparison_result.should_retrain_nlu() == retrain_nlu

async def test_update_with_new_domain(trained_rasa_model: Text, tmpdir: Path):
    _ = model.unpack_model(trained_rasa_model, tmpdir)

    new_domain = Domain.empty()

    mocked_importer = Mock()

    async def get_domain() -> Domain:
        return new_domain

    mocked_importer.get_domain = get_domain

    await model.update_model_with_new_domain(mocked_importer, tmpdir)

    actual = Domain.load(tmpdir / DEFAULT_CORE_SUBDIRECTORY_NAME / DEFAULT_DOMAIN_PATH)

    assert actual.is_empty()
<commit_msg>fix deepsource warnings<commit_after>import asyncio
import os
import tempfile
import time
import shutil
from pathlib import Path
from typing import Text, Optional, Any
from unittest.mock import Mock

import pytest

from rasa.importers.importer import TrainingDataImporter
from rasa.importers.rasa import RasaFileImporter
from rasa.constants import (
    DEFAULT_CONFIG_PATH,
    DEFAULT_DATA_PATH,
    DEFAULT_DOMAIN_PATH,
    DEFAULT_CORE_SUBDIRECTORY_NAME,
)
from rasa.core.domain import Domain, KEY_RESPONSES
from rasa import model
from rasa.model import (
    FINGERPRINT_CONFIG_KEY,
    FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY,
    FINGERPRINT_NLG_KEY,
    FINGERPRINT_FILE_PATH,
    FINGERPRINT_NLU_DATA_KEY,
    FINGERPRINT_RASA_VERSION_KEY,
    FINGERPRINT_STORIES_KEY,
    FINGERPRINT_TRAINED_AT_KEY,
    FINGERPRINT_CONFIG_CORE_KEY,
    FINGERPRINT_CONFIG_NLU_KEY,
    SECTION_CORE,
    SECTION_NLU,
    create_package_rasa,
    get_latest_model,
    get_model,
    get_model_subdirectories,
    model_fingerprint,
    Fingerprint,
    did_section_fingerprint_change,
    should_retrain,
    FingerprintComparisonResult,
)
from rasa.exceptions import ModelNotFound
from tests.core.conftest import DEFAULT_DOMAIN_PATH_WITH_MAPPING

def test_get_latest_model(trained_rasa_model: str):
    path_of_latest = os.path.join(os.path.dirname(trained_rasa_model), "latest.tar.gz")
    shutil.copy(trained_rasa_model, path_of_latest)

    model_directory = os.path.dirname(path_of_latest)

    assert get_latest_model(model_directory) == path_of_latest

def test_get_model_from_directory(trained_rasa_model: str):
    unpacked = get_model(trained_rasa_model)

    assert os.path.exists(os.path.join(unpacked, DEFAULT_CORE_SUBDIRECTORY_NAME))
    assert os.path.exists(os.path.join(unpacked, "nlu"))

def test_get_model_context_manager(trained_rasa_model: str):
    with get_model(trained_rasa_model) as unpacked:
        assert os.path.exists(unpacked)

    assert not os.path.exists(unpacked)

@pytest.mark.parametrize("model_path", ["foobar", "rasa", "README.md", None])
def test_get_model_exception(model_path: Optional[Text]):
    with pytest.raises(ModelNotFound):
        get_model(model_path)

def test_get_model_from_directory_with_subdirectories(
    trained_rasa_model: Text, tmp_path: Path
):
    unpacked = get_model(trained_rasa_model)
    unpacked_core, unpacked_nlu = get_model_subdirectories(unpacked)

    assert unpacked_core
    assert unpacked_nlu

    with pytest.raises(ModelNotFound):
        get_model_subdirectories(str(tmp_path))  # temp path should be empty

def test_get_model_from_directory_nlu_only(trained_rasa_model: Text):
    unpacked = get_model(trained_rasa_model)
    shutil.rmtree(os.path.join(unpacked, DEFAULT_CORE_SUBDIRECTORY_NAME))
    unpacked_core, unpacked_nlu = get_model_subdirectories(unpacked)

    assert not unpacked_core
    assert unpacked_nlu

def _fingerprint(
    config: Optional[Any] = None,
    config_nlu: Optional[Any] = None,
    config_core: Optional[Any] = None,
    domain: Optional[Any] = None,
    nlg: Optional[Any] = None,
    stories: Optional[Any] = None,
    nlu: Optional[Any] = None,
    rasa_version: Text = "1.0",
):
    return {
        FINGERPRINT_CONFIG_KEY: config if config is not None else ["test"],
        FINGERPRINT_CONFIG_CORE_KEY: config_core
        if config_core is not None
        else ["test"],
        FINGERPRINT_CONFIG_NLU_KEY: config_nlu if config_nlu is not None else ["test"],
        FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY: domain if domain is not None else ["test"],
        FINGERPRINT_NLG_KEY: nlg if nlg is not None else ["test"],
        FINGERPRINT_TRAINED_AT_KEY: time.time(),
        FINGERPRINT_RASA_VERSION_KEY: rasa_version,
        FINGERPRINT_STORIES_KEY: stories if stories is not None else ["test"],
        FINGERPRINT_NLU_DATA_KEY: nlu if nlu is not None else ["test"],
    }

def test_persist_and_load_fingerprint():
    from rasa.model import persist_fingerprint, fingerprint_from_path

    fingerprint = _fingerprint()
    output_directory = tempfile.mkdtemp()

    persist_fingerprint(output_directory, fingerprint)
    actual = fingerprint_from_path(output_directory)

    assert actual == fingerprint

@pytest.mark.parametrize(
    "fingerprint2, changed",
    [
        (_fingerprint(config=["other"]), True),
        (_fingerprint(config_core=["other"]), True),
        (_fingerprint(domain=["other"]), True),
        (_fingerprint(domain=Domain.empty()), True),
        (_fingerprint(stories=["test", "other"]), True),
        (_fingerprint(rasa_version="100"), True),
        (_fingerprint(config=["other"], domain=["other"]), True),
        (_fingerprint(nlg=["other"]), False),
        (_fingerprint(nlu=["test", "other"]), False),
        (_fingerprint(config_nlu=["other"]), False),
    ],
)
def test_core_fingerprint_changed(fingerprint2, changed):
    fingerprint1 = _fingerprint()
    assert (
        did_section_fingerprint_change(fingerprint1, fingerprint2, SECTION_CORE)
        is changed
    )

@pytest.mark.parametrize(
    "fingerprint2, changed",
    [
        (_fingerprint(config=["other"]), True),
        (_fingerprint(nlu=["test", "other"]), True),
        (_fingerprint(rasa_version="100"), True),
        (_fingerprint(rasa_version="100", config=["other"]), True),
        (_fingerprint(nlg=["other"]), False),
        (_fingerprint(config_core=["other"]), False),
        (_fingerprint(stories=["other"]), False),
    ],
)
def test_nlu_fingerprint_changed(fingerprint2, changed):
    fingerprint1 = _fingerprint()
    assert (
        did_section_fingerprint_change(fingerprint1, fingerprint2, SECTION_NLU)
        is changed
    )

def _project_files(
    project: Text,
    config_file: Text = DEFAULT_CONFIG_PATH,
    domain: Text = DEFAULT_DOMAIN_PATH,
    training_files: Text = DEFAULT_DATA_PATH,
) -> TrainingDataImporter:
    paths = {
        "config_file": config_file,
        "domain_path": domain,
        "training_data_paths": training_files,
    }
    paths = {
        k: v if v is None or Path(v).is_absolute() else os.path.join(project, v)
        for k, v in paths.items()
    }
    paths["training_data_paths"] = [paths["training_data_paths"]]

    return RasaFileImporter(**paths)

@pytest.mark.parametrize(
    "domain_path",
    [
        DEFAULT_DOMAIN_PATH,
        str((Path(".") / DEFAULT_DOMAIN_PATH_WITH_MAPPING).absolute()),
    ],
)
async def test_create_fingerprint_from_paths(project: Text, domain_path: Text):
    project_files = _project_files(project, domain=domain_path)

    assert await model_fingerprint(project_files)

async def test_fingerprinting_changed_response_text(project: Text):
    importer = _project_files(project)

    old_fingerprint = await model_fingerprint(importer)
    old_domain = await importer.get_domain()

    # Change NLG content but keep actions the same
    domain_with_changed_nlg = old_domain.as_dict()
    domain_with_changed_nlg[KEY_RESPONSES]["utter_greet"].append({"text": "hi"})
    domain_with_changed_nlg = Domain.from_dict(domain_with_changed_nlg)

    importer.get_domain = asyncio.coroutine(lambda: domain_with_changed_nlg)

    new_fingerprint = await model_fingerprint(importer)

    assert (
        old_fingerprint[FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY]
        == new_fingerprint[FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY]
    )
    assert old_fingerprint[FINGERPRINT_NLG_KEY] != new_fingerprint[FINGERPRINT_NLG_KEY]

async def test_fingerprinting_additional_action(project: Text):
    importer = _project_files(project)

    old_fingerprint = await model_fingerprint(importer)
    old_domain = await importer.get_domain()

    domain_with_new_action = old_domain.as_dict()
    domain_with_new_action[KEY_RESPONSES]["utter_new"] = [{"text": "hi"}]
    domain_with_new_action = Domain.from_dict(domain_with_new_action)

    importer.get_domain = asyncio.coroutine(lambda: domain_with_new_action)

    new_fingerprint = await model_fingerprint(importer)

    assert (
        old_fingerprint[FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY]
        != new_fingerprint[FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY]
    )
    assert old_fingerprint[FINGERPRINT_NLG_KEY] != new_fingerprint[FINGERPRINT_NLG_KEY]

@pytest.mark.parametrize("use_fingerprint", [True, False])
async def test_rasa_packaging(
    trained_rasa_model: Text, project: Text, use_fingerprint: bool, tmp_path: Path
):
    unpacked_model_path = get_model(trained_rasa_model)

    os.remove(os.path.join(unpacked_model_path, FINGERPRINT_FILE_PATH))
    if use_fingerprint:
        fingerprint = await model_fingerprint(_project_files(project))
    else:
        fingerprint = None

    output_path = str(tmp_path / "test.tar.gz")

    create_package_rasa(unpacked_model_path, output_path, fingerprint)

    unpacked = get_model(output_path)

    assert (
        os.path.exists(os.path.join(unpacked, FINGERPRINT_FILE_PATH)) == use_fingerprint
    )
    assert os.path.exists(os.path.join(unpacked, DEFAULT_CORE_SUBDIRECTORY_NAME))
    assert os.path.exists(os.path.join(unpacked, "nlu"))

    assert not os.path.exists(unpacked_model_path)

@pytest.mark.parametrize(
    "fingerprint",
    [
        {
            "new": _fingerprint(),
            "old": _fingerprint(stories=["others"]),
            "retrain_core": True,
            "retrain_nlu": False,
            "retrain_nlg": True,
        },
        {
            "new": _fingerprint(nlu=["others"]),
            "old": _fingerprint(),
            "retrain_core": False,
            "retrain_nlu": True,
            "retrain_nlg": False,
        },
        {
            "new": _fingerprint(config="others"),
            "old": _fingerprint(),
            "retrain_core": True,
            "retrain_nlu": True,
            "retrain_nlg": True,
        },
        {
            "new": _fingerprint(config_core="others"),
            "old": _fingerprint(),
            "retrain_core": True,
            "retrain_nlu": False,
            "retrain_nlg": True,
        },
        {
            "new": _fingerprint(),
            "old": _fingerprint(config_nlu="others"),
            "retrain_core": False,
            "retrain_nlu": True,
            "retrain_nlg": False,
        },
        {
            "new": _fingerprint(),
            "old": _fingerprint(),
            "retrain_core": False,
            "retrain_nlu": False,
            "retrain_nlg": False,
        },
        {
            "new": _fingerprint(),
            "old": _fingerprint(nlg=["others"]),
            "retrain_core": False,
            "retrain_nlu": False,
            "retrain_nlg": True,
        },
    ],
)
def test_should_retrain(
    trained_rasa_model: Text, fingerprint: Fingerprint, tmp_path: Path
):
    old_model = set_fingerprint(trained_rasa_model, fingerprint["old"], tmp_path)

    retrain = should_retrain(fingerprint["new"], old_model, str(tmp_path))

    assert retrain.should_retrain_core() == fingerprint["retrain_core"]
    assert retrain.should_retrain_nlg() == fingerprint["retrain_nlg"]
    assert retrain.should_retrain_nlu() == fingerprint["retrain_nlu"]

def set_fingerprint(
    trained_rasa_model: Text, fingerprint: Fingerprint, tmp_path: Path
) -> Text:
    unpacked_model_path = get_model(trained_rasa_model)

    os.remove(os.path.join(unpacked_model_path, FINGERPRINT_FILE_PATH))

    output_path = str(tmp_path / "test.tar.gz")

    create_package_rasa(unpacked_model_path, output_path, fingerprint)

    return output_path

@pytest.mark.parametrize(
    "comparison_result,retrain_all,retrain_core,retrain_nlg,retrain_nlu",
    [
        (FingerprintComparisonResult(force_training=True), True, True, True, True),
        (
            FingerprintComparisonResult(core=True, nlu=False, nlg=False),
            True,
            True,
            True,
            False,
        ),
        (
            FingerprintComparisonResult(core=False, nlu=True, nlg=False),
            True,
            False,
            False,
            True,
        ),
        (
            FingerprintComparisonResult(core=True, nlu=True, nlg=False),
            True,
            True,
            True,
            True,
        ),
    ],
)
def test_fingerprint_comparison_result(
    comparison_result: FingerprintComparisonResult,
    retrain_all: bool,
    retrain_core: bool,
    retrain_nlg: bool,
    retrain_nlu: bool,
):
    assert comparison_result.is_training_required() == retrain_all
    assert comparison_result.should_retrain_core() == retrain_core
    assert comparison_result.should_retrain_nlg() == retrain_nlg
    assert comparison_result.should_retrain_nlu() == retrain_nlu

async def test_update_with_new_domain(trained_rasa_model: Text, tmpdir: Path):
    _ = model.unpack_model(trained_rasa_model, tmpdir)

    new_domain = Domain.empty()

    mocked_importer = Mock()

    async def get_domain() -> Domain:
        return new_domain

    mocked_importer.get_domain = get_domain

    await model.update_model_with_new_domain(mocked_importer, tmpdir)

    actual = Domain.load(tmpdir / DEFAULT_CORE_SUBDIRECTORY_NAME / DEFAULT_DOMAIN_PATH)

    assert actual.is_empty()
<|endoftext|>


<commit_before>from __future__ import absolute_import

import datetime
from unittest import skipIf

try:
    import pytz
except ImportError:
    pytz = None

from django.test import TestCase
from django.test.utils import override_settings
from django.utils import timezone

from .models import Article, Comment, Category

class DateTimesTests(TestCase):
    def test_related_model_traverse(self):
        a1 = Article.objects.create(
            title="First one",
            pub_date=datetime.datetime(2005, 7, 28, 9, 0, 0),
        )
        a2 = Article.objects.create(
            title="Another one",
            pub_date=datetime.datetime(2010, 7, 28, 10, 0, 0),
        )
        a3 = Article.objects.create(
            title="Third one, in the first day",
            pub_date=datetime.datetime(2005, 7, 28, 17, 0, 0),
        )

        a1.comments.create(
            text="Im the HULK!",
            pub_date=datetime.datetime(2005, 7, 28, 9, 30, 0),
        )
        a1.comments.create(
            text="HULK SMASH!",
            pub_date=datetime.datetime(2005, 7, 29, 1, 30, 0),
        )
        a2.comments.create(
            text="LMAO",
            pub_date=datetime.datetime(2010, 7, 28, 10, 10, 10),
        )
        a3.comments.create(
            text="+1",
            pub_date=datetime.datetime(2005, 8, 29, 10, 10, 10),
        )

        c = Category.objects.create(name="serious-news")
        c.articles.add(a1, a3)

        self.assertQuerysetEqual(
            Comment.objects.datetimes("article__pub_date", "year"), [
                datetime.datetime(2005, 1, 1),
                datetime.datetime(2010, 1, 1),
            ],
            lambda d: d,
        )
        self.assertQuerysetEqual(
            Comment.objects.datetimes("article__pub_date", "month"), [
                datetime.datetime(2005, 7, 1),
                datetime.datetime(2010, 7, 1),
            ],
            lambda d: d
        )
        self.assertQuerysetEqual(
            Comment.objects.datetimes("article__pub_date", "day"), [
                datetime.datetime(2005, 7, 28),
                datetime.datetime(2010, 7, 28),
            ],
            lambda d: d
        )
        self.assertQuerysetEqual(
            Article.objects.datetimes("comments__pub_date", "day"), [
                datetime.datetime(2005, 7, 28),
                datetime.datetime(2005, 7, 29),
                datetime.datetime(2005, 8, 29),
                datetime.datetime(2010, 7, 28),
            ],
            lambda d: d
        )
        self.assertQuerysetEqual(
            Article.objects.datetimes("comments__approval_date", "day"), []
        )
        self.assertQuerysetEqual(
            Category.objects.datetimes("articles__pub_date", "day"), [
                datetime.datetime(2005, 7, 28),
            ],
            lambda d: d,
        )

    @skipIf(pytz is None, "this test requires pytz")
    @override_settings(USE_TZ=True)
    def test_21432(self):
        now = timezone.localtime(timezone.now().replace(microsecond=0))
        Article.objects.create(title="First one", pub_date=now)
        qs = Article.objects.datetimes('pub_date', 'second')
        self.assertEqual(qs[0], now)
<commit_msg>Fixed Python2.6 incompatibility caused by 67c30426c1370f5d6c39bd73888c3902c1c5f365.<commit_after>from __future__ import absolute_import

import datetime

try:
    import pytz
except ImportError:
    pytz = None

from django.test import TestCase
from django.test.utils import override_settings
from django.utils import timezone
from django.utils.unittest import skipIf

from .models import Article, Comment, Category

class DateTimesTests(TestCase):
    def test_related_model_traverse(self):
        a1 = Article.objects.create(
            title="First one",
            pub_date=datetime.datetime(2005, 7, 28, 9, 0, 0),
        )
        a2 = Article.objects.create(
            title="Another one",
            pub_date=datetime.datetime(2010, 7, 28, 10, 0, 0),
        )
        a3 = Article.objects.create(
            title="Third one, in the first day",
            pub_date=datetime.datetime(2005, 7, 28, 17, 0, 0),
        )

        a1.comments.create(
            text="Im the HULK!",
            pub_date=datetime.datetime(2005, 7, 28, 9, 30, 0),
        )
        a1.comments.create(
            text="HULK SMASH!",
            pub_date=datetime.datetime(2005, 7, 29, 1, 30, 0),
        )
        a2.comments.create(
            text="LMAO",
            pub_date=datetime.datetime(2010, 7, 28, 10, 10, 10),
        )
        a3.comments.create(
            text="+1",
            pub_date=datetime.datetime(2005, 8, 29, 10, 10, 10),
        )

        c = Category.objects.create(name="serious-news")
        c.articles.add(a1, a3)

        self.assertQuerysetEqual(
            Comment.objects.datetimes("article__pub_date", "year"), [
                datetime.datetime(2005, 1, 1),
                datetime.datetime(2010, 1, 1),
            ],
            lambda d: d,
        )
        self.assertQuerysetEqual(
            Comment.objects.datetimes("article__pub_date", "month"), [
                datetime.datetime(2005, 7, 1),
                datetime.datetime(2010, 7, 1),
            ],
            lambda d: d
        )
        self.assertQuerysetEqual(
            Comment.objects.datetimes("article__pub_date", "day"), [
                datetime.datetime(2005, 7, 28),
                datetime.datetime(2010, 7, 28),
            ],
            lambda d: d
        )
        self.assertQuerysetEqual(
            Article.objects.datetimes("comments__pub_date", "day"), [
                datetime.datetime(2005, 7, 28),
                datetime.datetime(2005, 7, 29),
                datetime.datetime(2005, 8, 29),
                datetime.datetime(2010, 7, 28),
            ],
            lambda d: d
        )
        self.assertQuerysetEqual(
            Article.objects.datetimes("comments__approval_date", "day"), []
        )
        self.assertQuerysetEqual(
            Category.objects.datetimes("articles__pub_date", "day"), [
                datetime.datetime(2005, 7, 28),
            ],
            lambda d: d,
        )

    @skipIf(pytz is None, "this test requires pytz")
    @override_settings(USE_TZ=True)
    def test_21432(self):
        now = timezone.localtime(timezone.now().replace(microsecond=0))
        Article.objects.create(title="First one", pub_date=now)
        qs = Article.objects.datetimes('pub_date', 'second')
        self.assertEqual(qs[0], now)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# name:             test_file.py
# author:           Harold Bradley III
# email:            harold@bradleystudio.net
# created on:       11/12/2015
#
# description:      A unit test for ext_pylib file module's File class and
#                   methods. This is fairly ugly, but it's pretty
#                   comprehensive.

from __future__ import print_function, unicode_literals

from ext_pylib.files import File
from mock import mock_open, patch
import pytest
from sys import version_info

if version_info[0] == 2 or version_info.major == 2:
    BUILTINS = '__builtin__'
else:
    BUILTINS = 'builtins'

class MockParentDir(object):
    """A Mock class for parent_dir property."""
    def __init__(self, _exists):
        """MockParentDir init method."""
        self._exists = _exists
        self.exists_called, self.create_called = 0, 0
    def exists(self):
        """MockParentDir exists method."""
        self.exists_called += 1
        return self._exists
    def create(self):
        """MockParentDir create method."""
        self.create_called += 1

class MockHandle(object):
    """A Mock class for a handle."""
    data = None
    def write(self, data):
        """MockHande write method."""
        self.data = data
    def called_once_with(self, var):
        """MockHande called_once_with method."""
        if var != self.data:
            print('Mock handle was not called with: ' + var)
            return False
        return True

DEFAULT_ARGS = { 'path' : '/tmp/nonexistant/path/file' }

init_args = [
    (None, '<file.File:stub>'),
    ('/this/file', '/this/file'),
    ('/this//path//file', '/this/path/file'),
]
@pytest.mark.parametrize(("atts", "expected"), init_args)
def test_file_initialize(atts, expected):
    """Tests initialize File."""
    file = File({'path' : atts})
    assert str(file) == expected

def test_file_initialize_with_trailing_slash():
    """Tests initializing a File with a trailing slash."""
    with pytest.raises(ValueError):
        file = File({'path' : '/this/path/'})

def test_file_create_stub():
    """Tests file creation of a stub."""
    assert File().create()

@patch('ext_pylib.files.file.prompt')
@patch('ext_pylib.files.node.Node.exists')
def test_file_create_already_existing_file_not_replacing(mock_exists, mock_prompt):
    """Tests file creation of an already existing file (NOT replacing the file)."""
    mock_exists.return_value = True
    mock_prompt.return_value = False # Answer no, don't replace
    file = File(DEFAULT_ARGS)
    assert not file.create()

@patch('ext_pylib.files.node.Node.chown')
@patch('ext_pylib.files.node.Node.chmod')
@patch('ext_pylib.files.file.prompt')
@patch('ext_pylib.files.node.Node.exists')
def test_file_create_already_existing_file_replacing(mock_exists, mock_prompt, mock_chmod, mock_chown):
    """Tests file creation of an already existing file (replacing the file)."""
    mock_parent_dir = MockParentDir(True)
    mock_exists.return_value = True
    mock_chown.return_value = mock_chmod.return_value = True
    mock_prompt.return_value = True # Answer yes, replace
    file = File(DEFAULT_ARGS)
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        assert file.create()
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w')
        m_open().close.assert_called_once()

@patch('ext_pylib.files.node.Node.chown')
@patch('ext_pylib.files.node.Node.chmod')
@patch('ext_pylib.files.node.Node.exists')
def test_file_create_and_create_parent_dirs(mock_exists, mock_chmod, mock_chown):
    """Tests file creation while creating parent dirs."""
    mock_parent_dir = MockParentDir(False)
    mock_exists.return_value = False
    mock_chown.return_value = mock_chmod.return_value = True
    File.parent_dir = mock_parent_dir
    file = File(DEFAULT_ARGS)
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        assert file.create()
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w')
        m_open().close.assert_called_once()
        assert mock_parent_dir.exists_called == 1
        assert mock_parent_dir.create_called == 1

@patch('ext_pylib.files.node.Node.chown')
@patch('ext_pylib.files.node.Node.chmod')
@patch('ext_pylib.files.node.Node.exists')
@patch('ext_pylib.files.file.File.write')
def test_file_create_with_data(mock_write, mock_exists, mock_chmod, mock_chown):
    """Tests file creation with data."""
    mock_parent_dir = MockParentDir(True)
    mock_exists.return_value = False
    mock_chown.return_value = mock_chmod.return_value = True
    File.parent_dir = mock_parent_dir
    file = File(DEFAULT_ARGS)
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        data = 'The data...'
        assert file.create(data)
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w')
        mock_write.assert_called_once_with(data, False, m_open())
        m_open().close.assert_called_once()
        assert mock_parent_dir.exists_called == 1
        assert mock_parent_dir.create_called == 0

@patch('ext_pylib.files.node.Node.chown')
@patch('ext_pylib.files.node.Node.chmod')
@patch('ext_pylib.files.node.Node.exists')
@patch('ext_pylib.files.file.File.write')
def test_file_create_with_data_but_not_as_arg(mock_write, mock_exists, mock_chmod, mock_chown):
    """Tests file creation with data."""
    mock_parent_dir = MockParentDir(True)
    mock_exists.return_value = False
    mock_chown.return_value = mock_chmod.return_value = True
    File.parent_dir = mock_parent_dir
    file = File(DEFAULT_ARGS)
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        data = 'The data...'
        file.data = data
        assert file.create()
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w')
        mock_write.assert_called_once_with(data, False, m_open())
        m_open().close.assert_called_once()
        assert mock_parent_dir.exists_called == 1
        assert mock_parent_dir.create_called == 0

def test_file_write_no_data():
    """Tests writing to a File with no data passed in."""
    file = File(DEFAULT_ARGS)
    with pytest.raises(UnboundLocalError):
        file.write()

def test_file_write_data_with_handle():
    """Tests writing to a File with a handle passed in."""
    file = File(DEFAULT_ARGS)
    mock_handle = MockHandle()
    data = 'Some mock data...'
    file.write(data=data, handle=mock_handle)
    assert mock_handle.called_once_with(data)

def test_file_write_append_data_without_handle():
    """Tests appending to a file without a handle."""
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        file = File(DEFAULT_ARGS)
        data = 'Some mock data...'
        assert file.write(data)
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'a')
        m_open().write.assert_called_once_with(data)
        m_open().close.assert_called_once()

def test_file_write_data_without_handle():
    """Tests writing to a file without a handle."""
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        file = File(DEFAULT_ARGS)
        data = 'Some mock data...'
        assert file.write(data, False)
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w')
        m_open().write.assert_called_once_with(data)
        m_open().close.assert_called_once()

@patch('ext_pylib.files.file.File.write')
def test_file_append(mock_write):
    """Tests appending to file."""
    File().append(None)
    mock_write.assert_called_once_with(None, True, None)

@patch('ext_pylib.files.file.File.write')
def test_file_overwrite(mock_write):
    """Tests overwriting a file."""
    File().overwrite(None)
    mock_write.assert_called_once_with(None, False, None)

@patch('ext_pylib.files.node.Node.exists')
@patch('os.remove')
def test_file_remove(mock_remove, mock_exists):
    """Tests File remove method."""
    mock_exists.return_value = True
    file = File(DEFAULT_ARGS)
    assert file.remove(False)
    mock_remove.assert_called_once_with(DEFAULT_ARGS['path'])

@patch('ext_pylib.files.node.Node.exists')
def test_file_read_nonexisting_file(mock_exists):
    """Tests File read method."""
    mock_exists.return_value = False
    file = File(DEFAULT_ARGS)
    assert file.read() == ''

@patch('ext_pylib.files.node.Node.exists')
def test_file_read_nonexisting_file_with_data_in_memory(mock_exists):
    """Tests File read method."""
    mock_exists.return_value = False
    file = File(DEFAULT_ARGS)
    file.data = 'Data is in memory...'
    assert file.read() == 'Data is in memory...'

@patch('ext_pylib.files.node.Node.exists')
def test_file_read_file_force_clear_memory(mock_exists):
    """Tests file read method forcing read from memory."""
    mock_exists.return_value = True
    file = File(DEFAULT_ARGS)
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        m_open.return_value.read.return_value = data_on_disk = 'The data on disk..'
        file.data = data_in_memory = 'The data...'
        assert file.read(True) == data_on_disk
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'r')
        m_open().close.assert_called_once()

@patch('ext_pylib.files.node.Node.exists')
def test_file_read_file_with_data(mock_exists):
    """Tests file read() a file that has data altered in memory."""
    mock_exists.return_value = True
    file = File(DEFAULT_ARGS)
    file.data = 'Test data'
    assert file.read() == file.data
    assert file.read() == 'Test data'
    file.data = 'New data'
    assert file.read() == file.data
    assert file.read() == 'New data'
<commit_msg>Removes assert_called_once() -- evidently not a real function.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# name:             test_file.py
# author:           Harold Bradley III
# email:            harold@bradleystudio.net
# created on:       11/12/2015
#
# description:      A unit test for ext_pylib file module's File class and
#                   methods. This is fairly ugly, but it's pretty
#                   comprehensive.

from __future__ import print_function, unicode_literals

from ext_pylib.files import File
from mock import mock_open, patch
import pytest
from sys import version_info

if version_info[0] == 2 or version_info.major == 2:
    BUILTINS = '__builtin__'
else:
    BUILTINS = 'builtins'

class MockParentDir(object):
    """A Mock class for parent_dir property."""
    def __init__(self, _exists):
        """MockParentDir init method."""
        self._exists = _exists
        self.exists_called, self.create_called = 0, 0
    def exists(self):
        """MockParentDir exists method."""
        self.exists_called += 1
        return self._exists
    def create(self):
        """MockParentDir create method."""
        self.create_called += 1

class MockHandle(object):
    """A Mock class for a handle."""
    data = None
    def write(self, data):
        """MockHande write method."""
        self.data = data
    def called_once_with(self, var):
        """MockHande called_once_with method."""
        if var != self.data:
            print('Mock handle was not called with: ' + var)
            return False
        return True

DEFAULT_ARGS = { 'path' : '/tmp/nonexistant/path/file' }

init_args = [
    (None, '<file.File:stub>'),
    ('/this/file', '/this/file'),
    ('/this//path//file', '/this/path/file'),
]
@pytest.mark.parametrize(("atts", "expected"), init_args)
def test_file_initialize(atts, expected):
    """Tests initialize File."""
    file = File({'path' : atts})
    assert str(file) == expected

def test_file_initialize_with_trailing_slash():
    """Tests initializing a File with a trailing slash."""
    with pytest.raises(ValueError):
        file = File({'path' : '/this/path/'})

def test_file_create_stub():
    """Tests file creation of a stub."""
    assert File().create()

@patch('ext_pylib.files.file.prompt')
@patch('ext_pylib.files.node.Node.exists')
def test_file_create_already_existing_file_not_replacing(mock_exists, mock_prompt):
    """Tests file creation of an already existing file (NOT replacing the file)."""
    mock_exists.return_value = True
    mock_prompt.return_value = False # Answer no, don't replace
    file = File(DEFAULT_ARGS)
    assert not file.create()

@patch('ext_pylib.files.node.Node.chown')
@patch('ext_pylib.files.node.Node.chmod')
@patch('ext_pylib.files.file.prompt')
@patch('ext_pylib.files.node.Node.exists')
def test_file_create_already_existing_file_replacing(mock_exists, mock_prompt, mock_chmod, mock_chown):
    """Tests file creation of an already existing file (replacing the file)."""
    mock_parent_dir = MockParentDir(True)
    mock_exists.return_value = True
    mock_chown.return_value = mock_chmod.return_value = True
    mock_prompt.return_value = True # Answer yes, replace
    file = File(DEFAULT_ARGS)
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        assert file.create()
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w')
        m_open().close.assert_called_once_with()

@patch('ext_pylib.files.node.Node.chown')
@patch('ext_pylib.files.node.Node.chmod')
@patch('ext_pylib.files.node.Node.exists')
def test_file_create_and_create_parent_dirs(mock_exists, mock_chmod, mock_chown):
    """Tests file creation while creating parent dirs."""
    mock_parent_dir = MockParentDir(False)
    mock_exists.return_value = False
    mock_chown.return_value = mock_chmod.return_value = True
    File.parent_dir = mock_parent_dir
    file = File(DEFAULT_ARGS)
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        assert file.create()
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w')
        m_open().close.assert_called_once_with()
        assert mock_parent_dir.exists_called == 1
        assert mock_parent_dir.create_called == 1

@patch('ext_pylib.files.node.Node.chown')
@patch('ext_pylib.files.node.Node.chmod')
@patch('ext_pylib.files.node.Node.exists')
@patch('ext_pylib.files.file.File.write')
def test_file_create_with_data(mock_write, mock_exists, mock_chmod, mock_chown):
    """Tests file creation with data."""
    mock_parent_dir = MockParentDir(True)
    mock_exists.return_value = False
    mock_chown.return_value = mock_chmod.return_value = True
    File.parent_dir = mock_parent_dir
    file = File(DEFAULT_ARGS)
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        data = 'The data...'
        assert file.create(data)
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w')
        mock_write.assert_called_once_with(data, False, m_open())
        m_open().close.assert_called_once_with()
        assert mock_parent_dir.exists_called == 1
        assert mock_parent_dir.create_called == 0

@patch('ext_pylib.files.node.Node.chown')
@patch('ext_pylib.files.node.Node.chmod')
@patch('ext_pylib.files.node.Node.exists')
@patch('ext_pylib.files.file.File.write')
def test_file_create_with_data_but_not_as_arg(mock_write, mock_exists, mock_chmod, mock_chown):
    """Tests file creation with data."""
    mock_parent_dir = MockParentDir(True)
    mock_exists.return_value = False
    mock_chown.return_value = mock_chmod.return_value = True
    File.parent_dir = mock_parent_dir
    file = File(DEFAULT_ARGS)
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        data = 'The data...'
        file.data = data
        assert file.create()
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w')
        mock_write.assert_called_once_with(data, False, m_open())
        m_open().close.assert_called_once_with()
        assert mock_parent_dir.exists_called == 1
        assert mock_parent_dir.create_called == 0

def test_file_write_no_data():
    """Tests writing to a File with no data passed in."""
    file = File(DEFAULT_ARGS)
    with pytest.raises(UnboundLocalError):
        file.write()

def test_file_write_data_with_handle():
    """Tests writing to a File with a handle passed in."""
    file = File(DEFAULT_ARGS)
    mock_handle = MockHandle()
    data = 'Some mock data...'
    file.write(data=data, handle=mock_handle)
    assert mock_handle.called_once_with(data)

def test_file_write_append_data_without_handle():
    """Tests appending to a file without a handle."""
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        file = File(DEFAULT_ARGS)
        data = 'Some mock data...'
        assert file.write(data)
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'a')
        m_open().write.assert_called_once_with(data)
        m_open().close.assert_called_once_with()

def test_file_write_data_without_handle():
    """Tests writing to a file without a handle."""
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        file = File(DEFAULT_ARGS)
        data = 'Some mock data...'
        assert file.write(data, False)
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w')
        m_open().write.assert_called_once_with(data)
        m_open().close.assert_called_once_with()

@patch('ext_pylib.files.file.File.write')
def test_file_append(mock_write):
    """Tests appending to file."""
    File().append(None)
    mock_write.assert_called_once_with(None, True, None)

@patch('ext_pylib.files.file.File.write')
def test_file_overwrite(mock_write):
    """Tests overwriting a file."""
    File().overwrite(None)
    mock_write.assert_called_once_with(None, False, None)

@patch('ext_pylib.files.node.Node.exists')
@patch('os.remove')
def test_file_remove(mock_remove, mock_exists):
    """Tests File remove method."""
    mock_exists.return_value = True
    file = File(DEFAULT_ARGS)
    assert file.remove(False)
    mock_remove.assert_called_once_with(DEFAULT_ARGS['path'])

@patch('ext_pylib.files.node.Node.exists')
def test_file_read_nonexisting_file(mock_exists):
    """Tests File read method."""
    mock_exists.return_value = False
    file = File(DEFAULT_ARGS)
    assert file.read() == ''

@patch('ext_pylib.files.node.Node.exists')
def test_file_read_nonexisting_file_with_data_in_memory(mock_exists):
    """Tests File read method."""
    mock_exists.return_value = False
    file = File(DEFAULT_ARGS)
    file.data = 'Data is in memory...'
    assert file.read() == 'Data is in memory...'

@patch('ext_pylib.files.node.Node.exists')
def test_file_read_file_force_clear_memory(mock_exists):
    """Tests file read method forcing read from memory."""
    mock_exists.return_value = True
    file = File(DEFAULT_ARGS)
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        m_open.return_value.read.return_value = data_on_disk = 'The data on disk..'
        file.data = data_in_memory = 'The data...'
        assert file.read(True) == data_on_disk
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'r')
        m_open().close.assert_called_once_with()

@patch('ext_pylib.files.node.Node.exists')
def test_file_read_file_with_data(mock_exists):
    """Tests file read() a file that has data altered in memory."""
    mock_exists.return_value = True
    file = File(DEFAULT_ARGS)
    file.data = 'Test data'
    assert file.read() == file.data
    assert file.read() == 'Test data'
    file.data = 'New data'
    assert file.read() == file.data
    assert file.read() == 'New data'
<|endoftext|>