-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: moves session and url to resource attributes
- Loading branch information
Showing
8 changed files
with
79 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from unittest.mock import Mock | ||
import pandas as pd | ||
import pytest | ||
import responses | ||
|
@@ -9,83 +10,86 @@ | |
|
||
from .api import load_mock # type: ignore | ||
|
||
session = Mock() | ||
url = Mock() | ||
|
||
|
||
class TestUser: | ||
def test_guid(self): | ||
user = User() | ||
user = User(session, url) | ||
assert hasattr(user, "guid") | ||
assert user.guid is None | ||
user = User({"guid": "test_guid"}) | ||
user = User(session, url, **{"guid": "test_guid"}) | ||
assert user.guid == "test_guid" | ||
|
||
def test_email(self): | ||
user = User() | ||
user = User(session, url) | ||
assert hasattr(user, "email") | ||
assert user.email is None | ||
user = User({"email": "[email protected]"}) | ||
user = User(session, url, **{"email": "[email protected]"}) | ||
assert user.email == "[email protected]" | ||
|
||
def test_username(self): | ||
user = User() | ||
user = User(session, url) | ||
assert hasattr(user, "username") | ||
assert user.username is None | ||
user = User({"username": "test_user"}) | ||
user = User(session, url, **{"username": "test_user"}) | ||
assert user.username == "test_user" | ||
|
||
def test_first_name(self): | ||
user = User() | ||
user = User(session, url) | ||
assert hasattr(user, "first_name") | ||
assert user.first_name is None | ||
user = User({"first_name": "John"}) | ||
user = User(session, url, **{"first_name": "John"}) | ||
assert user.first_name == "John" | ||
|
||
def test_last_name(self): | ||
user = User() | ||
user = User(session, url) | ||
assert hasattr(user, "last_name") | ||
assert user.last_name is None | ||
user = User({"last_name": "Doe"}) | ||
user = User(session, url, **{"last_name": "Doe"}) | ||
assert user.last_name == "Doe" | ||
|
||
def test_user_role(self): | ||
user = User() | ||
user = User(session, url) | ||
assert hasattr(user, "user_role") | ||
assert user.user_role is None | ||
user = User({"user_role": "admin"}) | ||
user = User(session, url, **{"user_role": "admin"}) | ||
assert user.user_role == "admin" | ||
|
||
def test_created_time(self): | ||
user = User() | ||
user = User(session, url) | ||
assert hasattr(user, "created_time") | ||
assert user.created_time is None | ||
user = User({"created_time": "2022-01-01T00:00:00"}) | ||
user = User(session, url, **{"created_time": "2022-01-01T00:00:00"}) | ||
assert user.created_time == "2022-01-01T00:00:00" | ||
|
||
def test_updated_time(self): | ||
user = User() | ||
user = User(session, url) | ||
assert hasattr(user, "updated_time") | ||
assert user.updated_time is None | ||
user = User({"updated_time": "2022-01-01T00:00:00"}) | ||
user = User(session, url, **{"updated_time": "2022-01-01T00:00:00"}) | ||
assert user.updated_time == "2022-01-01T00:00:00" | ||
|
||
def test_active_time(self): | ||
user = User() | ||
user = User(session, url) | ||
assert hasattr(user, "active_time") | ||
assert user.active_time is None | ||
user = User({"active_time": "2022-01-01T00:00:00"}) | ||
user = User(session, url, **{"active_time": "2022-01-01T00:00:00"}) | ||
assert user.active_time == "2022-01-01T00:00:00" | ||
|
||
def test_confirmed(self): | ||
user = User() | ||
user = User(session, url) | ||
assert hasattr(user, "confirmed") | ||
assert user.confirmed is None | ||
user = User({"confirmed": True}) | ||
user = User(session, url, **{"confirmed": True}) | ||
assert user.confirmed is True | ||
|
||
def test_locked(self): | ||
user = User() | ||
user = User(session, url) | ||
assert hasattr(user, "locked") | ||
assert user.locked is None | ||
user = User({"locked": False}) | ||
user = User(session, url, **{"locked": False}) | ||
assert user.locked is False | ||
|
||
|
||
|
@@ -117,7 +121,7 @@ def test_users_find(self): | |
|
||
df = pd.DataFrame(all_users) | ||
assert isinstance(df, pd.DataFrame) | ||
assert df.shape == (3, 13) | ||
assert df.shape == (3, 11) | ||
assert df.columns.to_list() == [ | ||
"email", | ||
"username", | ||
|
@@ -130,8 +134,6 @@ def test_users_find(self): | |
"confirmed", | ||
"locked", | ||
"guid", | ||
"session", | ||
"url", | ||
] | ||
assert df["username"].to_list() == ["al", "robert", "carlos12"] | ||
|
||
|
@@ -250,19 +252,3 @@ def test_user_update(self): | |
# * test setting the other fields | ||
# * test invalid field | ||
# * error response (e.g. not authorized) | ||
|
||
@responses.activate | ||
def test_user_cant_setattr(self): | ||
responses.get( | ||
"https://connect.example/__api__/v1/users/20a79ce3-6e87-4522-9faf-be24228800a4", | ||
json=load_mock("v1/users/20a79ce3-6e87-4522-9faf-be24228800a4.json"), | ||
) | ||
|
||
con = Client(api_key="12345", url="https://connect.example/") | ||
carlos = con.users.get("20a79ce3-6e87-4522-9faf-be24228800a4") | ||
|
||
with pytest.raises( | ||
AttributeError, | ||
match=r"Cannot set attributes: use update\(\) instead", | ||
): | ||
carlos.first_name = "Carlitos" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
from posit.connect import Client | ||
|
||
with Client() as client: | ||
print(client.get("v1/users")) | ||
print(client.users.get("f55ca95d-ce52-43ed-b31b-48dc4a07fe13")) | ||
|
||
users = client.users | ||
users = users.find(lambda user: user["first_name"].startswith("T")) | ||
users = users.find(lambda user: user["last_name"].startswith("S")) | ||
user = users.find_one(lambda user: user["user_role"] == "administrator") | ||
print(user) | ||
client.get("v1/users") | ||
client.users.get("f55ca95d-ce52-43ed-b31b-48dc4a07fe13") | ||
client.users.find(lambda user: user.first_name.startswith("T")) | ||
client.users.find_one(lambda user: user.first_name.startswith("T")) |