Skip to content

Commit

Permalink
Add proper tests to the build pipeline (#11)
Browse files Browse the repository at this point in the history
* Add tests and a bit of in-code docs for the Python interface

* Add tests to the pipeline

* Point the test command to the tests directory.
  • Loading branch information
gukoff authored Feb 13, 2021
1 parent 61e200e commit 3a17fb7
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ jobs:
CIBW_BEFORE_BUILD_LINUX: >
curl https://sh.rustup.rs -sSf | sh -s -- --profile=minimal -y &&
rustup show
CIBW_TEST_COMMAND: "pytest {project}/tests"
CIBW_TEST_EXTRAS: test

- name: build windows 32bit binaries
if: matrix.os == 'windows'
Expand All @@ -92,6 +94,8 @@ jobs:
CIBW_BEFORE_BUILD: >
rustup default stable-i686-pc-windows-msvc &&
rustup show
CIBW_TEST_COMMAND: "pytest {project}/tests"
CIBW_TEST_EXTRAS: test

- name: list wheels
run: ${{ matrix.ls || 'ls -lh' }} ${{ matrix.wheels-dir }}
Expand Down
19 changes: 18 additions & 1 deletion dtparse/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
"""Fast datetime parser for Python written in Rust.
Signature:
def parse(str_datetime: str, fmt: str) -> datetime:
pass
For example:
parse('2004-12-01 13:02:47', '%Y-%m-%d %H:%M:%S')
or
parse('2004-12-01 13:02:47.123456', '%Y-%m-%d %H:%M:%S.%f')
The format string is built a bit differently than for datetime.strptime.
Please, see the list of allowed format specifiers (%Y, %m, %d, ...) here:
https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html
"""
from __future__ import absolute_import

from ._dtparse import parse # Import Parser from the rust binary
from ._dtparse import parse # import from the compiled library

__all__ = ['parse']
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def run(self):
rust_extensions=[RustExtension('dtparse._dtparse', 'Cargo.toml')],
install_requires=install_requires,
tests_require=tests_require,
extras_require={'test': tests_require},
setup_requires=setup_requires,
include_package_data=True,
zip_safe=False,
Expand Down
19 changes: 19 additions & 0 deletions tests/test_call_signature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

from datetime import datetime

from dtparse import parse


def test_call_signature():
string = '2004-12-01 13:02:47.123456'
dt_format = '%Y-%m-%d %H:%M:%S%.f'
expected = datetime(2004, 12, 1, 13, 2, 47, 123456)

assert parse(string, dt_format) == expected
assert parse(string, fmt=dt_format) == expected
assert parse(str_datetime=string, fmt=dt_format) == expected

assert parse(*[string, dt_format]) == expected
assert parse(**dict(str_datetime=string, fmt=dt_format)) == expected
50 changes: 50 additions & 0 deletions tests/test_invalid_inputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

import pytest

from dtparse import parse


@pytest.mark.parametrize(
'str_datetime,fmt', [
["", ""],
["abc", ""],
["", "abc"],
["abc", "abc"],
['2018/01/02', '%Y/%m/%d'], # only date
['2018/01/02 12', '%Y/%m/%d %H'], # no minutes in the template
['2018/01/02 12', '%Y/%m/%d %M'], # no hours in the template
['2004-12-01 13:02:47 ', '%Y-%m-%d %H:%M:%S'], # extra whitespace in the string
['Fri Nov 28 12:00:09', '%a %b %e %T %Y'], # the year is missing in the string
['Fri Nov 28 12:00:09', '%a %b %e %T'], # the year is missing in the template
['Sat Nov 28 12:00:09 2014', '%a %b %e %T %Y'], # the weekday is incorrect
]
)
def test_throws_value_error(str_datetime, fmt):
with pytest.raises(ValueError):
parse(str_datetime=str_datetime, fmt=fmt)


@pytest.mark.parametrize(
'args', [
[],
[''],
[1],
[None],
[None, None],
[None, None, None],
[1, '%Y-%m-%d %H:%M:%S'],
[None, '%Y-%m-%d %H:%M:%S'],
['2004-12-01 13:02:47', 1],
['2004-12-01 13:02:47', None],
['2004-12-01 13:02:47', b'%Y-%m-%d %H:%M:%S'],
[b'2004-12-01 13:02:47', '%Y-%m-%d %H:%M:%S'],
[b'2004-12-01 13:02:47', b'%Y-%m-%d %H:%M:%S'],
['2004-12-01 13:02:47', '%Y-%m-%d %H:%M:%S', ""],
['2004-12-01 13:02:47', '%Y-%m-%d %H:%M:%S', None],
]
)
def test_throws_type_error(args):
with pytest.raises(TypeError):
parse(*args)
24 changes: 24 additions & 0 deletions tests/test_valid_inputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

from datetime import datetime

import pytest

from dtparse import parse


@pytest.mark.parametrize(
'str_datetime,fmt,expected', [
['2004-12-01 13:02:47', '%Y-%m-%d %H:%M:%S', datetime(2004, 12, 1, 13, 2, 47)],
['2004-12-01 13:02:47', '%Y-%m-%d %H:%M:%S ', datetime(2004, 12, 1, 13, 2, 47)], # extra whitespace in the template
['13:02:47XXX2004-12-01', '%H:%M:%SXXX%Y-%m-%d', datetime(2004, 12, 1, 13, 2, 47)],
['2004-12-01 13:02:47.123456', '%Y-%m-%d %H:%M:%S%.f', datetime(2004, 12, 1, 13, 2, 47, 123456)],
['2004-12-01 13:02:47.123456789', '%Y-%m-%d %H:%M:%S%.f', datetime(2004, 12, 1, 13, 2, 47, 123456)],
['Fri, 28 Nov 2014 21:00:09', '%a, %d %b %Y %H:%M:%S', datetime(2014, 11, 28, 21, 0, 9)],
['Fri, 28 Nov 2014 21:00:09', '%a, %d %b %Y %T', datetime(2014, 11, 28, 21, 0, 9)],
['Fri Nov 28 21:00:09 2014', '%a %b %e %T %Y', datetime(2014, 11, 28, 21, 0, 9)],
]
)
def test_parses_correctly(str_datetime, fmt, expected):
assert parse(str_datetime=str_datetime, fmt=fmt) == expected

0 comments on commit 3a17fb7

Please sign in to comment.