diff --git a/README.rst b/README.rst index 30cf856..cfc24b7 100644 --- a/README.rst +++ b/README.rst @@ -32,13 +32,18 @@ The ``FiscalYear`` class provides an object for storing information about the st .. code-block:: python - >>> from fiscalyear import * >>> a = FiscalYear(2017) >>> a.start FiscalDateTime(2016, 10, 1, 0, 0) >>> a.end FiscalDateTime(2017, 9, 30, 23, 59, 59) +You can also get the current ``FiscalYear`` with: + +.. code-block:: python + + >>> FiscalYear.current() + FiscalYear(2018) FiscalQuarter ------------- @@ -67,6 +72,12 @@ These objects represent the standalone ``FiscalQuarter`` class. >>> b in a True +You can also get the current ``FiscalQuarter`` with: + +.. code-block:: python + + >>> FiscalQuarter.current() + FiscalQuarter(2018, 2) FiscalDateTime -------------- diff --git a/docs/basic_usage.rst b/docs/basic_usage.rst index 93a741c..a8730d7 100644 --- a/docs/basic_usage.rst +++ b/docs/basic_usage.rst @@ -17,6 +17,14 @@ The ``FiscalYear`` class provides an object for storing information about the st >>> a.end FiscalDateTime(2017, 9, 30, 23, 59, 59) +You can also get the current ``FiscalYear`` with: + +.. code-block:: python + + >>> from fiscalyear import * + >>> FiscalYear.current() + FiscalYear(2018) + FiscalQuarter ------------- @@ -45,6 +53,14 @@ These objects represent the standalone ``FiscalQuarter`` class. >>> b in a True +You can also get the current ``FiscalQuarter`` with: + +.. code-block:: python + + >>> from fiscalyear import * + >>> FiscalQuarter.current() + FiscalQuarter(2018, 2) + FiscalDateTime -------------- diff --git a/docs/testing.rst b/docs/testing.rst index 7a0c8c6..b9a21b0 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -1,8 +1,13 @@ Testing ======= -``fiscalyear`` comes with a full test-suite called ``test_fiscalyear``. To run the test-suite, you will need to install the `pytest `_ package. +``fiscalyear`` comes with a full test-suite called ``test_fiscalyear``. To run the test-suite, you +will need to install the following packages: +* `pytest `_ +* `pytest-cov `_ +* `pytest-mock `_ +* `pytest-runner `_ Running tests ------------- diff --git a/fiscalyear.py b/fiscalyear.py index f3a7640..f1c3271 100644 --- a/fiscalyear.py +++ b/fiscalyear.py @@ -201,6 +201,16 @@ def __new__(cls, fiscal_year): self._fiscal_year = fiscal_year return self + @classmethod + def current(cls): + """Alternative constructor. Returns the current FiscalYear. + + :returns: A newly constructed FiscalYear object + :rtype: FiscalYear + """ + today = FiscalDate.today() + return cls(today.fiscal_year) + def __repr__(self): """Convert to formal string, for repr(). @@ -379,6 +389,16 @@ def __new__(cls, fiscal_year, quarter): self._quarter = quarter return self + @classmethod + def current(cls): + """Alternative constructor. Returns the current FiscalQuarter. + + :returns: A newly constructed FiscalQuarter object + :rtype: FiscalQuarter + """ + today = FiscalDate.today() + return cls(today.fiscal_year, today.quarter) + def __repr__(self): """Convert to formal string, for repr(). diff --git a/requirements.txt b/requirements.txt index fe65f2d..228f95a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ codecov pytest pytest-cov +pytest-mock pytest-runner sphinx sphinx_rtd_theme diff --git a/setup.py b/setup.py index 05153b5..466b39a 100755 --- a/setup.py +++ b/setup.py @@ -52,5 +52,5 @@ keywords=['fiscal year', 'fiscal quarter', 'calendar', 'datetime'], py_modules=[fiscalyear.__name__], setup_requires=['pytest-runner'], - tests_require=['pytest'], + tests_require=['pytest', 'pytest-mock'], ) diff --git a/test_fiscalyear.py b/test_fiscalyear.py index 5b2779f..04e2189 100644 --- a/test_fiscalyear.py +++ b/test_fiscalyear.py @@ -264,6 +264,12 @@ def d(self): def test_basic(self, a): assert a.fiscal_year == 2016 + def test_current(self, mocker): + mock_today = mocker.patch.object(fiscalyear.FiscalDate, 'today') + mock_today.return_value = fiscalyear.FiscalDate(2016, 10, 1) + current = fiscalyear.FiscalYear.current() + assert current == fiscalyear.FiscalYear(2017) + def test_repr(self, a): assert repr(a) == 'FiscalYear(2016)' @@ -407,6 +413,12 @@ def test_basic(self, a): assert a.fiscal_year == 2016 assert a.quarter == 4 + def test_current(self, mocker): + mock_today = mocker.patch.object(fiscalyear.FiscalDate, 'today') + mock_today.return_value = fiscalyear.FiscalDate(2016, 10, 1) + current = fiscalyear.FiscalQuarter.current() + assert current == fiscalyear.FiscalQuarter(2017, 1) + def test_repr(self, a): assert repr(a) == 'FiscalQuarter(2016, 4)'