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

UIP-49 First steps on narrativemanager updating. #99

Merged
merged 4 commits into from
Aug 23, 2024

Conversation

briehl
Copy link
Collaborator

@briehl briehl commented Jun 13, 2024

First steps here for the NarrativeManager module fixup. These include:

  1. pytest-ifying the unit tests
  2. add the "workspace mock" (which really makes a mini internal workspace, sort of, which I think is entirely overkill and leads to being some weird hybrid between unit testing and integration testing) library to the conftest fixtures.
  3. Do linting and typing on the narrativemanager module. I did my best to not change any actual code here, as I want a more complete regression test suite first before I go poking around and undoing things that should've been changed 5+ years ago.

Comment on lines -44 to -45
self._get_set_api_client(ctx["token"]),
self._get_data_palette_client(ctx["token"]),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These aren't actually used anywhere, so removed from the narrative manager module.

@@ -56,7 +58,7 @@ def auth_client(config: dict[str, str | int]) -> Generator[KBaseAuth, None, None
yield KBaseAuth(config["auth-service-url"])

@pytest.fixture(scope="module")
def context(auth_token: str, auth_client: KBaseAuth) -> Generator[dict[str, any], None, None]:
def context(auth_token: str, auth_client: KBaseAuth) -> Generator[dict[str, Any], None, None]:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any is a builtin function. typing.Any is the type. Oops. (more in the next PR).

@@ -0,0 +1,142 @@
"""
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gave this the pytest treatment, but mostly left it unchanged.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried really hard not to change any actual code here. Most of the changes are either linting, typing, or renaming variables from camelCase to snake_case. I'll point out any real changes.

Comment on lines -26 to -27
self.set_api_client = set_api_client # DynamicServiceCache type
self.data_palette_client = data_palette_client # DynamicServiceCache type
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are set, then never used by the module. So they've been removed.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😖

data = None
while tries < 60 and data is None:
while tries < max_tries and data is None:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still a "magic number" but passes linting.
A future PR will clean this up further, and likely export it to a different module.

@@ -21,24 +25,32 @@ class NarrativeManager:
KB_CODE_CELL = "kb_code"
KB_STATE = "widget_state"

def __init__(self, config, user_id, set_api_client, data_palette_client, workspace_client, search_service_client):
def __init__(
self,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The selfs can all have the class "NarrativeManager" (with quotes)

time_ms = int(round(time.time() * 1000))
newWsName = self.user_id + ":narrative_" + str(time_ms)
new_ws_name = self.user_id + ":narrative_" + str(time_ms)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love that it suddenly starts using camel case halfway through the file!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The person who originally wrote this (and other chunks of NarrativeService) was primarily a Java programmer. Also, this particular file was basically directly translated from a JavaScript module. I guess naming conventions came along for the ride.

Comment on lines +63 to +64
with pytest.raises(ValueError, match="required format is <workspace_id>/<object_id>/<version>"):
nm.get_narrative_doc("blah/blah/blah/blah")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if the UPA is in the format ws_name/obj_name?

For these tests, is it worth creating a test workspace with the various needed objects and then recording the responses with vcrpy?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth creating a test workspace with the various needed objects?

I think that's worth doing for integration tests, once those get reworked. I think the issue here is that we're testing modifications to existing data. For the unit tests, I think I want to just test the NarrativeManager code in question - does it fire off the right backend call ok and format results properly. This currently does kind of a weird hodge-podge that, IMO, isn't necessary.

Integration tests should go all in, make a new Workspace, put data in it, modify it, see if the Workspace service responds right, changes the data as expected, and so on.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly, I'd like to just pytest + ruff in this PR, keep all the warts the same for consistency, then go back in the next PR and rework the process a bit.

nm = NarrativeManager(config, mock_user, mock_workspace_client, mock.MagicMock())

# simulate reverting fake narrative to version #2
# (make_object_history=True automatically makes 5 versions)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you put this comment up next to where you create the narrative?

make_object_history=True
)

(ws_id, obj, _) = narrative_ref.split("/")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth checking the version of the narrative ref here to ensure there are five versions?

@briehl
Copy link
Collaborator Author

briehl commented Jun 13, 2024

Responded to comments, and made a few requested changes.

@briehl briehl requested a review from ialarmedalien June 14, 2024 15:39
@briehl briehl merged commit f769976 into main Aug 23, 2024
1 of 2 checks passed
@briehl briehl deleted the fix-narrativemanager-tests branch August 23, 2024 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants