Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

Commit

Permalink
Update project layout for better module import(#45)
Browse files Browse the repository at this point in the history
* Update project layout for better module import
  • Loading branch information
zhongnansu authored May 22, 2020
1 parent 6f8590e commit 80f370e
Show file tree
Hide file tree
Showing 22 changed files with 36 additions and 35 deletions.
11 changes: 6 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

_version_re = re.compile(r"__version__\s+=\s+(.*)")

with open("src/__init__.py", "rb") as f:
with open("src/odfe_sql_cli/__init__.py", "rb") as f:
version = str(
ast.literal_eval(_version_re.search(f.read().decode("utf-8")).group(1))
)
Expand All @@ -46,14 +46,15 @@
author_email="[email protected]",
version=version,
license="Apache 2.0",
url="https://opendistro.github.io/for-elasticsearch-docs/",
packages=find_packages(),
package_data={"src": ["conf/clirc", "esliterals/esliterals.json"]},
url="https://opendistro.github.io/for-elasticsearch-docs/docs/sql/cli/",
packages=find_packages('src'),
package_dir={'': 'src'},
package_data={"odfe_sql_cli": ["conf/clirc", "esliterals/esliterals.json"]},
description=description,
long_description=long_description,
long_description_content_type="text/markdown",
install_requires=install_requirements,
entry_points={"console_scripts": ["odfesql=src.main:cli"]},
entry_points={"console_scripts": ["odfesql=odfe_sql_cli.main:cli"]},
classifiers=[
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/config.py → src/odfe_sql_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def get_config(clirc_file=None):
This config comes from either existing config in the OS, or create a config file in the OS, and write default config
including in the package to it.
"""
from src.conf import __file__ as package_root
from .conf import __file__ as package_root

package_root = os.path.dirname(package_root)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/odfesql_cli.py → src/odfe_sql_cli/odfesql_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from .esstyle import style_factory, style_factory_output
from .formatter import Formatter
from .utils import OutputSettings
from .__init__ import __version__
from . import __version__


# Ref: https://stackoverflow.com/questions/30425105/filter-special-chars-such-as-color-codes-from-shell-output
Expand Down Expand Up @@ -174,7 +174,7 @@ def _get_literals(self):
:return: a dict that is parsed from esliterals.json
"""
from src.esliterals import __file__ as package_root
from .esliterals import __file__ as package_root

package_root = os.path.dirname(package_root)

Expand Down
File renamed without changes.
Empty file added tests/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import os
import pytest

from utils import create_index, delete_index, get_connection
from .utils import create_index, delete_index, get_connection


@pytest.fixture(scope="function")
Expand All @@ -34,7 +34,7 @@ def connection():

@pytest.fixture(scope="function")
def default_config_location():
from src.conf import __file__ as package_root
from src.odfe_sql_cli.conf import __file__ as package_root

package_root = os.path.dirname(package_root)
default_config = os.path.join(package_root, "clirc")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import stat
import pytest

from src.config import ensure_dir_exists
from src.odfe_sql_cli.config import ensure_dir_exists


class TestConfig:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_esconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
from elasticsearch.exceptions import ConnectionError
from elasticsearch import Elasticsearch, RequestsHttpConnection

from utils import estest, load_data, run, TEST_INDEX_NAME
from src.esconnection import ESConnection
from .utils import estest, load_data, run, TEST_INDEX_NAME
from src.odfe_sql_cli.esconnection import ESConnection

INVALID_ENDPOINT = "http://invalid:9200"
OPEN_DISTRO_ENDPOINT = "https://opedistro:9200"
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_query_nonexistent_index(self, connection):
"type": "IndexNotFoundException",
}

with mock.patch("src.esconnection.click.secho") as mock_secho:
with mock.patch("src.odfe_sql_cli.esconnection.click.secho") as mock_secho:
run(connection, "select * from non-existed")

mock_secho.assert_called_with(message=str(expected), fg="red")
Expand All @@ -68,7 +68,7 @@ def test_connection_fail(self):
test_executor = ESConnection(endpoint=INVALID_ENDPOINT)
err_message = "Can not connect to endpoint %s" % INVALID_ENDPOINT

with mock.patch("sys.exit") as mock_sys_exit, mock.patch("src.esconnection.click.secho") as mock_secho:
with mock.patch("sys.exit") as mock_sys_exit, mock.patch("src.odfe_sql_cli.esconnection.click.secho") as mock_secho:
test_executor.set_connection()

mock_sys_exit.assert_called()
Expand All @@ -83,7 +83,7 @@ def side_effect_set_connection(is_reconnected):
else:
return ConnectionError()

with mock.patch("src.esconnection.click.secho") as mock_secho, mock.patch.object(
with mock.patch("src.odfe_sql_cli.esconnection.click.secho") as mock_secho, mock.patch.object(
test_esexecutor, "set_connection"
) as mock_set_connection:
# Assume reconnection success
Expand Down
12 changes: 6 additions & 6 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
import pytest
from collections import namedtuple

from src.odfesql_cli import OdfeSqlCli, COLOR_CODE_REGEX
from src.formatter import Formatter
from src.utils import OutputSettings
from src.odfe_sql_cli.odfesql_cli import OdfeSqlCli, COLOR_CODE_REGEX
from src.odfe_sql_cli.formatter import Formatter
from src.odfe_sql_cli.utils import OutputSettings


class TestFormatter:
@pytest.fixture
def pset_pager_mocks(self):
cli = OdfeSqlCli()
with mock.patch("src.main.click.echo") as mock_echo, mock.patch(
"src.main.click.echo_via_pager"
with mock.patch("src.odfe_sql_cli.main.click.echo") as mock_echo, mock.patch(
"src.odfe_sql_cli.main.click.echo_via_pager"
) as mock_echo_via_pager, mock.patch.object(cli, "prompt_app") as mock_app:
yield cli, mock_echo, mock_echo_via_pager, mock_app

Expand Down Expand Up @@ -120,7 +120,7 @@ def test_format_output_vertical(self):
"age | 24",
]

with mock.patch("src.main.click.secho") as mock_secho, mock.patch("src.main.click.confirm") as mock_confirm:
with mock.patch("src.odfe_sql_cli.main.click.secho") as mock_secho, mock.patch("src.odfe_sql_cli.main.click.confirm") as mock_confirm:
expanded_results = formatter.format_output(data)

mock_secho.assert_called_with(message="Output longer than terminal width", fg="red")
Expand Down
8 changes: 4 additions & 4 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

from click.testing import CliRunner

from utils import estest, load_data, run, get_connection, TEST_INDEX_NAME
from src.main import cli
from src.odfesql_cli import OdfeSqlCli
from .utils import estest, load_data, TEST_INDEX_NAME
from src.odfe_sql_cli.main import cli
from src.odfe_sql_cli.odfesql_cli import OdfeSqlCli

INVALID_ENDPOINT = "http://invalid:9200"
ENDPOINT = "http://localhost:9200"
Expand All @@ -44,7 +44,7 @@ def test_explain(self, connection):
+-----+"""
)

with mock.patch("src.main.click.echo") as mock_echo, mock.patch("src.main.click.secho") as mock_secho:
with mock.patch("src.odfe_sql_cli.main.click.echo") as mock_echo, mock.patch("src.odfe_sql_cli.main.click.secho") as mock_secho:
runner = CliRunner()

# test -q -e
Expand Down
10 changes: 5 additions & 5 deletions tests/test_odfesql_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
from prompt_toolkit.shortcuts import PromptSession
from prompt_toolkit.input.defaults import create_pipe_input

from src.esbuffer import es_is_multiline
from utils import estest, load_data, TEST_INDEX_NAME, ENDPOINT
from src.odfesql_cli import OdfeSqlCli
from src.esconnection import ESConnection
from src.esstyle import style_factory
from src.odfe_sql_cli.esbuffer import es_is_multiline
from .utils import estest, load_data, TEST_INDEX_NAME, ENDPOINT
from src.odfe_sql_cli.odfesql_cli import OdfeSqlCli
from src.odfe_sql_cli.esconnection import ESConnection
from src.odfe_sql_cli.esstyle import style_factory

AUTH = None
QUERY_WITH_CTRL_D = "select * from %s;\r\x04\r" % TEST_INDEX_NAME
Expand Down
8 changes: 4 additions & 4 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import pytest
from elasticsearch import ConnectionError, helpers, ConnectionPool

from src.esconnection import ESConnection
from src.utils import OutputSettings
from src.formatter import Formatter
from src.odfe_sql_cli.esconnection import ESConnection
from src.odfe_sql_cli.utils import OutputSettings
from src.odfe_sql_cli.formatter import Formatter

TEST_INDEX_NAME = "odfesql_cli_test"
ENDPOINT = "http://localhost:9200"
Expand Down Expand Up @@ -88,4 +88,4 @@ def run(test_executor, query, use_console=True):
# use @estest annotation to mark test functions
estest = pytest.mark.skipif(
not CAN_CONNECT_TO_ES, reason="Need a Elasticsearch server running at localhost:9200 accessible"
)
)

0 comments on commit 80f370e

Please sign in to comment.