Repository: spotify/luigi
File: test/task_test.py (Python)

Code before:
    def test_externalized_task_picklable(self):
        task = luigi.task.externalize(luigi.Task())
        pickled_task = pickle.dumps(task)
        self.assertEqual(task, pickle.loads(pickled_task))

    def test_no_unpicklable_properties(self):
        task = luigi.Task()
        task.set_tracking_url = lambda tracking_url: tracking_url
        task.set_status_message = lambda message: message
        with task.no_unpicklable_properties():
            pickle.dumps(task)
        self.assertIsNotNone(task.set_tracking_url)
        self.assertIsNotNone(task.set_status_message)
        tracking_url = task.set_tracking_url('http://test.luigi.com/')
        self.assertEqual(tracking_url, 'http://test.luigi.com/')
        message = task.set_status_message('message')
        self.assertEqual(message, 'message')

    def test_no_warn_if_param_types_ok(self):
        with warnings.catch_warnings(record=True) as w:
            DummyTask(**DUMMY_TASK_OK_PARAMS)
        self.assertEqual(len(w), 0, msg='No warning should be raised when correct parameter types are used')

    if six.PY3:  # assertWarnsRegex was introduced in Python 3.2
        def test_warn_on_non_str_param(self):
            params = dict(**DUMMY_TASK_OK_PARAMS)
            params['param'] = 42
            with self.assertWarnsRegex(UserWarning, 'Parameter "param" with value "42" is not of type string.'):
                DummyTask(**params)

        def test_warn_on_non_timedelta_param(self):
            params = dict(**DUMMY_TASK_OK_PARAMS)

            class MockTimedelta(object):
                days = 1
                seconds = 1

            params['timedelta_param'] = MockTimedelta()
            with self.assertWarnsRegex(UserWarning, 'Parameter "timedelta_param" with value ".*" is not of type timedelta.'):
                DummyTask(**params)

class ExternalizeTaskTest(LuigiTestCase):

    def test_externalize_taskclass(self):
        class MyTask(luigi.Task):
            def run(self):
                pass

        self.assertIsNotNone(MyTask.run)  # Assert what we believe
        task_object = luigi.task.externalize(MyTask)()
        self.assertIsNone(task_object.run)

Review comment (suggestion):
Can't you just use `DUMMY_TASK_OK_PARAMS` directly? :)

Code after:
    def test_externalized_task_picklable(self):
        task = luigi.task.externalize(luigi.Task())
        pickled_task = pickle.dumps(task)
        self.assertEqual(task, pickle.loads(pickled_task))

    def test_no_unpicklable_properties(self):
        task = luigi.Task()
        task.set_tracking_url = lambda tracking_url: tracking_url
        task.set_status_message = lambda message: message
        with task.no_unpicklable_properties():
            pickle.dumps(task)
        self.assertIsNotNone(task.set_tracking_url)
        self.assertIsNotNone(task.set_status_message)
        tracking_url = task.set_tracking_url('http://test.luigi.com/')
        self.assertEqual(tracking_url, 'http://test.luigi.com/')
        message = task.set_status_message('message')
        self.assertEqual(message, 'message')

    def test_no_warn_if_param_types_ok(self):
        with warnings.catch_warnings(record=True) as w:
            DummyTask(**DUMMY_TASK_OK_PARAMS)
        self.assertEqual(len(w), 0, msg='No warning should be raised when correct parameter types are used')

    if six.PY3:  # assertWarnsRegex was introduced in Python 3.2
        def test_warn_on_non_str_param(self):
            params = dict(**DUMMY_TASK_OK_PARAMS)
            params['param'] = 42
            with self.assertWarnsRegex(UserWarning, 'Parameter "param" with value "42" is not of type string.'):
                DummyTask(**params)

        def test_warn_on_non_timedelta_param(self):
            params = dict(**DUMMY_TASK_OK_PARAMS)

            class MockTimedelta(object):
                days = 1
                seconds = 1

            params['timedelta_param'] = MockTimedelta()
            with self.assertWarnsRegex(UserWarning, 'Parameter "timedelta_param" with value ".*" is not of type timedelta.'):
                DummyTask(**params)


class ExternalizeTaskTest(LuigiTestCase):

    def test_externalize_taskclass(self):
        class MyTask(luigi.Task):
            def run(self):
                pass

        self.assertIsNotNone(MyTask.run)  # Assert what we believe
        task_object = luigi.task.externalize(MyTask)()