From 5f39ae14a11a3b0215c65e534a27654c13ea6db7 Mon Sep 17 00:00:00 2001 From: maskarb Date: Wed, 30 Aug 2023 15:47:47 -0400 Subject: [PATCH] fix tests --- nise/__main__.py | 2 +- tests/test_main.py | 130 ++++++++++++++++++++++++++++++++------------- 2 files changed, 94 insertions(+), 38 deletions(-) diff --git a/nise/__main__.py b/nise/__main__.py index 4cc0d0ee..47289c78 100644 --- a/nise/__main__.py +++ b/nise/__main__.py @@ -89,7 +89,7 @@ def valid_currency(currency): def today(): """Create the date of today.""" - return datetime.datetime.now().replace(microsecond=0, second=0, minute=0) + return datetime.datetime.now(tz=timezone.utc).replace(microsecond=0, second=0, minute=0) def add_aws_parser_args(parser): diff --git a/tests/test_main.py b/tests/test_main.py index 2f234b05..abb86ad7 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -20,6 +20,7 @@ from datetime import date from datetime import datetime from datetime import timedelta +from datetime import timezone from unittest import TestCase from unittest.mock import patch @@ -367,11 +368,12 @@ def test_load_static_report_data_azure_dates(self): _load_static_report_data(options) gen_values = dict(*options.get("static_report_data").get("generators")[0].values()) self.assertEqual( - gen_values.get("start_date"), str(datetime.now().replace(microsecond=0, second=0, minute=0, hour=0)) + gen_values.get("start_date"), + str(datetime.now(tz=timezone.utc).replace(microsecond=0, second=0, minute=0, hour=0)), ) self.assertEqual( gen_values.get("end_date"), - str(datetime.now().replace(microsecond=0, second=0, minute=0) + timedelta(hours=24)), + str(datetime.now(tz=timezone.utc).replace(microsecond=0, second=0, minute=0) + timedelta(hours=24)), ) def test_invalid_gcp_inputs(self): @@ -557,14 +559,26 @@ def test_aws_dates(self, mock_load): ] static_report_data = {"generators": aws_gens} expected = { - "aws_gen_first": {"start_date": datetime(2020, 6, 1, 0, 0), "end_date": datetime(2020, 6, 1, 0, 0)}, - "aws_gen_first_second": {"start_date": datetime(2020, 6, 1, 0, 0), "end_date": datetime(2020, 6, 2, 0, 0)}, + "aws_gen_first": { + "start_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + }, + "aws_gen_first_second": { + "start_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 2, 0, 0, tzinfo=timezone.utc), + }, "aws_gen_first_start": { - "start_date": datetime(2020, 6, 1, 0, 0), - "end_date": datetime.now().replace(minute=0, second=0, microsecond=0), + "start_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + "end_date": datetime.now(tz=timezone.utc).replace(minute=0, second=0, microsecond=0), + }, + "aws_gen_last": { + "start_date": datetime(2020, 5, 31, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 5, 31, 0, 0, tzinfo=timezone.utc), + }, + "aws_gen_last_first": { + "start_date": datetime(2020, 5, 31, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), }, - "aws_gen_last": {"start_date": datetime(2020, 5, 31, 0, 0), "end_date": datetime(2020, 5, 31, 0, 0)}, - "aws_gen_last_first": {"start_date": datetime(2020, 5, 31, 0, 0), "end_date": datetime(2020, 6, 1, 0, 0)}, } options = {"provider": "aws", "static_report_file": "tests/aws_static_report.yml"} mock_load.return_value = static_report_data @@ -602,19 +616,25 @@ def test_aws_market_dates(self, mock_load): ] static_report_data = {"generators": aws_mp_gens} expected = { - "aws_mp_gen_first": {"start_date": datetime(2020, 6, 1, 0, 0), "end_date": datetime(2020, 6, 1, 0, 0)}, + "aws_mp_gen_first": { + "start_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + }, "aws_mp_gen_first_second": { - "start_date": datetime(2020, 6, 1, 0, 0), - "end_date": datetime(2020, 6, 2, 0, 0), + "start_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 2, 0, 0, tzinfo=timezone.utc), }, "aws_mp_gen_first_start": { - "start_date": datetime(2020, 6, 1, 0, 0), - "end_date": datetime.now().replace(minute=0, second=0, microsecond=0), + "start_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + "end_date": datetime.now(tz=timezone.utc).replace(minute=0, second=0, microsecond=0), + }, + "aws_mp_gen_last": { + "start_date": datetime(2020, 5, 31, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 5, 31, 0, 0, tzinfo=timezone.utc), }, - "aws_mp_gen_last": {"start_date": datetime(2020, 5, 31, 0, 0), "end_date": datetime(2020, 5, 31, 0, 0)}, "aws_mp_gen_last_first": { - "start_date": datetime(2020, 5, 31, 0, 0), - "end_date": datetime(2020, 6, 1, 0, 0), + "start_date": datetime(2020, 5, 31, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), }, } options = {"provider": "aws-marketplace", "static_report_file": "tests/aws_static_report.yml"} @@ -630,7 +650,7 @@ def test_aws_market_dates(self, mock_load): def test_ocp_dates(self, mock_load): """Test that select static-data-file dates return correct dates.""" ocp_gens = [ - {"ocp_gen_first": {"start_date": datetime(2020, 6, 1).date(), "end_date": datetime(2020, 6, 1).date()}}, + {"ocp_gen_first": {"start_date": "2020-06-01", "end_date": datetime(2020, 6, 1).date()}}, { "ocp_gen_first_second": { "start_date": datetime(2020, 6, 1).date(), @@ -645,17 +665,34 @@ def test_ocp_dates(self, mock_load): "end_date": datetime(2020, 6, 1).date(), } }, + {"ocp_gen_times": {"start_date": "2023-08-01T05", "end_date": "2023-08-02T23:45"}}, ] static_report_data = {"generators": ocp_gens} expected = { - "ocp_gen_first": {"start_date": datetime(2020, 6, 1, 0, 0), "end_date": datetime(2020, 6, 1, 0, 0)}, - "ocp_gen_first_second": {"start_date": datetime(2020, 6, 1, 0, 0), "end_date": datetime(2020, 6, 2, 0, 0)}, + "ocp_gen_first": { + "start_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + }, + "ocp_gen_first_second": { + "start_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 2, 0, 0, tzinfo=timezone.utc), + }, "ocp_gen_first_start": { - "start_date": datetime(2020, 6, 1, 0, 0), - "end_date": datetime.now().replace(minute=0, second=0, microsecond=0), + "start_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + "end_date": datetime.now(tz=timezone.utc).replace(minute=0, second=0, microsecond=0), + }, + "ocp_gen_last": { + "start_date": datetime(2020, 5, 31, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 5, 31, 0, 0, tzinfo=timezone.utc), + }, + "ocp_gen_last_first": { + "start_date": datetime(2020, 5, 31, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + }, + "ocp_gen_times": { + "start_date": datetime(2023, 8, 1, 5, 0, tzinfo=timezone.utc), + "end_date": datetime(2023, 8, 2, 23, 45, tzinfo=timezone.utc), }, - "ocp_gen_last": {"start_date": datetime(2020, 5, 31, 0, 0), "end_date": datetime(2020, 5, 31, 0, 0)}, - "ocp_gen_last_first": {"start_date": datetime(2020, 5, 31, 0, 0), "end_date": datetime(2020, 6, 1, 0, 0)}, } options = {"provider": "ocp", "static_report_file": "tests/ocp_static_report.yml"} mock_load.return_value = static_report_data @@ -688,19 +725,26 @@ def test_azure_dates(self, mock_load): ] static_report_data = {"generators": azure_gens} expected = { - "azure_gen_first": {"start_date": datetime(2020, 6, 1, 0, 0), "end_date": datetime(2020, 6, 2, 0, 0)}, + "azure_gen_first": { + "start_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 2, 0, 0, tzinfo=timezone.utc), + }, "azure_gen_first_second": { - "start_date": datetime(2020, 6, 1, 0, 0), - "end_date": datetime(2020, 6, 3, 0, 0), + "start_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 3, 0, 0, tzinfo=timezone.utc), }, "azure_gen_first_start": { - "start_date": datetime(2020, 6, 1, 0, 0), - "end_date": datetime.now().replace(microsecond=0, second=0, minute=0) + timedelta(hours=24), + "start_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + "end_date": datetime.now(tz=timezone.utc).replace(microsecond=0, second=0, minute=0) + + timedelta(hours=24), + }, + "azure_gen_last": { + "start_date": datetime(2020, 5, 31, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), }, - "azure_gen_last": {"start_date": datetime(2020, 5, 31, 0, 0), "end_date": datetime(2020, 6, 1, 0, 0)}, "azure_gen_last_first": { - "start_date": datetime(2020, 5, 31, 0, 0), - "end_date": datetime(2020, 6, 2, 0, 0), + "start_date": datetime(2020, 5, 31, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 2, 0, 0, tzinfo=timezone.utc), }, } options = {"provider": "azure", "static_report_file": "tests/azure_static_report.yml"} @@ -734,14 +778,26 @@ def test_oci_dates(self, mock_load): ] static_report_data = {"generators": oci_gens} expected = { - "oci_gen_first": {"start_date": datetime(2020, 6, 1, 0, 0), "end_date": datetime(2020, 6, 1, 0, 0)}, - "oci_gen_first_second": {"start_date": datetime(2020, 6, 1, 0, 0), "end_date": datetime(2020, 6, 2, 0, 0)}, + "oci_gen_first": { + "start_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + }, + "oci_gen_first_second": { + "start_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 2, 0, 0, tzinfo=timezone.utc), + }, "oci_gen_first_start": { - "start_date": datetime(2020, 6, 1, 0, 0), - "end_date": datetime.now().replace(minute=0, second=0, microsecond=0), + "start_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), + "end_date": datetime.now(tz=timezone.utc).replace(minute=0, second=0, microsecond=0), + }, + "oci_gen_last": { + "start_date": datetime(2020, 5, 31, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 5, 31, 0, 0, tzinfo=timezone.utc), + }, + "oci_gen_last_first": { + "start_date": datetime(2020, 5, 31, 0, 0, tzinfo=timezone.utc), + "end_date": datetime(2020, 6, 1, 0, 0, tzinfo=timezone.utc), }, - "oci_gen_last": {"start_date": datetime(2020, 5, 31, 0, 0), "end_date": datetime(2020, 5, 31, 0, 0)}, - "oci_gen_last_first": {"start_date": datetime(2020, 5, 31, 0, 0), "end_date": datetime(2020, 6, 1, 0, 0)}, } options = {"provider": "oci", "static_report_file": "tests/oci_static_report.yml"} mock_load.return_value = static_report_data