Skip to content

Commit

Permalink
🎨 Format test code too
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Oct 20, 2021
1 parent 00b92ca commit e85a30f
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 160 deletions.
9 changes: 6 additions & 3 deletions tests/test_account_identified.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ def test_user_identified():
user2 = AccountIdentified.by_account_id("account_id")
user3 = AccountIdentified.by_domain("domain")

assert (user1.format_identification() == {"accountId": "account_id", "domain": "domain"})
assert (user2.format_identification() == {"accountId": "account_id"})
assert (user3.format_identification() == {"domain": "domain"})
assert user1.format_identification() == {
"accountId": "account_id",
"domain": "domain",
}
assert user2.format_identification() == {"accountId": "account_id"}
assert user3.format_identification() == {"domain": "domain"}

with pytest.raises(JournyException):
AccountIdentified("", "")
Expand Down
254 changes: 154 additions & 100 deletions tests/test_client.py

Large diffs are not rendered by default.

38 changes: 26 additions & 12 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@

def test_metadata():
metadata = Metadata()
assert (metadata["doesnotexist"] is None)
assert metadata["doesnotexist"] is None
metadata["doesexist"] = "hallo"
assert (metadata["doesexist"] == "hallo")
assert metadata["doesexist"] == "hallo"
metadata["doesexisttoo"] = 2
assert (metadata["doesexisttoo"] == 2)
assert metadata["doesexisttoo"] == 2
metadata["thistoo"] = True
assert (metadata["thistoo"])
assert metadata["thistoo"]
with pytest.raises(JournyException):
metadata[2] = "hallo"
with pytest.raises(JournyException):
metadata["doesexist"] = [1]
metadata2 = Metadata()
metadata2["new"] = "value"
metadata.union(metadata2)
assert (metadata2["new"] == "value")
assert (metadata["doesexist"] == "hallo")
assert (metadata["thistoo"])
assert metadata2["new"] == "value"
assert metadata["doesexist"] == "hallo"
assert metadata["thistoo"]


def test_event():
Expand All @@ -43,14 +43,28 @@ def test_event():
event_1 = Event.for_user("login", user).happened_at(dt)
event_2 = Event.for_user("logout", user).happened_at(dt2)
event_3 = Event.for_account("login", account).happened_at(dt)
event_4 = Event.for_user_in_account("login", user, account).happened_at(dt2).with_metadata(metadata)
event_4 = (
Event.for_user_in_account("login", user, account)
.happened_at(dt2)
.with_metadata(metadata)
)

assert (event_1.__str__() == "Event(login, UserIdentified(user_id, email), None, 2020-11-02 13:37:40, {})")
assert (event_2.__str__() == "Event(logout, UserIdentified(user_id, email), None, 2020-11-02 13:37:50, {})")
assert (
event_3.__str__() == "Event(login, None, AccountIdentified(account_id, www.account.be), 2020-11-02 13:37:40, {})")
event_1.__str__()
== "Event(login, UserIdentified(user_id, email), None, 2020-11-02 13:37:40, {})"
)
assert (
event_4.__str__() == 'Event(login, UserIdentified(user_id, email), AccountIdentified(account_id, www.account.be), 2020-11-02 13:37:50, {"true": true, "key": "value"})')
event_2.__str__()
== "Event(logout, UserIdentified(user_id, email), None, 2020-11-02 13:37:50, {})"
)
assert (
event_3.__str__()
== "Event(login, None, AccountIdentified(account_id, www.account.be), 2020-11-02 13:37:40, {})"
)
assert (
event_4.__str__()
== 'Event(login, UserIdentified(user_id, email), AccountIdentified(account_id, www.account.be), 2020-11-02 13:37:50, {"true": true, "key": "value"})'
)

with pytest.raises(JournyException):
Event(None, None, None, None, Metadata())
Expand Down
43 changes: 26 additions & 17 deletions tests/test_httpclient.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
import pytest

from journyio.httpclient import HttpHeaders, HttpRequest, HttpResponse, HttpClientRequests, Method, HttpClientTesting
from journyio.httpclient import (
HttpHeaders,
HttpRequest,
HttpResponse,
HttpClientRequests,
Method,
HttpClientTesting,
)
from journyio.utils import JournyException


