-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add proper tests to the build pipeline (#11)
* 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
Showing
6 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |