fix: commit verified semantic repair changes
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# [DEF:backend.src.api.routes.__tests__.test_git_api:Module]
|
||||
# @RELATION: VERIFIES -> src.api.routes.git
|
||||
# [DEF:TestGitApi:Module]
|
||||
# @COMPLEXITY: 3
|
||||
# @RELATION: VERIFIES ->[src.api.routes.git]
|
||||
# @PURPOSE: API tests for Git configurations and repository operations.
|
||||
|
||||
import pytest
|
||||
@@ -9,32 +10,52 @@ from fastapi import HTTPException
|
||||
from src.api.routes import git as git_routes
|
||||
from src.models.git import GitServerConfig, GitProvider, GitStatus, GitRepository
|
||||
|
||||
|
||||
class DbMock:
|
||||
def __init__(self, data=None):
|
||||
self._data = data or []
|
||||
self._deleted = []
|
||||
self._added = []
|
||||
self._filtered = None
|
||||
|
||||
def query(self, model):
|
||||
self._model = model
|
||||
self._filtered = None
|
||||
return self
|
||||
|
||||
def filter(self, condition):
|
||||
# Simplistic mocking for tests, assuming equality checks
|
||||
for item in self._data:
|
||||
# We assume condition is an equality expression like GitServerConfig.id == "123"
|
||||
# It's hard to eval the condition exactly in a mock without complex parsing,
|
||||
# so we'll just return items where type matches.
|
||||
pass
|
||||
# Honor simple SQLAlchemy equality expressions used by these route tests.
|
||||
candidates = [
|
||||
item
|
||||
for item in self._data
|
||||
if not hasattr(self, "_model") or isinstance(item, self._model)
|
||||
]
|
||||
try:
|
||||
left_key = getattr(getattr(condition, "left", None), "key", None)
|
||||
right_value = getattr(getattr(condition, "right", None), "value", None)
|
||||
if left_key is not None and right_value is not None:
|
||||
self._filtered = [
|
||||
item
|
||||
for item in candidates
|
||||
if getattr(item, left_key, None) == right_value
|
||||
]
|
||||
else:
|
||||
self._filtered = candidates
|
||||
except Exception:
|
||||
self._filtered = candidates
|
||||
return self
|
||||
|
||||
def first(self):
|
||||
if self._filtered is not None:
|
||||
return self._filtered[0] if self._filtered else None
|
||||
for item in self._data:
|
||||
if hasattr(self, "_model") and isinstance(item, self._model):
|
||||
return item
|
||||
return None
|
||||
|
||||
def all(self):
|
||||
if self._filtered is not None:
|
||||
return list(self._filtered)
|
||||
return self._data
|
||||
|
||||
def add(self, item):
|
||||
@@ -57,254 +78,410 @@ class DbMock:
|
||||
if not hasattr(item, "last_validated"):
|
||||
item.last_validated = "2026-03-08T00:00:00Z"
|
||||
|
||||
|
||||
# [DEF:test_get_git_configs_masks_pat:Function]
|
||||
# @RELATION: BINDS_TO ->[TestGitApi]
|
||||
def test_get_git_configs_masks_pat():
|
||||
"""
|
||||
@PRE: Database session `db` is available.
|
||||
@POST: Returns a list of all GitServerConfig objects from the database with PAT masked.
|
||||
"""
|
||||
db = DbMock([GitServerConfig(
|
||||
id="config-1", name="Test Server", provider=GitProvider.GITHUB,
|
||||
url="https://github.com", pat="secret-token",
|
||||
status=GitStatus.CONNECTED, last_validated="2026-03-08T00:00:00Z"
|
||||
)])
|
||||
|
||||
db = DbMock(
|
||||
[
|
||||
GitServerConfig(
|
||||
id="config-1",
|
||||
name="Test Server",
|
||||
provider=GitProvider.GITHUB,
|
||||
url="https://github.com",
|
||||
pat="secret-token",
|
||||
status=GitStatus.CONNECTED,
|
||||
last_validated="2026-03-08T00:00:00Z",
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
result = asyncio.run(git_routes.get_git_configs(db=db))
|
||||
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].pat == "********"
|
||||
assert result[0].name == "Test Server"
|
||||
|
||||
|
||||
# [/DEF:test_get_git_configs_masks_pat:Function]
|
||||
|
||||
|
||||
# [DEF:test_create_git_config_persists_config:Function]
|
||||
# @RELATION: BINDS_TO ->[TestGitApi]
|
||||
def test_create_git_config_persists_config():
|
||||
"""
|
||||
@PRE: `config` contains valid GitServerConfigCreate data.
|
||||
@POST: A new GitServerConfig record is created in the database.
|
||||
"""
|
||||
from src.api.routes.git_schemas import GitServerConfigCreate
|
||||
|
||||
db = DbMock()
|
||||
config = GitServerConfigCreate(
|
||||
name="New Server", provider=GitProvider.GITLAB,
|
||||
url="https://gitlab.com", pat="new-token",
|
||||
default_branch="master"
|
||||
name="New Server",
|
||||
provider=GitProvider.GITLAB,
|
||||
url="https://gitlab.com",
|
||||
pat="new-token",
|
||||
default_branch="master",
|
||||
)
|
||||
|
||||
|
||||
result = asyncio.run(git_routes.create_git_config(config=config, db=db))
|
||||
|
||||
|
||||
assert len(db._added) == 1
|
||||
assert db._added[0].name == "New Server"
|
||||
assert db._added[0].pat == "new-token"
|
||||
assert result.name == "New Server"
|
||||
assert result.pat == "new-token" # Note: route returns unmasked until serialized by FastAPI usually, but in tests schema might catch it or not.
|
||||
assert (
|
||||
result.pat == "new-token"
|
||||
) # Note: route returns unmasked until serialized by FastAPI usually, but in tests schema might catch it or not.
|
||||
|
||||
|
||||
# [/DEF:test_create_git_config_persists_config:Function]
|
||||
|
||||
from src.api.routes.git_schemas import GitServerConfigUpdate
|
||||
|
||||
|
||||
# [DEF:test_update_git_config_modifies_record:Function]
|
||||
# @RELATION: BINDS_TO ->[TestGitApi]
|
||||
def test_update_git_config_modifies_record():
|
||||
"""
|
||||
@PRE: `config_id` corresponds to an existing configuration.
|
||||
@POST: The configuration record is updated in the database, preserving PAT if masked is sent.
|
||||
"""
|
||||
existing_config = GitServerConfig(
|
||||
id="config-1", name="Old Server", provider=GitProvider.GITHUB,
|
||||
url="https://github.com", pat="old-token",
|
||||
status=GitStatus.CONNECTED, last_validated="2026-03-08T00:00:00Z"
|
||||
id="config-1",
|
||||
name="Old Server",
|
||||
provider=GitProvider.GITHUB,
|
||||
url="https://github.com",
|
||||
pat="old-token",
|
||||
status=GitStatus.CONNECTED,
|
||||
last_validated="2026-03-08T00:00:00Z",
|
||||
)
|
||||
|
||||
# The monkeypatched query will return existing_config as it's the only one in the list
|
||||
class SingleConfigDbMock:
|
||||
def query(self, *args): return self
|
||||
def filter(self, *args): return self
|
||||
def first(self): return existing_config
|
||||
def commit(self): pass
|
||||
def refresh(self, config): pass
|
||||
def query(self, *args):
|
||||
return self
|
||||
|
||||
def filter(self, *args):
|
||||
return self
|
||||
|
||||
def first(self):
|
||||
return existing_config
|
||||
|
||||
def commit(self):
|
||||
pass
|
||||
|
||||
def refresh(self, config):
|
||||
pass
|
||||
|
||||
db = SingleConfigDbMock()
|
||||
update_data = GitServerConfigUpdate(name="Updated Server", pat="********")
|
||||
|
||||
result = asyncio.run(git_routes.update_git_config(config_id="config-1", config_update=update_data, db=db))
|
||||
|
||||
|
||||
result = asyncio.run(
|
||||
git_routes.update_git_config(
|
||||
config_id="config-1", config_update=update_data, db=db
|
||||
)
|
||||
)
|
||||
|
||||
assert existing_config.name == "Updated Server"
|
||||
assert existing_config.pat == "old-token" # Ensure PAT is not overwritten with asterisks
|
||||
assert (
|
||||
existing_config.pat == "old-token"
|
||||
) # Ensure PAT is not overwritten with asterisks
|
||||
assert result.pat == "********"
|
||||
|
||||
|
||||
# [/DEF:test_update_git_config_modifies_record:Function]
|
||||
|
||||
|
||||
# [DEF:test_update_git_config_raises_404_if_not_found:Function]
|
||||
# @RELATION: BINDS_TO ->[TestGitApi]
|
||||
def test_update_git_config_raises_404_if_not_found():
|
||||
"""
|
||||
@PRE: `config_id` corresponds to a missing configuration.
|
||||
@THROW: HTTPException 404
|
||||
"""
|
||||
db = DbMock([]) # Empty db
|
||||
db = DbMock([]) # Empty db
|
||||
update_data = GitServerConfigUpdate(name="Updated Server", pat="new-token")
|
||||
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
asyncio.run(git_routes.update_git_config(config_id="config-1", config_update=update_data, db=db))
|
||||
|
||||
asyncio.run(
|
||||
git_routes.update_git_config(
|
||||
config_id="config-1", config_update=update_data, db=db
|
||||
)
|
||||
)
|
||||
|
||||
assert exc_info.value.status_code == 404
|
||||
assert exc_info.value.detail == "Configuration not found"
|
||||
|
||||
|
||||
# [/DEF:test_update_git_config_raises_404_if_not_found:Function]
|
||||
|
||||
|
||||
# [DEF:test_delete_git_config_removes_record:Function]
|
||||
# @RELATION: BINDS_TO ->[TestGitApi]
|
||||
def test_delete_git_config_removes_record():
|
||||
"""
|
||||
@PRE: `config_id` corresponds to an existing configuration.
|
||||
@POST: The configuration record is removed from the database.
|
||||
"""
|
||||
existing_config = GitServerConfig(id="config-1")
|
||||
|
||||
class SingleConfigDbMock:
|
||||
def query(self, *args): return self
|
||||
def filter(self, *args): return self
|
||||
def first(self): return existing_config
|
||||
def delete(self, config): self.deleted = config
|
||||
def commit(self): pass
|
||||
def query(self, *args):
|
||||
return self
|
||||
|
||||
def filter(self, *args):
|
||||
return self
|
||||
|
||||
def first(self):
|
||||
return existing_config
|
||||
|
||||
def delete(self, config):
|
||||
self.deleted = config
|
||||
|
||||
def commit(self):
|
||||
pass
|
||||
|
||||
db = SingleConfigDbMock()
|
||||
|
||||
|
||||
result = asyncio.run(git_routes.delete_git_config(config_id="config-1", db=db))
|
||||
|
||||
|
||||
assert db.deleted == existing_config
|
||||
assert result["status"] == "success"
|
||||
|
||||
|
||||
# [/DEF:test_delete_git_config_removes_record:Function]
|
||||
|
||||
|
||||
# [DEF:test_test_git_config_validates_connection_successfully:Function]
|
||||
# @RELATION: BINDS_TO ->[TestGitApi]
|
||||
def test_test_git_config_validates_connection_successfully(monkeypatch):
|
||||
"""
|
||||
@PRE: `config` contains provider, url, and pat.
|
||||
@POST: Returns success if the connection is validated via GitService.
|
||||
"""
|
||||
|
||||
class MockGitService:
|
||||
async def test_connection(self, provider, url, pat):
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(git_routes, "git_service", MockGitService())
|
||||
from src.api.routes.git_schemas import GitServerConfigCreate
|
||||
|
||||
|
||||
config = GitServerConfigCreate(
|
||||
name="Test Server", provider=GitProvider.GITHUB,
|
||||
url="https://github.com", pat="test-pat"
|
||||
name="Test Server",
|
||||
provider=GitProvider.GITHUB,
|
||||
url="https://github.com",
|
||||
pat="test-pat",
|
||||
)
|
||||
db = DbMock([])
|
||||
|
||||
|
||||
result = asyncio.run(git_routes.test_git_config(config=config, db=db))
|
||||
|
||||
|
||||
assert result["status"] == "success"
|
||||
|
||||
|
||||
# [/DEF:test_test_git_config_validates_connection_successfully:Function]
|
||||
|
||||
|
||||
# [DEF:test_test_git_config_fails_validation:Function]
|
||||
# @RELATION: BINDS_TO ->[TestGitApi]
|
||||
def test_test_git_config_fails_validation(monkeypatch):
|
||||
"""
|
||||
@PRE: `config` contains provider, url, and pat BUT connection fails.
|
||||
@THROW: HTTPException 400
|
||||
"""
|
||||
|
||||
class MockGitService:
|
||||
async def test_connection(self, provider, url, pat):
|
||||
return False
|
||||
|
||||
monkeypatch.setattr(git_routes, "git_service", MockGitService())
|
||||
from src.api.routes.git_schemas import GitServerConfigCreate
|
||||
|
||||
|
||||
config = GitServerConfigCreate(
|
||||
name="Test Server", provider=GitProvider.GITHUB,
|
||||
url="https://github.com", pat="bad-pat"
|
||||
name="Test Server",
|
||||
provider=GitProvider.GITHUB,
|
||||
url="https://github.com",
|
||||
pat="bad-pat",
|
||||
)
|
||||
db = DbMock([])
|
||||
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
asyncio.run(git_routes.test_git_config(config=config, db=db))
|
||||
|
||||
|
||||
assert exc_info.value.status_code == 400
|
||||
assert exc_info.value.detail == "Connection failed"
|
||||
|
||||
|
||||
# [/DEF:test_test_git_config_fails_validation:Function]
|
||||
|
||||
|
||||
# [DEF:test_list_gitea_repositories_returns_payload:Function]
|
||||
# @RELATION: BINDS_TO ->[TestGitApi]
|
||||
def test_list_gitea_repositories_returns_payload(monkeypatch):
|
||||
"""
|
||||
@PRE: config_id exists and provider is GITEA.
|
||||
@POST: Returns repositories visible to PAT user.
|
||||
"""
|
||||
|
||||
class MockGitService:
|
||||
async def list_gitea_repositories(self, url, pat):
|
||||
return [{"name": "test-repo", "full_name": "owner/test-repo", "private": True}]
|
||||
return [
|
||||
{"name": "test-repo", "full_name": "owner/test-repo", "private": True}
|
||||
]
|
||||
|
||||
monkeypatch.setattr(git_routes, "git_service", MockGitService())
|
||||
existing_config = GitServerConfig(
|
||||
id="config-1", name="Gitea Server", provider=GitProvider.GITEA,
|
||||
url="https://gitea.local", pat="gitea-token"
|
||||
id="config-1",
|
||||
name="Gitea Server",
|
||||
provider=GitProvider.GITEA,
|
||||
url="https://gitea.local",
|
||||
pat="gitea-token",
|
||||
)
|
||||
db = DbMock([existing_config])
|
||||
|
||||
result = asyncio.run(git_routes.list_gitea_repositories(config_id="config-1", db=db))
|
||||
|
||||
|
||||
result = asyncio.run(
|
||||
git_routes.list_gitea_repositories(config_id="config-1", db=db)
|
||||
)
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].name == "test-repo"
|
||||
assert result[0].private is True
|
||||
|
||||
|
||||
# [/DEF:test_list_gitea_repositories_returns_payload:Function]
|
||||
|
||||
|
||||
# [DEF:test_list_gitea_repositories_rejects_non_gitea:Function]
|
||||
# @RELATION: BINDS_TO ->[TestGitApi]
|
||||
def test_list_gitea_repositories_rejects_non_gitea(monkeypatch):
|
||||
"""
|
||||
@PRE: config_id exists and provider is NOT GITEA.
|
||||
@THROW: HTTPException 400
|
||||
"""
|
||||
existing_config = GitServerConfig(
|
||||
id="config-1", name="GitHub Server", provider=GitProvider.GITHUB,
|
||||
url="https://github.com", pat="token"
|
||||
id="config-1",
|
||||
name="GitHub Server",
|
||||
provider=GitProvider.GITHUB,
|
||||
url="https://github.com",
|
||||
pat="token",
|
||||
)
|
||||
db = DbMock([existing_config])
|
||||
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
asyncio.run(git_routes.list_gitea_repositories(config_id="config-1", db=db))
|
||||
|
||||
|
||||
assert exc_info.value.status_code == 400
|
||||
assert "GITEA provider only" in exc_info.value.detail
|
||||
|
||||
|
||||
# [/DEF:test_list_gitea_repositories_rejects_non_gitea:Function]
|
||||
|
||||
|
||||
# [DEF:test_create_remote_repository_creates_provider_repo:Function]
|
||||
# @RELATION: BINDS_TO ->[TestGitApi]
|
||||
def test_create_remote_repository_creates_provider_repo(monkeypatch):
|
||||
"""
|
||||
@PRE: config_id exists and PAT has creation permissions.
|
||||
@POST: Returns normalized remote repository payload.
|
||||
"""
|
||||
|
||||
class MockGitService:
|
||||
async def create_gitlab_repository(self, server_url, pat, name, private, description, auto_init, default_branch):
|
||||
async def create_gitlab_repository(
|
||||
self, server_url, pat, name, private, description, auto_init, default_branch
|
||||
):
|
||||
return {
|
||||
"name": name,
|
||||
"full_name": f"user/{name}",
|
||||
"private": private,
|
||||
"clone_url": f"{server_url}/user/{name}.git"
|
||||
"clone_url": f"{server_url}/user/{name}.git",
|
||||
}
|
||||
|
||||
monkeypatch.setattr(git_routes, "git_service", MockGitService())
|
||||
from src.api.routes.git_schemas import RemoteRepoCreateRequest
|
||||
|
||||
|
||||
existing_config = GitServerConfig(
|
||||
id="config-1", name="GitLab Server", provider=GitProvider.GITLAB,
|
||||
url="https://gitlab.com", pat="token"
|
||||
id="config-1",
|
||||
name="GitLab Server",
|
||||
provider=GitProvider.GITLAB,
|
||||
url="https://gitlab.com",
|
||||
pat="token",
|
||||
)
|
||||
db = DbMock([existing_config])
|
||||
|
||||
|
||||
request = RemoteRepoCreateRequest(name="new-repo", private=True, description="desc")
|
||||
result = asyncio.run(git_routes.create_remote_repository(config_id="config-1", request=request, db=db))
|
||||
|
||||
result = asyncio.run(
|
||||
git_routes.create_remote_repository(
|
||||
config_id="config-1", request=request, db=db
|
||||
)
|
||||
)
|
||||
|
||||
assert result.provider == GitProvider.GITLAB
|
||||
assert result.name == "new-repo"
|
||||
assert result.full_name == "user/new-repo"
|
||||
|
||||
|
||||
# [/DEF:test_create_remote_repository_creates_provider_repo:Function]
|
||||
|
||||
|
||||
# [DEF:test_init_repository_initializes_and_saves_binding:Function]
|
||||
# @RELATION: BINDS_TO ->[TestGitApi]
|
||||
def test_init_repository_initializes_and_saves_binding(monkeypatch):
|
||||
"""
|
||||
@PRE: `dashboard_ref` exists and `init_data` contains valid config_id and remote_url.
|
||||
@POST: Repository is initialized on disk and a GitRepository record is saved in DB.
|
||||
"""
|
||||
from src.api.routes.git_schemas import RepoInitRequest
|
||||
|
||||
|
||||
class MockGitService:
|
||||
def init_repo(self, dashboard_id, remote_url, pat, repo_key, default_branch):
|
||||
self.init_called = True
|
||||
|
||||
def _get_repo_path(self, dashboard_id, repo_key):
|
||||
return f"/tmp/repos/{repo_key}"
|
||||
|
||||
git_service_mock = MockGitService()
|
||||
monkeypatch.setattr(git_routes, "git_service", git_service_mock)
|
||||
monkeypatch.setattr(git_routes, "_resolve_dashboard_id_from_ref", lambda *args, **kwargs: 123)
|
||||
monkeypatch.setattr(git_routes, "_resolve_repo_key_from_ref", lambda *args, **kwargs: "dashboard-123")
|
||||
|
||||
monkeypatch.setattr(
|
||||
git_routes, "_resolve_dashboard_id_from_ref", lambda *args, **kwargs: 123
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
git_routes,
|
||||
"_resolve_repo_key_from_ref",
|
||||
lambda *args, **kwargs: "dashboard-123",
|
||||
)
|
||||
|
||||
existing_config = GitServerConfig(
|
||||
id="config-1", name="GitLab Server", provider=GitProvider.GITLAB,
|
||||
url="https://gitlab.com", pat="token", default_branch="main"
|
||||
id="config-1",
|
||||
name="GitLab Server",
|
||||
provider=GitProvider.GITLAB,
|
||||
url="https://gitlab.com",
|
||||
pat="token",
|
||||
default_branch="main",
|
||||
)
|
||||
db = DbMock([existing_config])
|
||||
|
||||
init_data = RepoInitRequest(config_id="config-1", remote_url="https://git.local/repo.git")
|
||||
|
||||
result = asyncio.run(git_routes.init_repository(dashboard_ref="123", init_data=init_data, config_manager=MagicMock(), db=db))
|
||||
|
||||
|
||||
init_data = RepoInitRequest(
|
||||
config_id="config-1", remote_url="https://git.local/repo.git"
|
||||
)
|
||||
|
||||
result = asyncio.run(
|
||||
git_routes.init_repository(
|
||||
dashboard_ref="123", init_data=init_data, config_manager=MagicMock(), db=db
|
||||
)
|
||||
)
|
||||
|
||||
assert result["status"] == "success"
|
||||
assert git_service_mock.init_called is True
|
||||
assert len(db._added) == 1
|
||||
assert isinstance(db._added[0], GitRepository)
|
||||
assert db._added[0].dashboard_id == 123
|
||||
|
||||
# [/DEF:backend.src.api.routes.__tests__.test_git_api:Module]
|
||||
|
||||
# [/DEF:test_init_repository_initializes_and_saves_binding:Function]
|
||||
# [/DEF:TestGitApi:Module]
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,5 @@
|
||||
# [DEF:backend.src.plugins.llm_analysis.__tests__.test_screenshot_service:Module]
|
||||
# [DEF:TestScreenshotService:Module]
|
||||
# @RELATION: VERIFIES ->[src.plugins.llm_analysis.service.ScreenshotService]
|
||||
# @COMPLEXITY: 3
|
||||
# @SEMANTICS: tests, screenshot-service, navigation, timeout-regression
|
||||
# @PURPOSE: Protect dashboard screenshot navigation from brittle networkidle waits.
|
||||
@@ -9,6 +10,7 @@ from src.plugins.llm_analysis.service import ScreenshotService
|
||||
|
||||
|
||||
# [DEF:test_iter_login_roots_includes_child_frames:Function]
|
||||
# @RELATION: BINDS_TO ->[TestScreenshotService]
|
||||
# @PURPOSE: Login discovery must search embedded auth frames, not only the main page.
|
||||
# @PRE: Page exposes child frames list.
|
||||
# @POST: Returned roots include page plus child frames in order.
|
||||
@@ -21,10 +23,13 @@ def test_iter_login_roots_includes_child_frames():
|
||||
roots = service._iter_login_roots(fake_page)
|
||||
|
||||
assert roots == [fake_page, frame_a, frame_b]
|
||||
|
||||
|
||||
# [/DEF:test_iter_login_roots_includes_child_frames:Function]
|
||||
|
||||
|
||||
# [DEF:test_response_looks_like_login_page_detects_login_markup:Function]
|
||||
# @RELATION: BINDS_TO ->[TestScreenshotService]
|
||||
# @PURPOSE: Direct login fallback must reject responses that render the login screen again.
|
||||
# @PRE: Response body contains stable login-page markers.
|
||||
# @POST: Helper returns True so caller treats fallback as failed authentication.
|
||||
@@ -45,10 +50,13 @@ def test_response_looks_like_login_page_detects_login_markup():
|
||||
)
|
||||
|
||||
assert result is True
|
||||
|
||||
|
||||
# [/DEF:test_response_looks_like_login_page_detects_login_markup:Function]
|
||||
|
||||
|
||||
# [DEF:test_find_first_visible_locator_skips_hidden_first_match:Function]
|
||||
# @RELATION: BINDS_TO ->[TestScreenshotService]
|
||||
# @PURPOSE: Locator helper must not reject a selector collection just because its first element is hidden.
|
||||
# @PRE: First matched element is hidden and second matched element is visible.
|
||||
# @POST: Helper returns the second visible candidate.
|
||||
@@ -73,18 +81,23 @@ async def test_find_first_visible_locator_skips_hidden_first_match():
|
||||
return self._elements[index]
|
||||
|
||||
service = ScreenshotService(env=type("Env", (), {})())
|
||||
hidden_then_visible = _FakeLocator([
|
||||
_FakeElement(False, "hidden"),
|
||||
_FakeElement(True, "visible"),
|
||||
])
|
||||
hidden_then_visible = _FakeLocator(
|
||||
[
|
||||
_FakeElement(False, "hidden"),
|
||||
_FakeElement(True, "visible"),
|
||||
]
|
||||
)
|
||||
|
||||
result = await service._find_first_visible_locator([hidden_then_visible])
|
||||
|
||||
assert result.label == "visible"
|
||||
|
||||
|
||||
# [/DEF:test_find_first_visible_locator_skips_hidden_first_match:Function]
|
||||
|
||||
|
||||
# [DEF:test_submit_login_via_form_post_uses_browser_context_request:Function]
|
||||
# @RELATION: BINDS_TO ->[TestScreenshotService]
|
||||
# @PURPOSE: Fallback login must submit hidden fields and credentials through the context request cookie jar.
|
||||
# @PRE: Login DOM exposes csrf hidden field and request context returns authenticated HTML.
|
||||
# @POST: Helper returns True and request payload contains csrf_token plus credentials plus request options.
|
||||
@@ -122,15 +135,25 @@ async def test_submit_login_via_form_post_uses_browser_context_request():
|
||||
def __init__(self):
|
||||
self.calls = []
|
||||
|
||||
async def post(self, url, form=None, headers=None, timeout=None, fail_on_status_code=None, max_redirects=None):
|
||||
self.calls.append({
|
||||
"url": url,
|
||||
"form": dict(form or {}),
|
||||
"headers": dict(headers or {}),
|
||||
"timeout": timeout,
|
||||
"fail_on_status_code": fail_on_status_code,
|
||||
"max_redirects": max_redirects,
|
||||
})
|
||||
async def post(
|
||||
self,
|
||||
url,
|
||||
form=None,
|
||||
headers=None,
|
||||
timeout=None,
|
||||
fail_on_status_code=None,
|
||||
max_redirects=None,
|
||||
):
|
||||
self.calls.append(
|
||||
{
|
||||
"url": url,
|
||||
"form": dict(form or {}),
|
||||
"headers": dict(headers or {}),
|
||||
"timeout": timeout,
|
||||
"fail_on_status_code": fail_on_status_code,
|
||||
"max_redirects": max_redirects,
|
||||
}
|
||||
)
|
||||
return _FakeResponse()
|
||||
|
||||
class _FakeContext:
|
||||
@@ -144,39 +167,48 @@ async def test_submit_login_via_form_post_uses_browser_context_request():
|
||||
|
||||
def locator(self, selector):
|
||||
if selector == "input[type='hidden'][name]":
|
||||
return _FakeLocator([
|
||||
_FakeInput("csrf_token", "csrf-123"),
|
||||
_FakeInput("next", "/superset/welcome/"),
|
||||
])
|
||||
return _FakeLocator(
|
||||
[
|
||||
_FakeInput("csrf_token", "csrf-123"),
|
||||
_FakeInput("next", "/superset/welcome/"),
|
||||
]
|
||||
)
|
||||
return _FakeLocator([])
|
||||
|
||||
env = type("Env", (), {"username": "admin", "password": "secret"})()
|
||||
service = ScreenshotService(env=env)
|
||||
page = _FakePage()
|
||||
|
||||
result = await service._submit_login_via_form_post(page, "https://example.test/login/")
|
||||
result = await service._submit_login_via_form_post(
|
||||
page, "https://example.test/login/"
|
||||
)
|
||||
|
||||
assert result is True
|
||||
assert page.context.request.calls == [{
|
||||
"url": "https://example.test/login/",
|
||||
"form": {
|
||||
"csrf_token": "csrf-123",
|
||||
"next": "/superset/welcome/",
|
||||
"username": "admin",
|
||||
"password": "secret",
|
||||
},
|
||||
"headers": {
|
||||
"Origin": "https://example.test",
|
||||
"Referer": "https://example.test/login/",
|
||||
},
|
||||
"timeout": 10000,
|
||||
"fail_on_status_code": False,
|
||||
"max_redirects": 0,
|
||||
}]
|
||||
assert page.context.request.calls == [
|
||||
{
|
||||
"url": "https://example.test/login/",
|
||||
"form": {
|
||||
"csrf_token": "csrf-123",
|
||||
"next": "/superset/welcome/",
|
||||
"username": "admin",
|
||||
"password": "secret",
|
||||
},
|
||||
"headers": {
|
||||
"Origin": "https://example.test",
|
||||
"Referer": "https://example.test/login/",
|
||||
},
|
||||
"timeout": 10000,
|
||||
"fail_on_status_code": False,
|
||||
"max_redirects": 0,
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
# [/DEF:test_submit_login_via_form_post_uses_browser_context_request:Function]
|
||||
|
||||
|
||||
# [DEF:test_submit_login_via_form_post_accepts_authenticated_redirect:Function]
|
||||
# @RELATION: BINDS_TO ->[TestScreenshotService]
|
||||
# @PURPOSE: Fallback login must treat non-login 302 redirect as success without waiting for redirect target.
|
||||
# @PRE: Request response is 302 with Location outside login path.
|
||||
# @POST: Helper returns True.
|
||||
@@ -212,7 +244,15 @@ async def test_submit_login_via_form_post_accepts_authenticated_redirect():
|
||||
return ""
|
||||
|
||||
class _FakeRequest:
|
||||
async def post(self, url, form=None, headers=None, timeout=None, fail_on_status_code=None, max_redirects=None):
|
||||
async def post(
|
||||
self,
|
||||
url,
|
||||
form=None,
|
||||
headers=None,
|
||||
timeout=None,
|
||||
fail_on_status_code=None,
|
||||
max_redirects=None,
|
||||
):
|
||||
return _FakeResponse()
|
||||
|
||||
class _FakeContext:
|
||||
@@ -232,13 +272,18 @@ async def test_submit_login_via_form_post_accepts_authenticated_redirect():
|
||||
env = type("Env", (), {"username": "admin", "password": "secret"})()
|
||||
service = ScreenshotService(env=env)
|
||||
|
||||
result = await service._submit_login_via_form_post(_FakePage(), "https://example.test/login/")
|
||||
result = await service._submit_login_via_form_post(
|
||||
_FakePage(), "https://example.test/login/"
|
||||
)
|
||||
|
||||
assert result is True
|
||||
|
||||
|
||||
# [/DEF:test_submit_login_via_form_post_accepts_authenticated_redirect:Function]
|
||||
|
||||
|
||||
# [DEF:test_submit_login_via_form_post_rejects_login_markup_response:Function]
|
||||
# @RELATION: BINDS_TO ->[TestScreenshotService]
|
||||
# @PURPOSE: Fallback login must fail when POST response still contains login form content.
|
||||
# @PRE: Login DOM exposes csrf hidden field and request response renders login markup.
|
||||
# @POST: Helper returns False.
|
||||
@@ -282,7 +327,15 @@ async def test_submit_login_via_form_post_rejects_login_markup_response():
|
||||
"""
|
||||
|
||||
class _FakeRequest:
|
||||
async def post(self, url, form=None, headers=None, timeout=None, fail_on_status_code=None, max_redirects=None):
|
||||
async def post(
|
||||
self,
|
||||
url,
|
||||
form=None,
|
||||
headers=None,
|
||||
timeout=None,
|
||||
fail_on_status_code=None,
|
||||
max_redirects=None,
|
||||
):
|
||||
return _FakeResponse()
|
||||
|
||||
class _FakeContext:
|
||||
@@ -302,13 +355,18 @@ async def test_submit_login_via_form_post_rejects_login_markup_response():
|
||||
env = type("Env", (), {"username": "admin", "password": "secret"})()
|
||||
service = ScreenshotService(env=env)
|
||||
|
||||
result = await service._submit_login_via_form_post(_FakePage(), "https://example.test/login/")
|
||||
result = await service._submit_login_via_form_post(
|
||||
_FakePage(), "https://example.test/login/"
|
||||
)
|
||||
|
||||
assert result is False
|
||||
|
||||
|
||||
# [/DEF:test_submit_login_via_form_post_rejects_login_markup_response:Function]
|
||||
|
||||
|
||||
# [DEF:test_goto_resilient_falls_back_from_domcontentloaded_to_load:Function]
|
||||
# @RELATION: BINDS_TO ->[TestScreenshotService]
|
||||
# @PURPOSE: Pages with unstable primary wait must retry with fallback wait strategy.
|
||||
# @PRE: First page.goto call raises; second succeeds.
|
||||
# @POST: Helper returns second response and attempts both wait modes in order.
|
||||
@@ -340,5 +398,7 @@ async def test_goto_resilient_falls_back_from_domcontentloaded_to_load():
|
||||
("https://example.test/dashboard", "domcontentloaded", 1234),
|
||||
("https://example.test/dashboard", "load", 1234),
|
||||
]
|
||||
|
||||
|
||||
# [/DEF:test_goto_resilient_falls_back_from_domcontentloaded_to_load:Function]
|
||||
# [/DEF:backend.src.plugins.llm_analysis.__tests__.test_screenshot_service:Module]
|
||||
# [/DEF:TestScreenshotService:Module]
|
||||
|
||||
@@ -7,6 +7,8 @@ from src.models.llm import ValidationRecord
|
||||
# [DEF:test_health_service:Module]
|
||||
# @COMPLEXITY: 3
|
||||
# @PURPOSE: Unit tests for HealthService aggregation logic.
|
||||
# @RELATION: VERIFIES ->[src.services.health_service.HealthService]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_health_summary_aggregation():
|
||||
@@ -15,9 +17,9 @@ async def test_get_health_summary_aggregation():
|
||||
"""
|
||||
# Setup: Mock DB session
|
||||
db = MagicMock()
|
||||
|
||||
|
||||
now = datetime.utcnow()
|
||||
|
||||
|
||||
# Dashboard 1: Old FAIL, New PASS
|
||||
rec1_old = ValidationRecord(
|
||||
id="rec-old",
|
||||
@@ -26,7 +28,7 @@ async def test_get_health_summary_aggregation():
|
||||
status="FAIL",
|
||||
timestamp=now - timedelta(hours=1),
|
||||
summary="Old failure",
|
||||
issues=[]
|
||||
issues=[],
|
||||
)
|
||||
rec1_new = ValidationRecord(
|
||||
id="rec-new",
|
||||
@@ -35,9 +37,9 @@ async def test_get_health_summary_aggregation():
|
||||
status="PASS",
|
||||
timestamp=now,
|
||||
summary="New pass",
|
||||
issues=[]
|
||||
issues=[],
|
||||
)
|
||||
|
||||
|
||||
# Dashboard 2: Single WARN
|
||||
rec2 = ValidationRecord(
|
||||
id="rec-warn",
|
||||
@@ -46,28 +48,28 @@ async def test_get_health_summary_aggregation():
|
||||
status="WARN",
|
||||
timestamp=now,
|
||||
summary="Warning",
|
||||
issues=[]
|
||||
issues=[],
|
||||
)
|
||||
|
||||
|
||||
# Mock the query chain
|
||||
# subquery = self.db.query(...).filter(...).group_by(...).subquery()
|
||||
# query = self.db.query(ValidationRecord).join(subquery, ...).all()
|
||||
|
||||
|
||||
mock_query = db.query.return_value
|
||||
mock_query.filter.return_value = mock_query
|
||||
mock_query.group_by.return_value = mock_query
|
||||
mock_query.subquery.return_value = MagicMock()
|
||||
|
||||
|
||||
db.query.return_value.join.return_value.all.return_value = [rec1_new, rec2]
|
||||
|
||||
|
||||
service = HealthService(db)
|
||||
summary = await service.get_health_summary(environment_id="env_1")
|
||||
|
||||
|
||||
assert summary.pass_count == 1
|
||||
assert summary.warn_count == 1
|
||||
assert summary.fail_count == 0
|
||||
assert len(summary.items) == 2
|
||||
|
||||
|
||||
# Verify dash_1 has the latest status (PASS)
|
||||
dash_1_item = next(item for item in summary.items if item.dashboard_id == "dash_1")
|
||||
assert dash_1_item.status == "PASS"
|
||||
@@ -75,6 +77,7 @@ async def test_get_health_summary_aggregation():
|
||||
assert dash_1_item.record_id == rec1_new.id
|
||||
assert dash_1_item.dashboard_slug == "dash_1"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_health_summary_empty():
|
||||
"""
|
||||
@@ -82,10 +85,10 @@ async def test_get_health_summary_empty():
|
||||
"""
|
||||
db = MagicMock()
|
||||
db.query.return_value.join.return_value.all.return_value = []
|
||||
|
||||
|
||||
service = HealthService(db)
|
||||
summary = await service.get_health_summary(environment_id="env_none")
|
||||
|
||||
|
||||
assert summary.pass_count == 0
|
||||
assert len(summary.items) == 0
|
||||
|
||||
@@ -159,6 +162,8 @@ async def test_get_health_summary_reuses_dashboard_metadata_cache_across_service
|
||||
HealthService._dashboard_summary_cache.clear()
|
||||
|
||||
|
||||
# [DEF:test_delete_validation_report_deletes_dashboard_scope_and_linked_tasks:Function]
|
||||
# @RELATION: BINDS_TO ->[test_health_service]
|
||||
def test_delete_validation_report_deletes_dashboard_scope_and_linked_tasks():
|
||||
db = MagicMock()
|
||||
config_manager = MagicMock()
|
||||
@@ -222,12 +227,17 @@ def test_delete_validation_report_deletes_dashboard_scope_and_linked_tasks():
|
||||
db.commit.assert_called_once()
|
||||
cleanup_instance.delete_task_with_logs.assert_any_call("task-1")
|
||||
cleanup_instance.delete_task_with_logs.assert_any_call("task-2")
|
||||
cleanup_instance.delete_task_with_logs.call_count == 2
|
||||
assert cleanup_instance.delete_task_with_logs.call_count == 2
|
||||
assert "task-1" not in task_manager.tasks
|
||||
assert "task-2" not in task_manager.tasks
|
||||
assert "task-3" in task_manager.tasks
|
||||
|
||||
|
||||
# [/DEF:test_delete_validation_report_deletes_dashboard_scope_and_linked_tasks:Function]
|
||||
|
||||
|
||||
# [DEF:test_delete_validation_report_returns_false_for_unknown_record:Function]
|
||||
# @RELATION: BINDS_TO ->[test_health_service]
|
||||
def test_delete_validation_report_returns_false_for_unknown_record():
|
||||
db = MagicMock()
|
||||
db.query.return_value.filter.return_value.first.return_value = None
|
||||
@@ -237,6 +247,11 @@ def test_delete_validation_report_returns_false_for_unknown_record():
|
||||
assert service.delete_validation_report("missing") is False
|
||||
|
||||
|
||||
# [/DEF:test_delete_validation_report_returns_false_for_unknown_record:Function]
|
||||
|
||||
|
||||
# [DEF:test_delete_validation_report_swallows_linked_task_cleanup_failure:Function]
|
||||
# @RELATION: BINDS_TO ->[test_health_service]
|
||||
def test_delete_validation_report_swallows_linked_task_cleanup_failure():
|
||||
db = MagicMock()
|
||||
config_manager = MagicMock()
|
||||
@@ -264,11 +279,14 @@ def test_delete_validation_report_swallows_linked_task_cleanup_failure():
|
||||
|
||||
db.query.side_effect = [first_query, peer_query]
|
||||
|
||||
with patch("src.services.health_service.TaskCleanupService") as cleanup_cls, patch(
|
||||
"src.services.health_service.logger"
|
||||
) as mock_logger:
|
||||
with (
|
||||
patch("src.services.health_service.TaskCleanupService") as cleanup_cls,
|
||||
patch("src.services.health_service.logger") as mock_logger,
|
||||
):
|
||||
cleanup_instance = MagicMock()
|
||||
cleanup_instance.delete_task_with_logs.side_effect = RuntimeError("cleanup exploded")
|
||||
cleanup_instance.delete_task_with_logs.side_effect = RuntimeError(
|
||||
"cleanup exploded"
|
||||
)
|
||||
cleanup_cls.return_value = cleanup_instance
|
||||
|
||||
service = HealthService(db, config_manager=config_manager)
|
||||
@@ -282,4 +300,5 @@ def test_delete_validation_report_swallows_linked_task_cleanup_failure():
|
||||
assert "task-1" not in task_manager.tasks
|
||||
|
||||
|
||||
# [/DEF:test_delete_validation_report_swallows_linked_task_cleanup_failure:Function]
|
||||
# [/DEF:test_health_service:Module]
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
# [DEF:backend.src.services.__tests__.test_llm_plugin_persistence:Module]
|
||||
# [DEF:test_llm_plugin_persistence:Module]
|
||||
# @RELATION: VERIFIES ->[src.plugins.llm_analysis.plugin.DashboardValidationPlugin]
|
||||
# @COMPLEXITY: 3
|
||||
# @PURPOSE: Regression test for ValidationRecord persistence fields populated from task context.
|
||||
|
||||
@@ -9,6 +10,7 @@ from src.plugins.llm_analysis import plugin as plugin_module
|
||||
|
||||
|
||||
# [DEF:_DummyLogger:Class]
|
||||
# @RELATION: BINDS_TO ->[test_llm_plugin_persistence]
|
||||
# @PURPOSE: Minimal logger shim for TaskContext-like objects used in tests.
|
||||
class _DummyLogger:
|
||||
def with_source(self, _source: str):
|
||||
@@ -25,10 +27,13 @@ class _DummyLogger:
|
||||
|
||||
def error(self, *_args, **_kwargs):
|
||||
return None
|
||||
|
||||
|
||||
# [/DEF:_DummyLogger:Class]
|
||||
|
||||
|
||||
# [DEF:_FakeDBSession:Class]
|
||||
# @RELATION: BINDS_TO ->[test_llm_plugin_persistence]
|
||||
# @PURPOSE: Captures persisted records for assertion and mimics SQLAlchemy session methods used by plugin.
|
||||
class _FakeDBSession:
|
||||
def __init__(self):
|
||||
@@ -44,13 +49,18 @@ class _FakeDBSession:
|
||||
|
||||
def close(self):
|
||||
self.closed = True
|
||||
|
||||
|
||||
# [/DEF:_FakeDBSession:Class]
|
||||
|
||||
|
||||
# [DEF:test_dashboard_validation_plugin_persists_task_and_environment_ids:Function]
|
||||
# @RELATION: BINDS_TO ->[test_llm_plugin_persistence]
|
||||
# @PURPOSE: Ensure db ValidationRecord includes context.task_id and params.environment_id.
|
||||
@pytest.mark.asyncio
|
||||
async def test_dashboard_validation_plugin_persists_task_and_environment_ids(tmp_path, monkeypatch):
|
||||
async def test_dashboard_validation_plugin_persists_task_and_environment_ids(
|
||||
tmp_path, monkeypatch
|
||||
):
|
||||
fake_db = _FakeDBSession()
|
||||
|
||||
env = types.SimpleNamespace(id="env-42")
|
||||
@@ -112,7 +122,9 @@ async def test_dashboard_validation_plugin_persists_task_and_environment_ids(tmp
|
||||
|
||||
class _FakeSupersetClient:
|
||||
def __init__(self, _env):
|
||||
self.network = types.SimpleNamespace(request=lambda **_kwargs: {"result": []})
|
||||
self.network = types.SimpleNamespace(
|
||||
request=lambda **_kwargs: {"result": []}
|
||||
)
|
||||
|
||||
monkeypatch.setattr(plugin_module, "SessionLocal", lambda: fake_db)
|
||||
monkeypatch.setattr(plugin_module, "LLMProviderService", _FakeProviderService)
|
||||
@@ -120,7 +132,9 @@ async def test_dashboard_validation_plugin_persists_task_and_environment_ids(tmp
|
||||
monkeypatch.setattr(plugin_module, "LLMClient", _FakeLLMClient)
|
||||
monkeypatch.setattr(plugin_module, "NotificationService", _FakeNotificationService)
|
||||
monkeypatch.setattr(plugin_module, "SupersetClient", _FakeSupersetClient)
|
||||
monkeypatch.setattr("src.dependencies.get_config_manager", lambda: _FakeConfigManager())
|
||||
monkeypatch.setattr(
|
||||
"src.dependencies.get_config_manager", lambda: _FakeConfigManager()
|
||||
)
|
||||
|
||||
context = types.SimpleNamespace(
|
||||
task_id="task-999",
|
||||
@@ -144,7 +158,9 @@ async def test_dashboard_validation_plugin_persists_task_and_environment_ids(tmp
|
||||
assert fake_db.added is not None
|
||||
assert fake_db.added.task_id == "task-999"
|
||||
assert fake_db.added.environment_id == "env-42"
|
||||
|
||||
|
||||
# [/DEF:test_dashboard_validation_plugin_persists_task_and_environment_ids:Function]
|
||||
|
||||
|
||||
# [/DEF:backend.src.services.__tests__.test_llm_plugin_persistence:Module]
|
||||
# [/DEF:test_llm_plugin_persistence:Module]
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
# [DEF:backend.src.services.__tests__.test_resource_service:Module]
|
||||
# [DEF:TestResourceService:Module]
|
||||
# @COMPLEXITY: 3
|
||||
# @SEMANTICS: resource-service, tests, dashboards, datasets, activity
|
||||
# @PURPOSE: Unit tests for ResourceService
|
||||
# @LAYER: Service
|
||||
# @RELATION: TESTS -> backend.src.services.resource_service
|
||||
# @RELATION: VERIFIES -> ResourceService
|
||||
# @RELATION: VERIFIES ->[src.services.resource_service.ResourceService]
|
||||
# @INVARIANT: Resource summaries preserve task linkage and status projection behavior.
|
||||
|
||||
import pytest
|
||||
@@ -13,25 +12,27 @@ from datetime import datetime, timezone
|
||||
|
||||
|
||||
# [DEF:test_get_dashboards_with_status:Function]
|
||||
# @RELATION: BINDS_TO ->[TestResourceService]
|
||||
# @PURPOSE: Validate dashboard enrichment includes git/task status projections.
|
||||
# @TEST: get_dashboards_with_status returns dashboards with git and task status
|
||||
# @PRE: SupersetClient returns dashboard list
|
||||
# @POST: Each dashboard has git_status and last_task fields
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_dashboards_with_status():
|
||||
with patch("src.services.resource_service.SupersetClient") as mock_client, \
|
||||
patch("src.services.resource_service.GitService"):
|
||||
|
||||
with (
|
||||
patch("src.services.resource_service.SupersetClient") as mock_client,
|
||||
patch("src.services.resource_service.GitService"),
|
||||
):
|
||||
from src.services.resource_service import ResourceService
|
||||
|
||||
|
||||
service = ResourceService()
|
||||
|
||||
|
||||
# Mock Superset response
|
||||
mock_client.return_value.get_dashboards_summary.return_value = [
|
||||
{"id": 1, "title": "Dashboard 1", "slug": "dash-1"},
|
||||
{"id": 2, "title": "Dashboard 2", "slug": "dash-2"}
|
||||
{"id": 2, "title": "Dashboard 2", "slug": "dash-2"},
|
||||
]
|
||||
|
||||
|
||||
# Mock tasks
|
||||
task_prod_old = MagicMock()
|
||||
task_prod_old.id = "task-123"
|
||||
@@ -62,7 +63,7 @@ async def test_get_dashboards_with_status():
|
||||
env,
|
||||
[task_prod_old, task_prod_new, task_other_env],
|
||||
)
|
||||
|
||||
|
||||
assert len(result) == 2
|
||||
assert result[0]["id"] == 1
|
||||
assert "git_status" in result[0]
|
||||
@@ -76,35 +77,35 @@ async def test_get_dashboards_with_status():
|
||||
|
||||
|
||||
# [DEF:test_get_datasets_with_status:Function]
|
||||
# @RELATION: BINDS_TO ->[TestResourceService]
|
||||
# @TEST: get_datasets_with_status returns datasets with task status
|
||||
# @PRE: SupersetClient returns dataset list
|
||||
# @POST: Each dataset has last_task field
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_datasets_with_status():
|
||||
with patch("src.services.resource_service.SupersetClient") as mock_client:
|
||||
|
||||
from src.services.resource_service import ResourceService
|
||||
|
||||
|
||||
service = ResourceService()
|
||||
|
||||
|
||||
# Mock Superset response
|
||||
mock_client.return_value.get_datasets_summary.return_value = [
|
||||
{"id": 1, "table_name": "users", "schema": "public", "database": "app"},
|
||||
{"id": 2, "table_name": "orders", "schema": "public", "database": "app"}
|
||||
{"id": 2, "table_name": "orders", "schema": "public", "database": "app"},
|
||||
]
|
||||
|
||||
|
||||
# Mock tasks
|
||||
mock_task = MagicMock()
|
||||
mock_task.id = "task-456"
|
||||
mock_task.status = "RUNNING"
|
||||
mock_task.params = {"resource_id": "dataset-1"}
|
||||
mock_task.created_at = datetime.now()
|
||||
|
||||
|
||||
env = MagicMock()
|
||||
env.id = "prod"
|
||||
|
||||
|
||||
result = await service.get_datasets_with_status(env, [mock_task])
|
||||
|
||||
|
||||
assert len(result) == 2
|
||||
assert result[0]["table_name"] == "users"
|
||||
assert "last_task" in result[0]
|
||||
@@ -116,35 +117,36 @@ async def test_get_datasets_with_status():
|
||||
|
||||
|
||||
# [DEF:test_get_activity_summary:Function]
|
||||
# @RELATION: BINDS_TO ->[TestResourceService]
|
||||
# @TEST: get_activity_summary returns active count and recent tasks
|
||||
# @PRE: tasks list provided
|
||||
# @POST: Returns dict with active_count and recent_tasks
|
||||
def test_get_activity_summary():
|
||||
from src.services.resource_service import ResourceService
|
||||
|
||||
|
||||
service = ResourceService()
|
||||
|
||||
|
||||
# Create mock tasks
|
||||
task1 = MagicMock()
|
||||
task1.id = "task-1"
|
||||
task1.status = "RUNNING"
|
||||
task1.params = {"resource_name": "Dashboard 1", "resource_type": "dashboard"}
|
||||
task1.created_at = datetime(2024, 1, 1, 10, 0, 0)
|
||||
|
||||
|
||||
task2 = MagicMock()
|
||||
task2.id = "task-2"
|
||||
task2.status = "SUCCESS"
|
||||
task2.params = {"resource_name": "Dataset 1", "resource_type": "dataset"}
|
||||
task2.created_at = datetime(2024, 1, 1, 9, 0, 0)
|
||||
|
||||
|
||||
task3 = MagicMock()
|
||||
task3.id = "task-3"
|
||||
task3.status = "WAITING_INPUT"
|
||||
task3.params = {"resource_name": "Dashboard 2", "resource_type": "dashboard"}
|
||||
task3.created_at = datetime(2024, 1, 1, 8, 0, 0)
|
||||
|
||||
|
||||
result = service.get_activity_summary([task1, task2, task3])
|
||||
|
||||
|
||||
assert result["active_count"] == 2 # RUNNING + WAITING_INPUT
|
||||
assert len(result["recent_tasks"]) == 3
|
||||
|
||||
@@ -153,51 +155,52 @@ def test_get_activity_summary():
|
||||
|
||||
|
||||
# [DEF:test_get_git_status_for_dashboard_no_repo:Function]
|
||||
# @RELATION: BINDS_TO ->[TestResourceService]
|
||||
# @TEST: _get_git_status_for_dashboard returns None when no repo exists
|
||||
# @PRE: GitService returns None for repo
|
||||
# @POST: Returns None
|
||||
def test_get_git_status_for_dashboard_no_repo():
|
||||
with patch("src.services.resource_service.GitService") as mock_git:
|
||||
|
||||
from src.services.resource_service import ResourceService
|
||||
|
||||
|
||||
service = ResourceService()
|
||||
mock_git.return_value.get_repo.return_value = None
|
||||
|
||||
|
||||
result = service._get_git_status_for_dashboard(123)
|
||||
|
||||
|
||||
assert result is not None
|
||||
assert result['sync_status'] == 'NO_REPO'
|
||||
assert result['has_repo'] is False
|
||||
assert result["sync_status"] == "NO_REPO"
|
||||
assert result["has_repo"] is False
|
||||
|
||||
|
||||
# [/DEF:test_get_git_status_for_dashboard_no_repo:Function]
|
||||
|
||||
|
||||
# [DEF:test_get_last_task_for_resource:Function]
|
||||
# @RELATION: BINDS_TO ->[TestResourceService]
|
||||
# @TEST: _get_last_task_for_resource returns most recent task for resource
|
||||
# @PRE: tasks list with matching resource_id
|
||||
# @POST: Returns task summary with task_id and status
|
||||
def test_get_last_task_for_resource():
|
||||
from src.services.resource_service import ResourceService
|
||||
|
||||
|
||||
service = ResourceService()
|
||||
|
||||
|
||||
# Create mock tasks
|
||||
task1 = MagicMock()
|
||||
task1.id = "task-old"
|
||||
task1.status = "SUCCESS"
|
||||
task1.params = {"resource_id": "dashboard-1"}
|
||||
task1.created_at = datetime(2024, 1, 1, 10, 0, 0)
|
||||
|
||||
|
||||
task2 = MagicMock()
|
||||
task2.id = "task-new"
|
||||
task2.status = "RUNNING"
|
||||
task2.params = {"resource_id": "dashboard-1"}
|
||||
task2.created_at = datetime(2024, 1, 1, 12, 0, 0)
|
||||
|
||||
|
||||
result = service._get_last_task_for_resource("dashboard-1", [task1, task2])
|
||||
|
||||
|
||||
assert result is not None
|
||||
assert result["task_id"] == "task-new" # Most recent
|
||||
assert result["status"] == "RUNNING"
|
||||
@@ -207,27 +210,28 @@ def test_get_last_task_for_resource():
|
||||
|
||||
|
||||
# [DEF:test_extract_resource_name_from_task:Function]
|
||||
# @RELATION: BINDS_TO ->[TestResourceService]
|
||||
# @TEST: _extract_resource_name_from_task extracts name from params
|
||||
# @PRE: task has resource_name in params
|
||||
# @POST: Returns resource name or fallback
|
||||
def test_extract_resource_name_from_task():
|
||||
from src.services.resource_service import ResourceService
|
||||
|
||||
|
||||
service = ResourceService()
|
||||
|
||||
|
||||
# Task with resource_name
|
||||
task = MagicMock()
|
||||
task.id = "task-123"
|
||||
task.params = {"resource_name": "My Dashboard"}
|
||||
|
||||
|
||||
result = service._extract_resource_name_from_task(task)
|
||||
assert result == "My Dashboard"
|
||||
|
||||
|
||||
# Task without resource_name
|
||||
task2 = MagicMock()
|
||||
task2.id = "task-456"
|
||||
task2.params = {}
|
||||
|
||||
|
||||
result2 = service._extract_resource_name_from_task(task2)
|
||||
assert "task-456" in result2
|
||||
|
||||
@@ -236,48 +240,56 @@ def test_extract_resource_name_from_task():
|
||||
|
||||
|
||||
# [DEF:test_get_last_task_for_resource_empty_tasks:Function]
|
||||
# @RELATION: BINDS_TO ->[TestResourceService]
|
||||
# @TEST: _get_last_task_for_resource returns None for empty tasks list
|
||||
# @PRE: tasks is empty list
|
||||
# @POST: Returns None
|
||||
def test_get_last_task_for_resource_empty_tasks():
|
||||
from src.services.resource_service import ResourceService
|
||||
|
||||
|
||||
service = ResourceService()
|
||||
|
||||
|
||||
result = service._get_last_task_for_resource("dashboard-1", [])
|
||||
assert result is None
|
||||
|
||||
|
||||
# [/DEF:test_get_last_task_for_resource_empty_tasks:Function]
|
||||
|
||||
|
||||
# [DEF:test_get_last_task_for_resource_no_match:Function]
|
||||
# @RELATION: BINDS_TO ->[TestResourceService]
|
||||
# @TEST: _get_last_task_for_resource returns None when no tasks match resource_id
|
||||
# @PRE: tasks list has no matching resource_id
|
||||
# @POST: Returns None
|
||||
def test_get_last_task_for_resource_no_match():
|
||||
from src.services.resource_service import ResourceService
|
||||
|
||||
|
||||
service = ResourceService()
|
||||
|
||||
|
||||
task = MagicMock()
|
||||
task.id = "task-999"
|
||||
task.status = "SUCCESS"
|
||||
task.params = {"resource_id": "dashboard-99"}
|
||||
task.created_at = datetime(2024, 1, 1, 10, 0, 0)
|
||||
|
||||
|
||||
result = service._get_last_task_for_resource("dashboard-1", [task])
|
||||
assert result is None
|
||||
|
||||
|
||||
# [/DEF:test_get_last_task_for_resource_no_match:Function]
|
||||
|
||||
|
||||
# [DEF:test_get_dashboards_with_status_handles_mixed_naive_and_aware_task_datetimes:Function]
|
||||
# @RELATION: BINDS_TO ->[TestResourceService]
|
||||
# @TEST: get_dashboards_with_status handles mixed naive/aware datetimes without comparison errors.
|
||||
# @PRE: Task list includes both timezone-aware and timezone-naive timestamps.
|
||||
# @POST: Latest task is selected deterministically and no exception is raised.
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_dashboards_with_status_handles_mixed_naive_and_aware_task_datetimes():
|
||||
with patch("src.services.resource_service.SupersetClient") as mock_client, \
|
||||
patch("src.services.resource_service.GitService"):
|
||||
|
||||
with (
|
||||
patch("src.services.resource_service.SupersetClient") as mock_client,
|
||||
patch("src.services.resource_service.GitService"),
|
||||
):
|
||||
from src.services.resource_service import ResourceService
|
||||
|
||||
service = ResourceService()
|
||||
@@ -305,18 +317,22 @@ async def test_get_dashboards_with_status_handles_mixed_naive_and_aware_task_dat
|
||||
result = await service.get_dashboards_with_status(env, [task_naive, task_aware])
|
||||
|
||||
assert result[0]["last_task"]["task_id"] == "task-aware"
|
||||
|
||||
|
||||
# [/DEF:test_get_dashboards_with_status_handles_mixed_naive_and_aware_task_datetimes:Function]
|
||||
|
||||
|
||||
# [DEF:test_get_dashboards_with_status_prefers_latest_decisive_validation_status_over_newer_unknown:Function]
|
||||
# @RELATION: BINDS_TO ->[TestResourceService]
|
||||
# @TEST: get_dashboards_with_status keeps latest task identity while falling back to older decisive validation status.
|
||||
# @PRE: Same dashboard has older WARN and newer UNKNOWN validation tasks.
|
||||
# @POST: Returned last_task points to newest task but preserves WARN as last meaningful validation state.
|
||||
@pytest.mark.anyio
|
||||
async def test_get_dashboards_with_status_prefers_latest_decisive_validation_status_over_newer_unknown():
|
||||
with patch("src.services.resource_service.SupersetClient") as mock_client, \
|
||||
patch("src.services.resource_service.GitService"):
|
||||
|
||||
with (
|
||||
patch("src.services.resource_service.SupersetClient") as mock_client,
|
||||
patch("src.services.resource_service.GitService"),
|
||||
):
|
||||
from src.services.resource_service import ResourceService
|
||||
|
||||
service = ResourceService()
|
||||
@@ -343,23 +359,29 @@ async def test_get_dashboards_with_status_prefers_latest_decisive_validation_sta
|
||||
env = MagicMock()
|
||||
env.id = "prod"
|
||||
|
||||
result = await service.get_dashboards_with_status(env, [task_warn, task_unknown])
|
||||
result = await service.get_dashboards_with_status(
|
||||
env, [task_warn, task_unknown]
|
||||
)
|
||||
|
||||
assert result[0]["last_task"]["task_id"] == "task-unknown"
|
||||
assert result[0]["last_task"]["status"] == "RUNNING"
|
||||
assert result[0]["last_task"]["validation_status"] == "WARN"
|
||||
|
||||
|
||||
# [/DEF:test_get_dashboards_with_status_prefers_latest_decisive_validation_status_over_newer_unknown:Function]
|
||||
|
||||
|
||||
# [DEF:test_get_dashboards_with_status_falls_back_to_latest_unknown_without_decisive_history:Function]
|
||||
# @RELATION: BINDS_TO ->[TestResourceService]
|
||||
# @TEST: get_dashboards_with_status still returns newest UNKNOWN when no decisive validation exists.
|
||||
# @PRE: Same dashboard has only UNKNOWN validation tasks.
|
||||
# @POST: Returned last_task keeps newest UNKNOWN task.
|
||||
@pytest.mark.anyio
|
||||
async def test_get_dashboards_with_status_falls_back_to_latest_unknown_without_decisive_history():
|
||||
with patch("src.services.resource_service.SupersetClient") as mock_client, \
|
||||
patch("src.services.resource_service.GitService"):
|
||||
|
||||
with (
|
||||
patch("src.services.resource_service.SupersetClient") as mock_client,
|
||||
patch("src.services.resource_service.GitService"),
|
||||
):
|
||||
from src.services.resource_service import ResourceService
|
||||
|
||||
service = ResourceService()
|
||||
@@ -386,14 +408,19 @@ async def test_get_dashboards_with_status_falls_back_to_latest_unknown_without_d
|
||||
env = MagicMock()
|
||||
env.id = "prod"
|
||||
|
||||
result = await service.get_dashboards_with_status(env, [task_unknown_old, task_unknown_new])
|
||||
result = await service.get_dashboards_with_status(
|
||||
env, [task_unknown_old, task_unknown_new]
|
||||
)
|
||||
|
||||
assert result[0]["last_task"]["task_id"] == "task-unknown-new"
|
||||
assert result[0]["last_task"]["validation_status"] == "UNKNOWN"
|
||||
|
||||
|
||||
# [/DEF:test_get_dashboards_with_status_falls_back_to_latest_unknown_without_decisive_history:Function]
|
||||
|
||||
|
||||
# [DEF:test_get_last_task_for_resource_handles_mixed_naive_and_aware_created_at:Function]
|
||||
# @RELATION: BINDS_TO ->[TestResourceService]
|
||||
# @TEST: _get_last_task_for_resource handles mixed naive/aware created_at values.
|
||||
# @PRE: Matching tasks include naive and aware created_at timestamps.
|
||||
# @POST: Latest task is returned without raising datetime comparison errors.
|
||||
@@ -414,11 +441,15 @@ def test_get_last_task_for_resource_handles_mixed_naive_and_aware_created_at():
|
||||
task_aware.params = {"resource_id": "dashboard-1"}
|
||||
task_aware.created_at = datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc)
|
||||
|
||||
result = service._get_last_task_for_resource("dashboard-1", [task_naive, task_aware])
|
||||
result = service._get_last_task_for_resource(
|
||||
"dashboard-1", [task_naive, task_aware]
|
||||
)
|
||||
|
||||
assert result is not None
|
||||
assert result["task_id"] == "task-new"
|
||||
|
||||
|
||||
# [/DEF:test_get_last_task_for_resource_handles_mixed_naive_and_aware_created_at:Function]
|
||||
|
||||
|
||||
# [/DEF:backend.src.services.__tests__.test_resource_service:Module]
|
||||
# [/DEF:TestResourceService:Module]
|
||||
|
||||
Reference in New Issue
Block a user