#
# Copyright 2012 WebFilings, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import unittest

from mock import patch


class ThrowAway(object):
    @classmethod
    def run_me(cls):
        cls.i_was_ran = True


class TestGetFunctionPathAndOptions(unittest.TestCase):
    """Make sure junk paths raise exceptions and function args get remapped
    to a path.

    Ensure any specified defaults for functions passed by reference are
    correctly returned.
    """

    def test_valid_name(self):
        """Ensure check job function accepts good input."""
        from furious.job_utils import get_function_path_and_options

        good_names = ['name', 'good.name', 'gr8.name', 'ok.na_me',
                      'Name', 'Good.Name', 'Gr8.Name', 'ok.Na_me']
        for name in good_names:
            cleansed_path, options = get_function_path_and_options(name)
            self.assertEqual(name, cleansed_path)
            self.assertIsNone(options)

    def test_bad_function_path(self):
        """Ensure get_function_path_and_options function raises
        BadFunctionPathError when given a bad path.
        """
        from furious.job_utils import BadFunctionPathError
        from furious.job_utils import get_function_path_and_options

        bad_names = ['', '0abc', 'test.0abc', 'test.ab-cd',
                     'bad%ness', '.nogood']
        for bad_name in bad_names:
            self.assertRaises(
                BadFunctionPathError, get_function_path_and_options, bad_name)

    def test_none_as_function_path(self):
        """Ensure get_function_path_and_options raises BadFunctionPathError
        on missing path.
        """
        from furious.job_utils import BadFunctionPathError
        from furious.job_utils import get_function_path_and_options

        self.assertRaises(
            BadFunctionPathError, get_function_path_and_options, None)

    def test_gets_callable_path(self):
        """Ensure check job function returns the path of a callable."""
        from furious.job_utils import get_function_path_and_options