def test_http_headers():
headers = HttpHeaders()
assert (headers["doesnotexist"] is None)
assert headers["doesnotexist"] is None
headers["doesexist"] = "hallo"
assert (headers["doesexist"] == "hallo")
assert headers["doesexist"] == "hallo"
headers["thistoo"] = ["a", "b"]
assert (headers["thistoo"])
assert headers["thistoo"]
with pytest.raises(JournyException):
headers[2] = "hallo"
with pytest.raises(JournyException):
headers["doesexist"] = [2, False]
headers2 = HttpHeaders()
headers2["new"] = "value"
headers.union(headers2)
assert (headers["new"] == "value")
assert (headers["doesexist"] == "hallo")
assert (headers["thistoo"] == ["a", "b"])
assert headers["new"] == "value"
assert headers["doesexist"] == "hallo"
assert headers["thistoo"] == ["a", "b"]


def test_http_request():
request = HttpRequest("https://journy.io", Method.GET, None, None)

assert (request.url == "https://journy.io")
assert (request.method is Method.GET)
assert (request.headers == HttpHeaders())
assert request.url == "https://journy.io"
assert request.method is Method.GET
assert request.headers == HttpHeaders()

assert (request.__str__() == "HttpRequest(https://journy.io, Method.GET, {}, None)")
assert request.__str__() == "HttpRequest(https://journy.io, Method.GET, {}, None)"

with pytest.raises(JournyException):
HttpRequest(123, Method.GET, HttpHeaders(), None)
Expand All @@ -43,10 +50,10 @@ def test_http_request():
def test_http_response():
response = HttpResponse(200, HttpHeaders(), None)

assert (response.status_code == 200)
assert (response.headers == HttpHeaders())
assert response.status_code == 200
assert response.headers == HttpHeaders()

assert (response.__str__() == "HttpResponse(200, {}, None)")
assert response.__str__() == "HttpResponse(200, {}, None)"

with pytest.raises(JournyException):
HttpResponse("200", HttpHeaders(), None)
Expand All @@ -67,8 +74,10 @@ def test_http_client_testing():
dummy_response = HttpResponse(201, HttpHeaders(), {"message": "created"})
client = HttpClientTesting(dummy_response)

dummy_request = HttpRequest("/test", Method.POST, HttpHeaders(), {"object": "hello"})
dummy_request = HttpRequest(
"/test", Method.POST, HttpHeaders(), {"object": "hello"}
)
response = client.send(dummy_request)

assert (response.__str__() == dummy_response.__str__())
assert (client.received_request.__str__() == dummy_request.__str__())
assert response.__str__() == dummy_response.__str__()
assert client.received_request.__str__() == dummy_request.__str__()
43 changes: 26 additions & 17 deletions tests/test_results.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
import pytest

from journyio.results import Success, Failure, TrackingSnippetResponse, ApiKeyDetails, APIError
from journyio.results import (
Success,
Failure,
TrackingSnippetResponse,
ApiKeyDetails,
APIError,
)
from journyio.utils import JournyException


def test_success():
success = Success[None]("request_id", 100, None)
assert (success.data is None)
assert (success.request_id == "request_id")
assert (success.calls_remaining == 100)
assert success.data is None
assert success.request_id == "request_id"
assert success.calls_remaining == 100

with pytest.raises(JournyException):
Success[None](1234, 100, None)

with pytest.raises(JournyException):
Success[None]("request_id", "100", None)

assert (success.__str__() == "Success(request_id, 100, None)")
assert success.__str__() == "Success(request_id, 100, None)"


def test_failure():
failure = Failure(None, None, APIError.ServerError)
failure2 = Failure("request_id", 100, APIError.UnknownError)

assert (failure2.request_id == "request_id")
assert (failure2.calls_remaining == 100)
assert (failure2.error is APIError.UnknownError)
assert (failure.__str__() == "Failure(None, None, APIError.ServerError)")
assert (failure2.__str__() == "Failure(request_id, 100, APIError.UnknownError)")
assert failure2.request_id == "request_id"
assert failure2.calls_remaining == 100
assert failure2.error is APIError.UnknownError
assert failure.__str__() == "Failure(None, None, APIError.ServerError)"
assert failure2.__str__() == "Failure(request_id, 100, APIError.UnknownError)"

