From 6f6366bb19b5f1e1b68d1b1f41a1d374e05ecf04 Mon Sep 17 00:00:00 2001 From: Neal Richardson Date: Wed, 21 Feb 2024 16:52:32 -0500 Subject: [PATCH 1/6] Add a test with responses for users.find --- src/posit/connect/users_test.py | 76 +++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/posit/connect/users_test.py b/src/posit/connect/users_test.py index e2304214..3164359b 100644 --- a/src/posit/connect/users_test.py +++ b/src/posit/connect/users_test.py @@ -1,7 +1,9 @@ import pytest +import responses from unittest.mock import patch +from .client import Client from .users import Users @@ -21,3 +23,77 @@ class TestUsers: def test_init(self, mock_config, mock_session): with pytest.raises(ValueError): Users(mock_config, mock_session, page_size=9999) + + @responses.activate + def test_get_users(self): + responses.get( + "https://connect.example/__api__/v1/users", + match=[ + responses.matchers.query_param_matcher( + {"page_size": 2, "page_number": 1} + ) + ], + json={ + "results": [ + { + "email": "alice@connect.example", + "username": "al", + "first_name": "Alice", + "last_name": "User", + "user_role": "publisher", + "created_time": "2017-08-08T15:24:32Z", + "updated_time": "2023-03-02T20:25:06Z", + "active_time": "2018-05-09T16:58:45Z", + "confirmed": True, + "locked": False, + "guid": "a01792e3-2e67-402e-99af-be04a48da074", + }, + { + "email": "bob@connect.example", + "username": "robert", + "first_name": "Bob", + "last_name": "Loblaw", + "user_role": "publisher", + "created_time": "2023-01-06T19:47:29Z", + "updated_time": "2023-05-05T19:08:45Z", + "active_time": "2023-05-05T20:29:11Z", + "confirmed": True, + "locked": False, + "guid": "87c12c08-11cd-4de1-8da3-12a7579c4998", + }, + ], + "current_page": 1, + "total": 3, + }, + ) + responses.get( + "https://connect.example/__api__/v1/users", + match=[ + responses.matchers.query_param_matcher( + {"page_size": 2, "page_number": 2} + ) + ], + json={ + "results": [ + { + "email": "carlos@connect.example", + "username": "carlos12", + "first_name": "Carlos", + "last_name": "User", + "user_role": "publisher", + "created_time": "2019-09-09T15:24:32Z", + "updated_time": "2022-03-02T20:25:06Z", + "active_time": "2020-05-11T16:58:45Z", + "confirmed": True, + "locked": False, + "guid": "20a79ce3-6e87-4522-9faf-be24228800a4", + }, + ], + "current_page": 2, + "total": 3, + }, + ) + + con = Client(api_key="12345", url="https://connect.example/") + u = Users(con.config, con.session, page_size=2) + assert len(u.find().data) == 3 From b3db2c4893e08d7b6b91477d21f9df2cd869ddc1 Mon Sep 17 00:00:00 2001 From: Neal Richardson Date: Wed, 21 Feb 2024 16:58:36 -0500 Subject: [PATCH 2/6] Add TODOs --- src/posit/connect/users_test.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/posit/connect/users_test.py b/src/posit/connect/users_test.py index 3164359b..26fe9670 100644 --- a/src/posit/connect/users_test.py +++ b/src/posit/connect/users_test.py @@ -95,5 +95,7 @@ def test_get_users(self): ) con = Client(api_key="12345", url="https://connect.example/") + # TODO: page_size should go with find(), can't pass it to client.users u = Users(con.config, con.session, page_size=2) + # TODO: Add __len__ method to Users assert len(u.find().data) == 3 From a3d63df313fead07a571dfc51ddd93b10331ba6d Mon Sep 17 00:00:00 2001 From: Neal Richardson Date: Thu, 22 Feb 2024 16:02:16 -0500 Subject: [PATCH 3/6] Add tests for to_pandas and find_one --- requirements-dev.txt | 1 + src/posit/connect/users_test.py | 31 ++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index d75abeb9..be7b5040 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -6,3 +6,4 @@ responses>=0.25 ruff==0.1.14 setuptools==69.0.3 setuptools-scm==8.0.4 +pandas==2.2.0 \ No newline at end of file diff --git a/src/posit/connect/users_test.py b/src/posit/connect/users_test.py index 26fe9670..7023cf8a 100644 --- a/src/posit/connect/users_test.py +++ b/src/posit/connect/users_test.py @@ -1,10 +1,11 @@ import pytest import responses +from pandas import DataFrame from unittest.mock import patch from .client import Client -from .users import Users +from .users import User, Users @pytest.fixture @@ -95,7 +96,31 @@ def test_get_users(self): ) con = Client(api_key="12345", url="https://connect.example/") - # TODO: page_size should go with find(), can't pass it to client.users + # TODO(#48): page_size should go with find(), can't pass it to client.users u = Users(con.config, con.session, page_size=2) - # TODO: Add __len__ method to Users + # TODO(#47): Add __len__ method to Users assert len(u.find().data) == 3 + + # Test to_pandas() + df = u.to_pandas() + assert isinstance(df, DataFrame) + assert df.shape == (3, 11) + assert df.columns.to_list() == [ + "email", + "username", + "first_name", + "last_name", + "user_role", + "created_time", + "updated_time", + "active_time", + "confirmed", + "locked", + "guid", + ] + assert df["username"].to_list() == ["al", "robert", "carlos12"] + + # Test find_one() + bob = u.find_one(lambda u: u['first_name'] == "Bob") + # Can't isinstance(bob, User) bc inherits TypedDict (cf. #23) + assert bob['username'] == "robert" \ No newline at end of file From ac5d0272b6d0f63301f604521b0776b6839aa101 Mon Sep 17 00:00:00 2001 From: Neal Richardson Date: Thu, 22 Feb 2024 16:04:51 -0500 Subject: [PATCH 4/6] Fix CI --- requirements-dev.txt | 2 +- src/posit/connect/users_test.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index be7b5040..394c4955 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -6,4 +6,4 @@ responses>=0.25 ruff==0.1.14 setuptools==69.0.3 setuptools-scm==8.0.4 -pandas==2.2.0 \ No newline at end of file +pandas \ No newline at end of file diff --git a/src/posit/connect/users_test.py b/src/posit/connect/users_test.py index 7023cf8a..52099817 100644 --- a/src/posit/connect/users_test.py +++ b/src/posit/connect/users_test.py @@ -121,6 +121,6 @@ def test_get_users(self): assert df["username"].to_list() == ["al", "robert", "carlos12"] # Test find_one() - bob = u.find_one(lambda u: u['first_name'] == "Bob") + bob = u.find_one(lambda u: u["first_name"] == "Bob") # Can't isinstance(bob, User) bc inherits TypedDict (cf. #23) - assert bob['username'] == "robert" \ No newline at end of file + assert bob["username"] == "robert" From 804ac67228c01b865e7a6ccc1a0ba73fda2fc6a9 Mon Sep 17 00:00:00 2001 From: Neal Richardson Date: Fri, 23 Feb 2024 12:04:54 -0500 Subject: [PATCH 5/6] test get user by id --- src/posit/connect/users_test.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/posit/connect/users_test.py b/src/posit/connect/users_test.py index 52099817..c6050056 100644 --- a/src/posit/connect/users_test.py +++ b/src/posit/connect/users_test.py @@ -97,7 +97,8 @@ def test_get_users(self): con = Client(api_key="12345", url="https://connect.example/") # TODO(#48): page_size should go with find(), can't pass it to client.users - u = Users(con.config, con.session, page_size=2) + u = con.users + u.page_size = 2 # TODO(#47): Add __len__ method to Users assert len(u.find().data) == 3 @@ -124,3 +125,28 @@ def test_get_users(self): bob = u.find_one(lambda u: u["first_name"] == "Bob") # Can't isinstance(bob, User) bc inherits TypedDict (cf. #23) assert bob["username"] == "robert" + + @responses.activate + def test_users_get(self): + responses.get( + "https://connect.example/__api__/v1/users/20a79ce3-6e87-4522-9faf-be24228800a4", + json={ + "email": "carlos@connect.example", + "username": "carlos12", + "first_name": "Carlos", + "last_name": "User", + "user_role": "publisher", + "created_time": "2019-09-09T15:24:32Z", + "updated_time": "2022-03-02T20:25:06Z", + "active_time": "2020-05-11T16:58:45Z", + "confirmed": True, + "locked": False, + "guid": "20a79ce3-6e87-4522-9faf-be24228800a4", + }, + ) + + con = Client(api_key="12345", url="https://connect.example/") + assert ( + con.users.get("20a79ce3-6e87-4522-9faf-be24228800a4")["username"] + == "carlos12" + ) From 40e03d758f20e98b36ac4cf96f3af6c5ecbe4452 Mon Sep 17 00:00:00 2001 From: Neal Richardson Date: Fri, 23 Feb 2024 12:18:12 -0500 Subject: [PATCH 6/6] Remove unused import --- src/posit/connect/users_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/posit/connect/users_test.py b/src/posit/connect/users_test.py index c6050056..08745053 100644 --- a/src/posit/connect/users_test.py +++ b/src/posit/connect/users_test.py @@ -5,7 +5,7 @@ from unittest.mock import patch from .client import Client -from .users import User, Users +from .users import Users @pytest.fixture