Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix paths in test_check_query.py Windows compatible #198

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 34 additions & 22 deletions tests/wikidata/test_check_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from pathlib import Path
from unittest.mock import MagicMock, mock_open, patch
from urllib.error import HTTPError

import pytest
from scribe_data.wikidata.check_query.check import (
all_queries,
Expand All @@ -41,16 +40,22 @@
from scribe_data.wikidata.check_query.query import QueryExecutionException, QueryFile
from scribe_data.wikidata.check_query.sparql import execute


def normalize_path(path):
return str(Path(path))


S_PATH = "/root/project/src/dir/query.sparql"
A_PATH = Path(S_PATH)
S_PATH = normalize_path(S_PATH)
A_PATH = Path(normalize_path(S_PATH))


@pytest.fixture
def a_query():
return QueryFile(A_PATH)


# Query
# MARK: Query
def test_full_path(a_query):
assert a_query.path == A_PATH

Expand All @@ -65,27 +70,33 @@ def test_query_equals(a_query):


def test_query_not_equals(a_query):
assert a_query != QueryFile("/root/project/src/Dir/query.sparql")
assert a_query != QueryFile(normalize_path("/root/project/src/Dir/query.sparql"))


def test_query_not_equals_object(a_query):
assert a_query != object()


def test_query_str(a_query):
assert str(a_query) == "QueryFile(path=/root/project/src/dir/query.sparql)"
assert (
str(a_query)
== f"QueryFile(path={normalize_path('/root/project/src/dir/query.sparql')})"
)


def test_query_repr(a_query):
assert repr(a_query) == "QueryFile(path=/root/project/src/dir/query.sparql)"
assert (
repr(a_query)
== f"QueryFile(path={normalize_path('/root/project/src/dir/query.sparql')})"
)


def test_query_execution_exception(a_query):
exception = QueryExecutionException("failure", a_query)
assert str(exception) == f"{S_PATH} : failure"


# ping
# MARK: ping
@patch("urllib.request.urlopen")
def test_ping_pass(mock_urlopen):
mock_urlopen.return_value.__enter__.return_value.getcode.return_value = (
Expand Down Expand Up @@ -117,7 +128,7 @@ def test_ping_fail(mock_urlopen):
assert not ping("http://www.python.org", 0)


# check_sparql_file
# MARK: check_sparql_file
@patch.object(Path, "is_file", return_value=True)
def test_check_sparql_file_exists(_):
assert check_sparql_file(S_PATH) == A_PATH
Expand All @@ -140,7 +151,7 @@ def test_check_sparql_file_not_sparql_extension(_):
assert str(err.value) == f"{fpath} does not have a '.sparql' extension"


# changed_queries
# MARK: changed_queries
@pytest.mark.parametrize(
"git_status, expected",
[
Expand All @@ -165,6 +176,7 @@ def test_changed_queries(mock_run, git_status, expected):
mock_result.configure_mock(**{"returncode": 0, "stdout": git_status})

mock_run.return_value = mock_result
expected = [QueryFile(Path(p.path).resolve()) for p in expected]
assert changed_queries() == expected


Expand All @@ -180,7 +192,7 @@ def test_changed_queries_failure(mock_run, capsys):
assert "ERROR: no git" == err_out.strip()


# all_queries
# MARK: all_queries
@pytest.mark.parametrize(
"tree, expected",
[
Expand All @@ -197,8 +209,8 @@ def test_changed_queries_failure(mock_run, capsys):
("/root/src", (), ("sparql.pdf", "b.sparql")),
],
[
QueryFile(Path("/root/a.sparql")),
QueryFile(Path("/root/src/b.sparql")),
QueryFile(Path(normalize_path("/root/a.sparql"))),
QueryFile(Path(normalize_path("/root/src/b.sparql"))),
],
),
],
Expand All @@ -209,7 +221,7 @@ def test_all_queries(tree, expected):
assert all_queries() == expected


# execute
# MARK: execute


def test_execute(a_query):
Expand All @@ -219,7 +231,7 @@ def test_execute(a_query):
assert str(err) == f"{a_query.path} : Failed too many times."


# check_limit
# MARK: check_limit
@pytest.mark.parametrize(
"candidate, limit",
[
Expand Down Expand Up @@ -248,7 +260,7 @@ def test_check_limit_neg(candidate):
assert str(err.value) == "LIMIT must be an integer of value 1 or greater."


# check_timeout
# MARK: check_timeout
@pytest.mark.parametrize(
"candidate, timeout",
[
Expand Down Expand Up @@ -277,7 +289,7 @@ def test_check_timeout_neg(candidate):
assert str(err.value) == "timeout must be an integer of value 1 or greater."


# main
# MARK: main


@pytest.mark.parametrize("arg", ["-h", "--help"])
Expand Down Expand Up @@ -314,7 +326,7 @@ def test_error_report_single(a_query, capsys):

assert (
err_out == "\nFollowing query failed:\n\n"
"/root/project/src/dir/query.sparql : timeout\n"
f"{normalize_path('/root/project/src/dir/query.sparql')} : timeout\n"
)


Expand All @@ -328,8 +340,8 @@ def test_error_report_multiple(a_query, capsys):

assert (
err_out == "\nFollowing queries failed:\n\n"
"/root/project/src/dir/query.sparql : timeout\n"
"/root/project/src/dir/query.sparql : bad format\n"
f"{normalize_path('/root/project/src/dir/query.sparql')} : timeout\n"
f"{normalize_path('/root/project/src/dir/query.sparql')} : bad format\n"
)


Expand All @@ -346,7 +358,7 @@ def test_success_report_single_display_set(a_query, capsys):

assert (
out == "\nFollowing query ran successfully:\n\n"
"/root/project/src/dir/query.sparql returned: {'a': 23}\n"
f"{normalize_path('/root/project/src/dir/query.sparql')} returned: {{'a': 23}}\n"
)


Expand Down Expand Up @@ -375,6 +387,6 @@ def test_success_report_multiple_display_set(a_query, capsys):

assert (
out == "\nFollowing queries ran successfully:\n\n"
"/root/project/src/dir/query.sparql returned: {'a': 23}\n"
"/root/project/src/dir/query.sparql returned: {'b': 57}\n"
f"{normalize_path('/root/project/src/dir/query.sparql')} returned: {{'a': 23}}\n"
f"{normalize_path('/root/project/src/dir/query.sparql')} returned: {{'b': 57}}\n"
)
Loading