1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
|
"""test_helpers_date_ops"""
from datetime import datetime
from unittest import TestCase
from unittest.mock import Mock
import pytest
from elasticsearch9 import NotFoundError
from elastic_transport import ApiResponseMeta
from curator.exceptions import ConfigurationError
from curator.helpers.date_ops import (
absolute_date_range,
date_range,
datetime_to_epoch,
fix_epoch,
get_date_regex,
get_datemath,
get_point_of_reference,
isdatemath,
)
class TestGetDateRegex(TestCase):
"""TestGetDateRegex
Test helpers.date_ops.get_date_regex functionality.
"""
def test_non_escaped(self):
"""test_non_escaped
Should return a proper regex from a non-escaped Python date string
"""
assert '\\d{4}\\-\\d{2}\\-\\d{2}t\\d{2}' == get_date_regex('%Y-%m-%dt%H')
class TestFixEpoch(TestCase):
"""TestFixEpoch
Test helpers.date_ops.fix_epoch functionality.
"""
def test_fix_epoch(self):
"""test_fix_epoch
Should return straight epoch time in seconds, removing milliseconds or more decimals
"""
for long_epoch, epoch in [
(1459287636, 1459287636),
(14592876369, 14592876),
(145928763699, 145928763),
(1459287636999, 1459287636),
(1459287636000000, 1459287636),
(145928763600000000, 1459287636),
(145928763600000001, 1459287636),
(1459287636123456789, 1459287636),
]:
assert epoch == fix_epoch(long_epoch)
def test_fix_epoch_raise(self):
"""test_fix_epoch_raise
Should raise a ``ValueError`` exception when an improper value is passed
"""
with pytest.raises(ValueError):
fix_epoch(None)
class TestGetPointOfReference(TestCase):
"""TestGetPointOfReference
Test helpers.date_ops.get_point_of_reference functionality.
"""
def test_get_point_of_reference(self):
"""test_get_point_of_reference
Should return a reference point n units * seconds prior to the present epoch time
"""
epoch = 1459288037
for unit, result in [
('seconds', epoch - 1),
('minutes', epoch - 60),
('hours', epoch - 3600),
('days', epoch - 86400),
('weeks', epoch - (86400 * 7)),
('months', epoch - (86400 * 30)),
('years', epoch - (86400 * 365)),
]:
# self.assertEqual(result, get_point_of_reference(unit, 1, epoch))
assert result == get_point_of_reference(unit, 1, epoch)
def test_get_por_raise(self):
"""test_get_por_raise
Should raise a ``ValueError`` exception when an improper value is passed
"""
self.assertRaises(ValueError, get_point_of_reference, 'invalid', 1)
with pytest.raises(ValueError):
get_point_of_reference('invalid', 1)
class TestDateRange(TestCase):
"""TestDateRange
Test helpers.date_ops.date_range functionality.
"""
EPOCH = datetime_to_epoch(datetime(2017, 4, 3, 22, 50, 17))
def test_bad_unit(self):
"""test_bad_unit
Should raise a ``ConfigurationError`` exception when an improper unit value is passed
"""
with pytest.raises(ConfigurationError, match=r'"unit" must be one of'):
date_range('invalid', 1, 1)
def test_bad_range(self):
"""test_bad_range
Should raise a ``ConfigurationError`` exception when an improper range value is passed
"""
with pytest.raises(
ConfigurationError, match=r'must be greater than or equal to'
):
date_range('hours', 1, -1)
def test_hours_single(self):
"""test_hours_single
Should match hard-coded values when range_from = -1 and range_to = -1 and unit is hours
"""
unit = 'hours'
range_from = -1
range_to = -1
start = datetime_to_epoch(datetime(2017, 4, 3, 21, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 3, 21, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_hours_past_range(self):
"""test_hours_past_range
Should match hard-coded values when range_from = -3 and range_to = -1 and unit is hours
"""
unit = 'hours'
range_from = -3
range_to = -1
start = datetime_to_epoch(datetime(2017, 4, 3, 19, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 3, 21, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_hours_future_range(self):
"""test_hours_future_range
Should match hard-coded values when range_from = 0 and range_to = 2 and unit is hours
"""
unit = 'hours'
range_from = 0
range_to = 2
start = datetime_to_epoch(datetime(2017, 4, 3, 22, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 4, 00, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_hours_span_range(self):
"""test_hours_span_range
Should match hard-coded values when range_from = -1 and range_to = 2 and unit is hours
"""
unit = 'hours'
range_from = -1
range_to = 2
start = datetime_to_epoch(datetime(2017, 4, 3, 21, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 4, 00, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_days_single(self):
"""test_days_single
Should match hard-coded values when range_from = -1 and range_to = -1 and unit is days
"""
unit = 'days'
range_from = -1
range_to = -1
start = datetime_to_epoch(datetime(2017, 4, 2, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 2, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_days_past_range(self):
"""test_days_range
Should match hard-coded values when range_from = -3 and range_to = -1 and unit is days
"""
unit = 'days'
range_from = -3
range_to = -1
start = datetime_to_epoch(datetime(2017, 3, 31, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 2, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_days_future_range(self):
"""test_days_future_range
Should match hard-coded values when range_from = 0 and range_to = 2 and unit is days
"""
unit = 'days'
range_from = 0
range_to = 2
start = datetime_to_epoch(datetime(2017, 4, 3, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 5, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_days_span_range(self):
"""test_days_span_range
Should match hard-coded values when range_from = -1 and range_to = 2 and unit is days
"""
unit = 'days'
range_from = -1
range_to = 2
start = datetime_to_epoch(datetime(2017, 4, 2, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 5, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_weeks_single(self):
"""test_weeks_single
Should match hard-coded values when range_from = -1 and range_to = -1 and unit is weeks
"""
unit = 'weeks'
range_from = -1
range_to = -1
start = datetime_to_epoch(datetime(2017, 3, 26, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 1, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_weeks_past_range(self):
"""test_weeks_past_range
Should match hard-coded values when range_from = -3 and range_to = -1 and unit is weeks
"""
unit = 'weeks'
range_from = -3
range_to = -1
start = datetime_to_epoch(datetime(2017, 3, 12, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 1, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_weeks_future_range(self):
"""test_weeks_future_range
Should match hard-coded values when range_from = 0 and range_to = 2 and unit is weeks
"""
unit = 'weeks'
range_from = 0
range_to = 2
start = datetime_to_epoch(datetime(2017, 4, 2, 00, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 22, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_weeks_span_range(self):
"""test_weeks_span_range
Should match hard-coded values when range_from = -1 and range_to = 2 and unit is weeks
"""
unit = 'weeks'
range_from = -1
range_to = 2
start = datetime_to_epoch(datetime(2017, 3, 26, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 22, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_weeks_single_iso(self):
"""test_weeks_single_iso
Should match hard-coded values when range_from = -1 and range_to = -1, unit is weeks,
and ``week_starts_on`` = ``monday``
"""
unit = 'weeks'
range_from = -1
range_to = -1
start = datetime_to_epoch(datetime(2017, 3, 27, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 2, 23, 59, 59))
# pylint: disable=line-too-long
assert (start, end) == date_range(
unit, range_from, range_to, epoch=self.EPOCH, week_starts_on='monday'
)
def test_weeks_past_range_iso(self):
"""test_weeks_past_range_iso
Should match hard-coded values when range_from = -3 and range_to = -1, unit is weeks,
and ``week_starts_on`` = ``monday``
"""
unit = 'weeks'
range_from = -3
range_to = -1
start = datetime_to_epoch(datetime(2017, 3, 13, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 2, 23, 59, 59))
# pylint: disable=line-too-long
assert (start, end) == date_range(
unit, range_from, range_to, epoch=self.EPOCH, week_starts_on='monday'
)
def test_weeks_future_range_iso(self):
"""test_weeks_future_range_iso
Should match hard-coded values when range_from = 0 and range_to = 2, unit is weeks,
and ``week_starts_on`` = ``monday``
"""
unit = 'weeks'
range_from = 0
range_to = 2
start = datetime_to_epoch(datetime(2017, 4, 3, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 23, 23, 59, 59))
# pylint: disable=line-too-long
assert (start, end) == date_range(
unit, range_from, range_to, epoch=self.EPOCH, week_starts_on='monday'
)
def test_weeks_span_range_iso(self):
"""test_weeks_span_range_iso
Should match hard-coded values when range_from = -1 and range_to = 2, unit is weeks,
and ``week_starts_on`` = ``monday``
"""
unit = 'weeks'
range_from = -1
range_to = 2
start = datetime_to_epoch(datetime(2017, 3, 27, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 4, 23, 23, 59, 59))
# pylint: disable=line-too-long
assert (start, end) == date_range(
unit, range_from, range_to, epoch=self.EPOCH, week_starts_on='monday'
)
def test_months_single(self):
"""test_months_single
Should match hard-coded values when range_from = -1 and range_to = -1 and unit is months
"""
unit = 'months'
range_from = -1
range_to = -1
start = datetime_to_epoch(datetime(2017, 3, 1, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 3, 31, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_months_past_range(self):
"""test_months_past_range
Should match hard-coded values when range_from = -4 and range_to = -1 and unit is months
"""
unit = 'months'
range_from = -4
range_to = -1
start = datetime_to_epoch(datetime(2016, 12, 1, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 3, 31, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_months_future_range(self):
"""test_months_future_range
Should match hard-coded values when range_from = 7 and range_to = 10 and unit is months
"""
unit = 'months'
range_from = 7
range_to = 10
start = datetime_to_epoch(datetime(2017, 11, 1, 0, 0, 0))
end = datetime_to_epoch(datetime(2018, 2, 28, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_months_super_future_range(self):
"""test_months_super_future_range
Should match hard-coded values when range_from = 9 and range_to = 10 and unit is months
"""
unit = 'months'
range_from = 9
range_to = 10
start = datetime_to_epoch(datetime(2018, 1, 1, 0, 0, 0))
end = datetime_to_epoch(datetime(2018, 2, 28, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_months_span_range(self):
"""test_months_span_range
Should match hard-coded values when range_from = -1 and range_to = 2 and unit is months
"""
unit = 'months'
range_from = -1
range_to = 2
start = datetime_to_epoch(datetime(2017, 3, 1, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 6, 30, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_years_single(self):
"""test_years_single
Should match hard-coded values when range_from = -1 and range_to = -1 and unit is years
"""
unit = 'years'
range_from = -1
range_to = -1
start = datetime_to_epoch(datetime(2016, 1, 1, 0, 0, 0))
end = datetime_to_epoch(datetime(2016, 12, 31, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_years_past_range(self):
"""test_years_past_range
Should match hard-coded values when range_from = -3 and range_to = -1 and unit is years
"""
unit = 'years'
range_from = -3
range_to = -1
start = datetime_to_epoch(datetime(2014, 1, 1, 0, 0, 0))
end = datetime_to_epoch(datetime(2016, 12, 31, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_years_future_range(self):
"""test_years_future_range
Should match hard-coded values when range_from = 0 and range_to = 2 and unit is years
"""
unit = 'years'
range_from = 0
range_to = 2
start = datetime_to_epoch(datetime(2017, 1, 1, 0, 0, 0))
end = datetime_to_epoch(datetime(2019, 12, 31, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
def test_years_span_range(self):
"""test_years_span_range
Should match hard-coded values when range_from = -1 and range_to = 2 and unit is years
"""
unit = 'years'
range_from = -1
range_to = 2
start = datetime_to_epoch(datetime(2016, 1, 1, 0, 0, 0))
end = datetime_to_epoch(datetime(2019, 12, 31, 23, 59, 59))
assert (start, end) == date_range(unit, range_from, range_to, epoch=self.EPOCH)
class TestAbsoluteDateRange(TestCase):
"""TestAbsoluteDateRange
Test helpers.date_ops.absolute_date_range functionality.
"""
def test_bad_unit(self):
"""test_bad_unit
Should raise a ``ConfigurationError`` exception when an invalid value for unit is passed
"""
unit = 'invalid'
date_from = '2017.01'
date_from_format = '%Y.%m'
date_to = '2017.01'
date_to_format = '%Y.%m'
with pytest.raises(ConfigurationError, match=r'"unit" must be one of'):
absolute_date_range(
unit, date_from, date_to, date_from_format, date_to_format
)
def test_bad_formats(self):
"""test_bad_formats
Should raise a ``ConfigurationError`` exception when no value for ``date_from_format`` or
``date_to_format`` is passed.
"""
unit = 'days'
with pytest.raises(
ConfigurationError,
match=r'Must provide "date_from_format" and "date_to_format"',
):
absolute_date_range(unit, 'meh', 'meh', None, 'meh')
with pytest.raises(
ConfigurationError,
match=r'Must provide "date_from_format" and "date_to_format"',
):
absolute_date_range(unit, 'meh', 'meh', 'meh', None)
def test_bad_dates(self):
"""test_bad_dates
Should raise a ``ConfigurationError`` exception when date formats cannot be parsed for
``date_from_format`` and ``date_to_format``
"""
unit = 'weeks'
date_from_format = '%Y.%m'
date_to_format = '%Y.%m'
with pytest.raises(ConfigurationError, match=r'Unable to parse "date_from"'):
absolute_date_range(
unit, 'meh', '2017.01', date_from_format, date_to_format
)
with pytest.raises(ConfigurationError, match=r'Unable to parse "date_to"'):
absolute_date_range(
unit, '2017.01', 'meh', date_from_format, date_to_format
)
def test_single_month(self):
"""test_single_month
Output should match hard-coded values
"""
unit = 'months'
date_from = '2017.01'
date_from_format = '%Y.%m'
date_to = '2017.01'
date_to_format = '%Y.%m'
start = datetime_to_epoch(datetime(2017, 1, 1, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 1, 31, 23, 59, 59))
result = absolute_date_range(
unit, date_from, date_to, date_from_format, date_to_format
)
assert (start, end) == result
def test_multiple_month(self):
"""test_multiple_month
Output should match hard-coded values
"""
unit = 'months'
date_from = '2016.11'
date_from_format = '%Y.%m'
date_to = '2016.12'
date_to_format = '%Y.%m'
start = datetime_to_epoch(datetime(2016, 11, 1, 0, 0, 0))
end = datetime_to_epoch(datetime(2016, 12, 31, 23, 59, 59))
result = absolute_date_range(
unit, date_from, date_to, date_from_format, date_to_format
)
assert (start, end) == result
def test_single_year(self):
"""test_single_year
Output should match hard-coded values
"""
unit = 'years'
date_from = '2017'
date_from_format = '%Y'
date_to = '2017'
date_to_format = '%Y'
start = datetime_to_epoch(datetime(2017, 1, 1, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 12, 31, 23, 59, 59))
result = absolute_date_range(
unit, date_from, date_to, date_from_format, date_to_format
)
assert (start, end) == result
def test_multiple_year(self):
"""test_multiple_year
Output should match hard-coded values
"""
unit = 'years'
date_from = '2016'
date_from_format = '%Y'
date_to = '2017'
date_to_format = '%Y'
start = datetime_to_epoch(datetime(2016, 1, 1, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 12, 31, 23, 59, 59))
result = absolute_date_range(
unit, date_from, date_to, date_from_format, date_to_format
)
assert (start, end) == result
def test_single_week_uw(self):
"""test_single_week_UW
Output should match hard-coded values
"""
unit = 'weeks'
date_from = '2017-01'
date_from_format = '%Y-%U'
date_to = '2017-01'
date_to_format = '%Y-%U'
start = datetime_to_epoch(datetime(2017, 1, 2, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 1, 8, 23, 59, 59))
result = absolute_date_range(
unit, date_from, date_to, date_from_format, date_to_format
)
assert (start, end) == result
def test_multiple_weeks_uw(self):
"""test_multiple_weeks_UW
Output should match hard-coded values
"""
unit = 'weeks'
date_from = '2017-01'
date_from_format = '%Y-%U'
date_to = '2017-04'
date_to_format = '%Y-%U'
start = datetime_to_epoch(datetime(2017, 1, 2, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 1, 29, 23, 59, 59))
result = absolute_date_range(
unit, date_from, date_to, date_from_format, date_to_format
)
assert (start, end) == result
def test_single_week_iso(self):
"""test_single_week_ISO
Output should match hard-coded values
"""
unit = 'weeks'
date_from = '2014-01'
date_from_format = '%G-%V'
date_to = '2014-01'
date_to_format = '%G-%V'
start = datetime_to_epoch(datetime(2013, 12, 30, 0, 0, 0))
end = datetime_to_epoch(datetime(2014, 1, 5, 23, 59, 59))
result = absolute_date_range(
unit, date_from, date_to, date_from_format, date_to_format
)
assert (start, end) == result
def test_multiple_weeks_iso(self):
"""test_multiple_weeks_ISO
Output should match hard-coded values
"""
unit = 'weeks'
date_from = '2014-01'
date_from_format = '%G-%V'
date_to = '2014-04'
date_to_format = '%G-%V'
start = datetime_to_epoch(datetime(2013, 12, 30, 0, 0, 0))
end = datetime_to_epoch(datetime(2014, 1, 26, 23, 59, 59))
result = absolute_date_range(
unit, date_from, date_to, date_from_format, date_to_format
)
assert (start, end) == result
def test_single_day(self):
"""test_single_day
Output should match hard-coded values
"""
unit = 'days'
date_from = '2017.01.01'
date_from_format = '%Y.%m.%d'
date_to = '2017.01.01'
date_to_format = '%Y.%m.%d'
start = datetime_to_epoch(datetime(2017, 1, 1, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 1, 1, 23, 59, 59))
result = absolute_date_range(
unit, date_from, date_to, date_from_format, date_to_format
)
assert (start, end) == result
def test_multiple_days(self):
"""test_multiple_days
Output should match hard-coded values
"""
unit = 'days'
date_from = '2016.12.31'
date_from_format = '%Y.%m.%d'
date_to = '2017.01.01'
date_to_format = '%Y.%m.%d'
start = datetime_to_epoch(datetime(2016, 12, 31, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 1, 1, 23, 59, 59))
result = absolute_date_range(
unit, date_from, date_to, date_from_format, date_to_format
)
assert (start, end) == result
def test_iso8601(self):
"""test_ISO8601
Output should match hard-coded values
"""
unit = 'seconds'
date_from = '2017-01-01T00:00:00'
date_from_format = '%Y-%m-%dT%H:%M:%S'
date_to = '2017-01-01T12:34:56'
date_to_format = '%Y-%m-%dT%H:%M:%S'
start = datetime_to_epoch(datetime(2017, 1, 1, 0, 0, 0))
end = datetime_to_epoch(datetime(2017, 1, 1, 12, 34, 56))
result = absolute_date_range(
unit, date_from, date_to, date_from_format, date_to_format
)
assert (start, end) == result
class TestIsDateMath(TestCase):
"""TestIsDateMath
Test helpers.date_ops.isdatemath functionality.
"""
def test_positive(self):
"""test_positive
Result should match hard-coded sample
"""
data = '<encapsulated>'
assert isdatemath(data)
def test_negative(self):
"""test_negative
Result should not match hard-coded sample
"""
data = 'not_encapsulated'
assert not isdatemath(data)
def test_raises(self):
"""test_raises
Should raise ConfigurationError exception when malformed data is passed
"""
data = '<badly_encapsulated'
with pytest.raises(
ConfigurationError, match=r'Incomplete datemath encapsulation'
):
isdatemath(data)
class TestGetDateMath(TestCase):
"""TestGetDateMath
Test helpers.date_ops.get_datemath functionality.
"""
def test_success(self):
"""test_success
Result should match hard-coded sample
"""
client = Mock()
datemath = '{hasthemath}'
psuedo_random = 'not_random_at_all'
expected = 'curator_get_datemath_function_' + psuedo_random + '-hasthemath'
# 5 positional args for meta: status, http_version, headers, duration, node
meta = ApiResponseMeta(404, '1.1', {}, 0.01, None)
body = {'error': {'index': expected}}
msg = 'index_not_found_exception'
# 3 positional args for NotFoundError: message, meta, body
effect = NotFoundError(msg, meta, body)
client.indices.get.side_effect = effect
self.assertEqual('hasthemath', get_datemath(client, datemath, psuedo_random))
def test_failure(self):
"""test_failure
Should raise ConfigurationError exception when index is not found
"""
client = Mock()
datemath = '{hasthemath}'
# 5 positional args for meta: status, http_version, headers, duration, node
meta = ApiResponseMeta(404, '1.1', {}, 0.01, None)
body = {'error': {'index': 'this_will_not_be_found'}}
msg = 'index_not_found_exception'
# 3 positional args for NotFoundError: message, meta, body
effect = NotFoundError(msg, meta, body)
client.indices.get.side_effect = effect
self.assertRaises(ConfigurationError, get_datemath, client, datemath)
|