-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow defining a json file with preferred aliases (#1382)
* fix psycopg.sql.Identifier in \ev handling (#1384) * Allow defining a json file with preferred aliases At $WORK we have a lot of tables with names like `foo_noun_verb` or `foo_noun_related-noun_verb` and so while the default aliasing is very helpful for shortening unwieldy names we do end up with lots of aliases like `LEFT JOIN fnv on fnv2.id = fnv.fnv2_id` This change will allow defining a json file of preferred aliases ``` > cat ~/.config/pgcli/aliases.json { "foo_user": "user", "foo_user_group": "user_group" } ``` so the alias suggestion for `SELECT * FROM foo_user` will be `SELECT * FROM foo_user AS user` instead of the default `SELECT * FROM foo_user AS fu` * When cannot open or parse alias_map_file raise error Raise a (hopefully) helpful exception when the alias_map_file cannot be parsed or does not exist * Add tests for load_alias_map_file * Add tests for generate_alias * Update AUTHORS file * Remove comment. Discussed this on the PR with a project maintainer --------- Co-authored-by: Andy Schoenberger <[email protected]> Co-authored-by: Rob B <[email protected]> Co-authored-by: Irina Truong <[email protected]>
- Loading branch information
1 parent
43360b5
commit 97a1fd6
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
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
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,76 @@ | ||
import pytest | ||
from pgcli import pgcompleter | ||
|
||
|
||
def test_load_alias_map_file_missing_file(): | ||
with pytest.raises( | ||
pgcompleter.InvalidMapFile, | ||
match=r"Cannot read alias_map_file - /path/to/non-existent/file.json does not exist$", | ||
): | ||
pgcompleter.load_alias_map_file("/path/to/non-existent/file.json") | ||
|
||
|
||
def test_load_alias_map_file_invalid_json(tmp_path): | ||
fpath = tmp_path / "foo.json" | ||
fpath.write_text("this is not valid json") | ||
with pytest.raises(pgcompleter.InvalidMapFile, match=r".*is not valid json$"): | ||
pgcompleter.load_alias_map_file(str(fpath)) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"table_name, alias", | ||
[ | ||
("SomE_Table", "SET"), | ||
("SOmeTabLe", "SOTL"), | ||
("someTable", "T"), | ||
], | ||
) | ||
def test_generate_alias_uses_upper_case_letters_from_name(table_name, alias): | ||
assert pgcompleter.generate_alias(table_name) == alias | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"table_name, alias", | ||
[ | ||
("some_tab_le", "stl"), | ||
("s_ome_table", "sot"), | ||
("sometable", "s"), | ||
], | ||
) | ||
def test_generate_alias_uses_first_char_and_every_preceded_by_underscore( | ||
table_name, alias | ||
): | ||
assert pgcompleter.generate_alias(table_name) == alias | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"table_name, alias_map, alias", | ||
[ | ||
("some_table", {"some_table": "my_alias"}, "my_alias"), | ||
], | ||
) | ||
def test_generate_alias_can_use_alias_map(table_name, alias_map, alias): | ||
assert pgcompleter.generate_alias(table_name, alias_map) == alias | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"table_name, alias_map, alias", | ||
[ | ||
("SomeTable", {"SomeTable": "my_alias"}, "my_alias"), | ||
], | ||
) | ||
def test_generate_alias_prefers_alias_over_upper_case_name( | ||
table_name, alias_map, alias | ||
): | ||
assert pgcompleter.generate_alias(table_name, alias_map) == alias | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"table_name, alias", | ||
[ | ||
("Some_tablE", "SE"), | ||
("SomeTab_le", "ST"), | ||
], | ||
) | ||
def test_generate_alias_prefers_upper_case_name_over_underscore_name(table_name, alias): | ||
assert pgcompleter.generate_alias(table_name) == alias |