This repository has been archived by the owner on Aug 23, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
134 additions
and
28 deletions.
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
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,17 @@ | ||
import pathlib | ||
import pytest | ||
import sqlite_utils | ||
|
||
|
||
@pytest.fixture | ||
def test_db(tmpdir): | ||
db_path = str(pathlib.Path(tmpdir) / "data.db") | ||
db = sqlite_utils.Database(db_path) | ||
db["example"].insert_all( | ||
[ | ||
{"id": 1, "dt": "5th October 2019 12:04"}, | ||
{"id": 2, "dt": "6th October 2019 00:05:06"}, | ||
], | ||
pk="id", | ||
) | ||
return db_path |
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,62 @@ | ||
from click.testing import CliRunner | ||
from sqlite_transform import cli | ||
import sqlite_utils | ||
|
||
|
||
def test_lambda_single_line(test_db): | ||
result = CliRunner().invoke( | ||
cli.cli, | ||
[ | ||
"lambda", | ||
test_db, | ||
"example", | ||
"dt", | ||
"--code", | ||
"return value.replace('October', 'Spooktober')", | ||
], | ||
) | ||
assert 0 == result.exit_code, result.output | ||
assert [ | ||
{"id": 1, "dt": "5th Spooktober 2019 12:04"}, | ||
{"id": 2, "dt": "6th Spooktober 2019 00:05:06"}, | ||
] == list(sqlite_utils.Database(test_db)["example"].rows) | ||
|
||
|
||
def test_lambda_multiple_lines(test_db): | ||
result = CliRunner().invoke( | ||
cli.cli, | ||
[ | ||
"lambda", | ||
test_db, | ||
"example", | ||
"dt", | ||
"--code", | ||
"v = value.replace('October', 'Spooktober')\nreturn v.upper()", | ||
], | ||
) | ||
assert 0 == result.exit_code, result.output | ||
assert [ | ||
{"id": 1, "dt": "5TH SPOOKTOBER 2019 12:04"}, | ||
{"id": 2, "dt": "6TH SPOOKTOBER 2019 00:05:06"}, | ||
] == list(sqlite_utils.Database(test_db)["example"].rows) | ||
|
||
|
||
def test_lambda_import(test_db): | ||
result = CliRunner().invoke( | ||
cli.cli, | ||
[ | ||
"lambda", | ||
test_db, | ||
"example", | ||
"dt", | ||
"--code", | ||
"return re.sub('O..', 'OXX', value)", | ||
"--import", | ||
"re", | ||
], | ||
) | ||
assert 0 == result.exit_code, result.output | ||
assert [ | ||
{"id": 1, "dt": "5th OXXober 2019 12:04"}, | ||
{"id": 2, "dt": "6th OXXober 2019 00:05:06"}, | ||
] == list(sqlite_utils.Database(test_db)["example"].rows) |
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,38 +1,20 @@ | ||
from click.testing import CliRunner | ||
from sqlite_transform import cli | ||
import pathlib | ||
import pytest | ||
import sqlite_utils | ||
|
||
|
||
@pytest.fixture | ||
def db_with_dates(tmpdir): | ||
db_path = str(pathlib.Path(tmpdir) / "data.db") | ||
db = sqlite_utils.Database(db_path) | ||
db["example"].insert_all( | ||
[ | ||
{"id": 1, "dt": "5th October 2019 12:04"}, | ||
{"id": 2, "dt": "6th October 2019 00:05:06"}, | ||
], | ||
pk="id", | ||
) | ||
return db_path | ||
|
||
|
||
def test_parsedate(db_with_dates): | ||
result = CliRunner().invoke(cli.cli, ["parsedate", db_with_dates, "example", "dt"]) | ||
def test_parsedate(test_db): | ||
result = CliRunner().invoke(cli.cli, ["parsedate", test_db, "example", "dt"]) | ||
assert 0 == result.exit_code, result.output | ||
assert [{"id": 1, "dt": "2019-10-05"}, {"id": 2, "dt": "2019-10-06"}] == list( | ||
sqlite_utils.Database(db_with_dates)["example"].rows | ||
sqlite_utils.Database(test_db)["example"].rows | ||
) | ||
|
||
|
||
def test_parsedatetime(db_with_dates): | ||
result = CliRunner().invoke( | ||
cli.cli, ["parsedatetime", db_with_dates, "example", "dt"] | ||
) | ||
def test_parsedatetime(test_db): | ||
result = CliRunner().invoke(cli.cli, ["parsedatetime", test_db, "example", "dt"]) | ||
assert 0 == result.exit_code, result.output | ||
assert [ | ||
{"id": 1, "dt": "2019-10-05T12:04:00"}, | ||
{"id": 2, "dt": "2019-10-06T00:05:06"}, | ||
] == list(sqlite_utils.Database(db_with_dates)["example"].rows) | ||
] == list(sqlite_utils.Database(test_db)["example"].rows) |