Skip to content

Commit

Permalink
Merge pull request #4 from pmav99/current_constructor
Browse files Browse the repository at this point in the history
Add syntactic sugar for getting the current Fiscal{Year,Quarter}.
  • Loading branch information
adamjstewart authored Oct 22, 2019
2 parents 92817b0 + 7655cae commit 58190b6
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 3 deletions.
13 changes: 12 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------
Expand Down Expand Up @@ -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
--------------
Expand Down
16 changes: 16 additions & 0 deletions docs/basic_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------
Expand Down Expand Up @@ -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
--------------
Expand Down
7 changes: 6 additions & 1 deletion docs/testing.rst
Original file line number Diff line number Diff line change
@@ -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 <https://docs.pytest.org/en/latest/>`_ 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 <https://docs.pytest.org/en/latest/>`_
* `pytest-cov <https://docs.pytest.org/en/latest/>`_
* `pytest-mock <https://docs.pytest.org/en/latest/>`_
* `pytest-runner <https://docs.pytest.org/en/latest/>`_

Running tests
-------------
Expand Down
20 changes: 20 additions & 0 deletions fiscalyear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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().
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
codecov
pytest
pytest-cov
pytest-mock
pytest-runner
sphinx
sphinx_rtd_theme
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
)
12 changes: 12 additions & 0 deletions test_fiscalyear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)'

Expand Down Expand Up @@ -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)'

Expand Down

0 comments on commit 58190b6

Please sign in to comment.