with pytest.raises(JournyException):
Failure("request_id", "100", APIError.ServerError)
Expand All @@ -40,10 +46,13 @@ def test_failure():
def test_tracking_snippet_response():
response = TrackingSnippetResponse("www.journy.io", "<div>SNIPPET</div>")

assert (response.domain == "www.journy.io")
assert (response.snippet == "<div>SNIPPET</div>")
assert response.domain == "www.journy.io"
assert response.snippet == "<div>SNIPPET</div>"

assert (response.__str__() == "TrackingSnippetResponse(www.journy.io, <div>SNIPPET</div>)")
assert (
response.__str__()
== "TrackingSnippetResponse(www.journy.io, <div>SNIPPET</div>)"
)

with pytest.raises(JournyException):
TrackingSnippetResponse(123, "snippet")
Expand All @@ -52,19 +61,19 @@ def test_tracking_snippet_response():

success = Success[TrackingSnippetResponse]("request_id", 100, response)

assert (success.data.__str__() == response.__str__())
assert success.data.__str__() == response.__str__()


def test_api_key_details():
details = ApiKeyDetails(["TrackData", "GetTrackingSnippet"])

assert (details.permissions == ["TrackData", "GetTrackingSnippet"])
assert details.permissions == ["TrackData", "GetTrackingSnippet"]

assert (details.__str__() == "ApiKeyDetails(['TrackData', 'GetTrackingSnippet'])")
assert details.__str__() == "ApiKeyDetails(['TrackData', 'GetTrackingSnippet'])"

with pytest.raises(JournyException):
ApiKeyDetails(123)

success = Success[ApiKeyDetails]("request_id", 100, details)

assert (success.data.__str__() == details.__str__())
assert success.data.__str__() == details.__str__()
6 changes: 3 additions & 3 deletions tests/test_user_identified.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ def test_user_identified():
user2 = UserIdentified.by_user_id("user_id")
user3 = UserIdentified.by_email("email")

assert (user1.format_identification() == {"email": "email", "userId": "user_id"})
assert (user2.format_identification() == {"userId": "user_id"})
assert (user3.format_identification() == {"email": "email"})
assert user1.format_identification() == {"email": "email", "userId": "user_id"}
assert user2.format_identification() == {"userId": "user_id"}
assert user3.format_identification() == {"email": "email"}

with pytest.raises(JournyException):
UserIdentified("", "")
Expand Down
21 changes: 13 additions & 8 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import pytest

from journyio.utils import JournyException, status_code_to_api_error, assert_journy, APIError
from journyio.utils import (
JournyException,
status_code_to_api_error,
assert_journy,
APIError,
)


def test_journy_exception():
Expand All @@ -10,19 +15,19 @@ def test_journy_exception():
try:
raise JournyException("custom message")
except JournyException as e:
assert (e.__str__() == "JournyException(custom message)")
assert e.__str__() == "JournyException(custom message)"


def test_status_code_to_api_error():
with pytest.raises(JournyException):
raise status_code_to_api_error("status_code")

assert (status_code_to_api_error(200) is APIError.UnknownError)
assert (status_code_to_api_error(401) is APIError.UnauthorizedError)
assert (status_code_to_api_error(400) is APIError.BadArgumentsError)
assert (status_code_to_api_error(429) is APIError.TooManyRequests)
assert (status_code_to_api_error(404) is APIError.NotFoundError)
assert (status_code_to_api_error(500) is APIError.ServerError)
assert status_code_to_api_error(200) is APIError.UnknownError
assert status_code_to_api_error(401) is APIError.UnauthorizedError
assert status_code_to_api_error(400) is APIError.BadArgumentsError
assert status_code_to_api_error(429) is APIError.TooManyRequests
assert status_code_to_api_error(404) is APIError.NotFoundError
assert status_code_to_api_error(500) is APIError.ServerError


def test_assert_journy():
Expand Down

0 comments on commit e85a30f

Please sign in to comment.