Skip to content

Commit

Permalink
Some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
readicculus committed May 3, 2024
1 parent b83a006 commit a455ea2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
[project]
# See https://setuptools.pypa.io/en/latest/userguide/quickstart.html for more project configuration options.
name = "pysh-client"
version = "0.1"
version = "0.1.0"
readme = "README.md"
description = "A phish.net API wrapper in Python."
classifiers = [
Expand Down
6 changes: 3 additions & 3 deletions src/pysh/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ def _validate_request_schema(
if column.lower() not in attributes:
raise ValueError(
f"{column.lower()} is not a valid filter column."
f"Api Method {method.value} has the following attributes: {attributes}"
f" Api Method {method.value} has the following attributes: {attributes}"
)
if parameters and parameters.params["order_by"]:
if parameters.params["order_by"].lower() not in attributes:
raise ValueError(
f"{column.lower()} is not a valid order_by attribute."
f"Api Method {method.value} has the following attributes: {attributes}"
f"{column.lower()} is not a valid order_by attribute. "
f" Api Method {method.value} has the following attributes: {attributes}"
)


Expand Down
61 changes: 58 additions & 3 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,72 @@
import os
import unittest

from pysh import Client
from pysh import Client, Parameters
from pysh.api.client import Endpoint
from pysh.api.schema import ApiMethod

TEST_API_KEY = "<KEY>"
TEST_ATTRIBUTE = "attribute"
TEST_API_URL = "https://dummyapi.net"
TEST_API_VERSION = "v1"


class TestClient(unittest.TestCase):
def test_initialize_client_with_apikey(self):
def test_initialize_client_with_apikey_succeeds(self):
client = Client(TEST_API_KEY)
self.assertEqual(client._api_key, TEST_API_KEY)

def test_initialize_client_with_environ(self):
def test_initialize_client_with_environ_succeeds(self):
os.environ["PHISH_API_KEY"] = TEST_API_KEY
client = Client()
self.assertEqual(client._api_key, TEST_API_KEY)

def test_parameters_only_returns_noneNone_parameters_succeeds(self):
p = Parameters(order_by=TEST_ATTRIBUTE)
r = p.get_params()
self.assertEqual(r, {"order_by": TEST_ATTRIBUTE})

def test_parameters_returns_all_parameters_succeeds(self):
p = Parameters(
order_by=TEST_ATTRIBUTE,
direction="asc",
limit="1",
no_header="true",
callback="callback",
)
r = p.get_params()
self.assertEqual(
r,
{
"order_by": TEST_ATTRIBUTE,
"direction": "asc",
"limit": "1",
"no_header": "true",
"callback": "callback",
},
)

def test_endpoint_format_defaults_to_json_succeeds(self):
e = Endpoint(TEST_API_URL, TEST_API_VERSION)
r = e.build_endpoint(ApiMethod.SONGS)
expected = f"{TEST_API_URL}/{TEST_API_VERSION}/songs.json"
self.assertEqual(r, expected)

def test_endpoint_validation_requires_value_if_column_fails(self):
e = Endpoint(TEST_API_URL, TEST_API_VERSION)
with self.assertRaises(ValueError):
e.build_endpoint(ApiMethod.SONGS, column="test")

def test_endpoint_validation_requires_column_if_value_fails(self):
e = Endpoint(TEST_API_URL, TEST_API_VERSION)
with self.assertRaises(ValueError):
e.build_endpoint(ApiMethod.SONGS, value="test")

def test_endpoint_column_does_not_exist_fails(self):
e = Endpoint(TEST_API_URL, TEST_API_VERSION)
with self.assertRaises(ValueError) as context:
e.build_endpoint(ApiMethod.SONGS, column="badcol", value="test")
self.assertTrue(
"badcol is not a valid filter column. Api Method songs has the following attributes"
in str(context.exception)
)

0 comments on commit a455ea2

Please sign in to comment.