Skip to content

Commit

Permalink
Merge pull request #1 from meltano/edgarrmondragon/test/test-more-sel…
Browse files Browse the repository at this point in the history
…ect-cli-select-service

test: Add more tests for `SelectService` and the `select` command
  • Loading branch information
andyoneal committed May 2, 2024
2 parents 6754b22 + 952ace8 commit 4b5b63f
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/meltano/core/elt_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ELTContext: # noqa: WPS230

def __init__(
self,
project,
project: Project,
job: Job | None = None,
session=None,
extractor: PluginContext | None = None,
Expand Down
7 changes: 6 additions & 1 deletion tests/fixtures/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,12 @@ def discovery(): # noqa: WPS213
"kind": "object",
"value": {"nested": "from_default"},
},
{"name": "hidden", "kind": "hidden", "value": 42},
{
"name": "hidden",
"kind": "integer",
"value": 42,
"hidden": True,
},
{"name": "boolean", "kind": "boolean"},
{"name": "auth.username"},
{"name": "auth.password", "sensitive": True},
Expand Down
43 changes: 43 additions & 0 deletions tests/meltano/cli/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

from asserts import assert_cli_runner
from meltano.cli import cli
from meltano.core.plugin.singer.catalog import (
ListSelectedExecutor,
SelectedNode,
SelectionType,
)
from meltano.core.select_service import SelectService


class TestCliSelect:
Expand Down Expand Up @@ -33,3 +39,40 @@ def test_update_select_pattern(self, cli_runner, tap):
assert_cli_runner(result)
json_config = json.loads(result.stdout)
assert "mock.*" not in json_config["_select"]

@pytest.mark.usefixtures("project")
def test_select_list(self, cli_runner, tap, monkeypatch: pytest.MonkeyPatch):
async def mock_list_all(*args, **kwargs): # noqa: ARG001
result = ListSelectedExecutor()
result.streams = {
SelectedNode(key="users", selection=SelectionType.SELECTED)
}
result.properties = {
"users": {
SelectedNode(key="id", selection=SelectionType.SELECTED),
SelectedNode(key="name", selection=SelectionType.EXCLUDED),
}
}
return result

monkeypatch.setattr(
SelectService,
"list_all",
mock_list_all,
)

# list selection
result = cli_runner.invoke(
cli,
[
"--no-environment",
"select",
tap.name,
"--list",
"--all",
],
)
assert_cli_runner(result)

assert "[SelectionType.SELECTED] users.id" in result.stdout
assert "[SelectionType.EXCLUDED] users.name" in result.stdout
105 changes: 105 additions & 0 deletions tests/meltano/core/test_select_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from __future__ import annotations

import json
import typing as t
from collections import OrderedDict

import pytest

from meltano.core.plugin.singer.catalog import SelectedNode, SelectionType
from meltano.core.plugin.singer.tap import SingerTap
from meltano.core.select_service import SelectService

if t.TYPE_CHECKING:
from sqlalchemy.orm.session import Session

from meltano.core.project import Project


@pytest.mark.asyncio()
@pytest.mark.usefixtures("tap")
async def test_select_service_list_all(
project: Project,
session: Session,
monkeypatch: pytest.MonkeyPatch,
):
catalog = {
"streams": [
{
"stream": "users",
"tap_stream_id": "users",
"metadata": [
{"breadcrumb": [], "metadata": {"selected": True}},
],
"schema": {
"properties": {
"id": {"type": "integer"},
}
},
}
]
}
extractor = "tap-mock"
service = SelectService(project, extractor)

async def mock_run_discovery(tap, plugin_invoker, catalog_path): # noqa: ARG001
with catalog_path.open("w") as catalog_file:
json.dump(catalog, catalog_file)

# Mock tap's run_discovery method
monkeypatch.setattr(
SingerTap,
"run_discovery",
mock_run_discovery,
)

list_all = await service.list_all(session, refresh=False)
assert list_all.streams == {
SelectedNode(
key="users",
selection=SelectionType.SELECTED,
)
}
assert list_all.properties == OrderedDict(
{
"users": {
SelectedNode(
key="id",
selection=SelectionType.AUTOMATIC,
),
}
}
)

# Update the catalog to include a new property
catalog["streams"][0]["schema"]["properties"]["name"] = {"type": "string"} # noqa: WPS219

# Without refreshing the catalog, the new property should not be included
list_all = await service.list_all(session, refresh=False)
assert list_all.properties == OrderedDict(
{
"users": {
SelectedNode(
key="id",
selection=SelectionType.AUTOMATIC,
),
}
}
)

# Refreshing the catalog should include the new property
list_all = await service.list_all(session, refresh=True)
assert list_all.properties == OrderedDict(
{
"users": {
SelectedNode(
key="id",
selection=SelectionType.AUTOMATIC,
),
SelectedNode(
key="name",
selection=SelectionType.AUTOMATIC,
),
}
}
)

0 comments on commit 4b5b63f

Please sign in to comment.