generated from simonw/click-app-template-repository
-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
2 changed files
with
46 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import re | ||
|
||
RESERVED = ("_id", "_item", "_version", "_commit", "rowid") | ||
reserved_with_suffix_re = re.compile("^({})_*$".format("|".join(RESERVED))) | ||
|
||
|
||
def fix_reserved_columns(item): | ||
if not any(reserved_with_suffix_re.match(key) for key in item): | ||
return item | ||
|
||
return {_fix_key(key): item[key] for key in item} | ||
|
||
|
||
def _fix_key(key): | ||
# Add a trailing _ if it's reserved or reserved with _ suffix | ||
if reserved_with_suffix_re.match(key): | ||
return key + "_" | ||
else: | ||
return key |
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,27 @@ | ||
from git_history.utils import fix_reserved_columns | ||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"column,expected", | ||
( | ||
("_id", "_id_"), | ||
("_item", "_item_"), | ||
("_version", "_version_"), | ||
("_commit", "_commit_"), | ||
("rowid", "rowid_"), | ||
("rowid_", "rowid__"), | ||
("_id__", "_id___"), | ||
), | ||
) | ||
def test_fix_reserved_columns(column, expected): | ||
item = {column: 1} | ||
fixed = fix_reserved_columns(item) | ||
assert fixed == {expected: 1} | ||
assert item is not fixed | ||
|
||
|
||
def test_fix_reserved_columns_unchanged_if_no_reserved(): | ||
item = {"id": 1, "version": "v2"} | ||
fixed = fix_reserved_columns(item) | ||
assert item is fixed |