Skip to content

Instantly share code, notes, and snippets.

@samdoran
Last active December 22, 2021 23:59
Show Gist options
  • Save samdoran/66ef35bb95a30c1af2925618d0310468 to your computer and use it in GitHub Desktop.
Save samdoran/66ef35bb95a30c1af2925618d0310468 to your computer and use it in GitHub Desktop.
date_time unit tests
# -*- coding: utf-8 -*-
# Copyright (c) 2020 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import pytest
import datetime
from ansible.module_utils.facts.system.date_time import DateTimeFactCollector
UTC_TIMESTAMP = datetime.datetime(2012, 2, 14, 10, 30, 0, 972278)
@pytest.fixture
def fake_now(monkeypatch):
class FakeNow:
@classmethod
def utcnow(cls):
return UTC_TIMESTAMP
monkeypatch.setattr(datetime, 'datetime', FakeNow)
@pytest.fixture
def date_collector(monkeypatch, fake_now):
monkeypatch.setenv('TZ', 'Australia/Melbourne')
collector = DateTimeFactCollector()
data = collector.collect()
return data
def test_date_time_month(date_collector):
assert date_collector['date_time']['month'] == '02'
def test_date_time_tz(date_collector):
assert date_collector['date_time']['tz'] == 'AEST'
assert date_collector['date_time']['tz_dst'] == 'AEDT'
assert date_collector['date_time']['tz_offset'] == '+1000'
def test_date_time_day(date_collector):
assert date_collector['date_time']['day'] == '14'
assert date_collector['date_time']['weekday'] == 'Tuesday'
def test_date_time_iso(date_collector, mocker):
# Omit these tests for now since there is a bug in date_time.py
assert date_collector['date_time']['iso8601_micro'] == '2012-02-14T10:30:00.972278Z'
assert date_collector['date_time']['iso8601'] == '2012-02-14T10:30:00Z'
assert date_collector['date_time']['iso8601_basic'] == '20120214T203000972278'
assert date_collector['date_time']['iso8601_basic_short'] == '20120214T203000'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment