Skip to content

Commit

Permalink
Move tests for airflow.utils.dates out of tests/core/ (apache#13088)
Browse files Browse the repository at this point in the history
These three functions were in test_core, but the separate test_dates
file is better suited.

In addition I have removed the use of `assert_array_almost_equal` from
numpy as pytest provides it's own version
  • Loading branch information
ashb authored Dec 16, 2020
1 parent 99c2e03 commit 95d9088
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 50 deletions.
50 changes: 0 additions & 50 deletions tests/core/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
from datetime import timedelta
from time import sleep

from dateutil.relativedelta import relativedelta
from numpy.testing import assert_array_almost_equal

from airflow import settings
from airflow.exceptions import AirflowException, AirflowTaskTimeout
from airflow.hooks.base import BaseHook
Expand All @@ -38,7 +35,6 @@
from airflow.operators.dummy import DummyOperator
from airflow.operators.python import PythonOperator
from airflow.settings import Session
from airflow.utils.dates import infer_time_unit, round_time, scale_time_units
from airflow.utils.state import State
from airflow.utils.timezone import datetime
from airflow.utils.types import DagRunType
Expand Down Expand Up @@ -322,52 +318,6 @@ def test_raw_job(self):
)
ti.run(ignore_ti_state=True)

def test_round_time(self):

rt1 = round_time(datetime(2015, 1, 1, 6), timedelta(days=1))
self.assertEqual(datetime(2015, 1, 1, 0, 0), rt1)

rt2 = round_time(datetime(2015, 1, 2), relativedelta(months=1))
self.assertEqual(datetime(2015, 1, 1, 0, 0), rt2)

rt3 = round_time(datetime(2015, 9, 16, 0, 0), timedelta(1), datetime(2015, 9, 14, 0, 0))
self.assertEqual(datetime(2015, 9, 16, 0, 0), rt3)

rt4 = round_time(datetime(2015, 9, 15, 0, 0), timedelta(1), datetime(2015, 9, 14, 0, 0))
self.assertEqual(datetime(2015, 9, 15, 0, 0), rt4)

rt5 = round_time(datetime(2015, 9, 14, 0, 0), timedelta(1), datetime(2015, 9, 14, 0, 0))
self.assertEqual(datetime(2015, 9, 14, 0, 0), rt5)

rt6 = round_time(datetime(2015, 9, 13, 0, 0), timedelta(1), datetime(2015, 9, 14, 0, 0))
self.assertEqual(datetime(2015, 9, 14, 0, 0), rt6)

def test_infer_time_unit(self):

self.assertEqual('minutes', infer_time_unit([130, 5400, 10]))

self.assertEqual('seconds', infer_time_unit([110, 50, 10, 100]))

self.assertEqual('hours', infer_time_unit([100000, 50000, 10000, 20000]))

self.assertEqual('days', infer_time_unit([200000, 100000]))

def test_scale_time_units(self):

# use assert_almost_equal from numpy.testing since we are comparing
# floating point arrays
arr1 = scale_time_units([130, 5400, 10], 'minutes')
assert_array_almost_equal(arr1, [2.167, 90.0, 0.167], decimal=3)

arr2 = scale_time_units([110, 50, 10, 100], 'seconds')
assert_array_almost_equal(arr2, [110.0, 50.0, 10.0, 100.0], decimal=3)

arr3 = scale_time_units([100000, 50000, 10000, 20000], 'hours')
assert_array_almost_equal(arr3, [27.778, 13.889, 2.778, 5.556], decimal=3)

arr4 = scale_time_units([200000, 100000], 'days')
assert_array_almost_equal(arr4, [2.315, 1.157], decimal=3)

def test_bad_trigger_rule(self):
with self.assertRaises(AirflowException):
DummyOperator(task_id='test_bad_trigger', trigger_rule="non_existent", dag=self.dag)
Expand Down
53 changes: 53 additions & 0 deletions tests/utils/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from datetime import datetime, timedelta

import pendulum
from dateutil.relativedelta import relativedelta
from pytest import approx

from airflow.utils import dates, timezone

Expand Down Expand Up @@ -51,6 +53,57 @@ def test_parse_execution_date(self):
)
self.assertRaises(ValueError, dates.parse_execution_date, bad_execution_date_str)

def test_round_time(self):

rt1 = dates.round_time(timezone.datetime(2015, 1, 1, 6), timedelta(days=1))
assert timezone.datetime(2015, 1, 1, 0, 0) == rt1

rt2 = dates.round_time(timezone.datetime(2015, 1, 2), relativedelta(months=1))
assert timezone.datetime(2015, 1, 1, 0, 0) == rt2

rt3 = dates.round_time(
timezone.datetime(2015, 9, 16, 0, 0), timedelta(1), timezone.datetime(2015, 9, 14, 0, 0)
)
assert timezone.datetime(2015, 9, 16, 0, 0) == rt3

rt4 = dates.round_time(
timezone.datetime(2015, 9, 15, 0, 0), timedelta(1), timezone.datetime(2015, 9, 14, 0, 0)
)
assert timezone.datetime(2015, 9, 15, 0, 0) == rt4

rt5 = dates.round_time(
timezone.datetime(2015, 9, 14, 0, 0), timedelta(1), timezone.datetime(2015, 9, 14, 0, 0)
)
assert timezone.datetime(2015, 9, 14, 0, 0) == rt5

rt6 = dates.round_time(
timezone.datetime(2015, 9, 13, 0, 0), timedelta(1), timezone.datetime(2015, 9, 14, 0, 0)
)
assert timezone.datetime(2015, 9, 14, 0, 0) == rt6

def test_infer_time_unit(self):
assert dates.infer_time_unit([130, 5400, 10]) == 'minutes'

assert dates.infer_time_unit([110, 50, 10, 100]) == 'seconds'

assert dates.infer_time_unit([100000, 50000, 10000, 20000]) == 'hours'

assert dates.infer_time_unit([200000, 100000]) == 'days'

def test_scale_time_units(self):
# floating point arrays
arr1 = dates.scale_time_units([130, 5400, 10], 'minutes')
assert arr1 == approx([2.1667, 90.0, 0.1667], rel=1e-3)

arr2 = dates.scale_time_units([110, 50, 10, 100], 'seconds')
assert arr2 == approx([110.0, 50.0, 10.0, 100.0])

arr3 = dates.scale_time_units([100000, 50000, 10000, 20000], 'hours')
assert arr3 == approx([27.7778, 13.8889, 2.7778, 5.5556], rel=1e-3)

arr4 = dates.scale_time_units([200000, 100000], 'days')
assert arr4 == approx([2.3147, 1.1574], rel=1e-3)


class TestUtilsDatesDateRange(unittest.TestCase):
def test_no_delta(self):
Expand Down

0 comments on commit 95d9088

Please sign in to comment.