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

Move expected tracer.events from test code to golden files #133

Merged
merged 15 commits into from
Oct 8, 2021

Conversation

victorjzsun
Copy link
Collaborator

Addresses #125. Replaces certain assertions to instead be checked using golden files.

@laike9m
Copy link
Owner

laike9m commented Aug 27, 2021

Thanks for taking care of this. Actually what I meant in #125 is that, we still keep the assertions for tracer.events, but instead of writing something like assert trace.events == [...] in test code, we modify this check to something similar to the check of mocked_responses, aka using json.

You removed the assert trace.events == [...], but I think they still provide values by ensuring the correctness of an eariler stage. This can help ease dedebugging, because we know at which stage (generation of events, or generation of RPC payload) the data goes wrong.

@victorjzsun
Copy link
Collaborator Author

Could we generate the trace.events from the existing json data files? the events should be the same would seem a bit redundant to be put seperately. It would mean diff'ing the actual to intended might be a bit harder

@laike9m
Copy link
Owner

laike9m commented Aug 28, 2021

Generating trace.events JSON from the existing json data files is fine, but eventually we need to be able to dump these JSON from the trace.events in memory. Imagine we add a field to the events, and we definitely don't want to manually update the golden files. We already support the golden file override feature for mock response, and it would be the same for trace.events.

@laike9m
Copy link
Owner

laike9m commented Oct 4, 2021

I'm working on #126 to support Python 3.10. 3.10 changes bytecode quite a bit, so hard-code trace.events in test doesn't work anymore. I'm looking forward to this feature😀

Btw, do you think get_value in test/utils.py can be removed once this is in?

@victorjzsun
Copy link
Collaborator Author

I'm working on #126 to support Python 3.10. 3.10 changes bytecode quite a bit, so hard-code trace.events in test doesn't work anymore. I'm looking forward to this feature😀

Btw, do you think get_value in test/utils.py can be removed once this is in?

get_value is also used in tracer.loops assertions so not yet. I was planning on just focusing on tracer.events for this PR and deal with tracer.loops assertions in the next PR.

@victorjzsun victorjzsun marked this pull request as ready for review October 4, 2021 23:59
@@ -64,6 +64,8 @@ class Event:

@staticmethod
def value_serializer(inst: type, field: attr.Attribute, value: Any):
if field is None:
Copy link
Owner

Choose a reason for hiding this comment

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

why do we need this?

Copy link
Collaborator Author

@victorjzsun victorjzsun Oct 6, 2021

Choose a reason for hiding this comment

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

It's not particularly needed for this PR but if you use it in attr.asdict without the filter removing sources, it throws an error. This is due to it recursing into the sources list and becoming simple types with no fields. I added this as a sanity check to avoid that error if we happen to use attr.asdict for other purposes. If you don't think it's necessary, I'll remove it.

Copy link
Owner

Choose a reason for hiding this comment

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

We can leave it here and add a comment

test/conftest.py Outdated
@@ -73,4 +126,16 @@ def mocked_responses(request):
if get_os_type() == "windows" and frame_name in {"test_numpy", "test_pandas"}:
return

assert frame_data == golden_frame_data, json.dumps(frame_data, indent=4)
if golden_frame_data.get("response", None) is None:
Copy link
Owner

@laike9m laike9m Oct 5, 2021

Choose a reason for hiding this comment

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

If default is not given, it defaults to None

so None is not needed here

@pytest.fixture
def mocked_responses(request):
def check_response(request):
Copy link
Owner

Choose a reason for hiding this comment

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

I feel like this function is doing multiple things and we should break into smaller ones, something like

golden_response = get_golden_response()
if golden_response:
  assert ...
else:
  update_golden_response()

This makese the intent more clear. A good side effect: skip assertion if golden_response does not exist (now assertion still happens if the file exists).

Copy link
Owner

Choose a reason for hiding this comment

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

Same with check_tracer_events

test/conftest.py Outdated
snapshot = {
"location": symbol.snapshot.location,
"events_pointer": symbol.snapshot.events_pointer,
}
Copy link
Owner

@laike9m laike9m Oct 5, 2021

Choose a reason for hiding this comment

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

you can do

# If snapshot is None, keep it.
snapshot = snapshot and {
    "location": symbol.snapshot.location,
    "events_pointer": symbol.snapshot.events_pointer,
}


yield

tracer_events = []
Copy link
Owner

Choose a reason for hiding this comment

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

this can be extracted to a separate function.

Copy link
Owner

@laike9m laike9m left a comment

Choose a reason for hiding this comment

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

left some comments

@laike9m
Copy link
Owner

laike9m commented Oct 6, 2021 via email

test/conftest.py Outdated
Comment on lines 44 to 45
def get_golden_data(golden_filepath, key):
directory = os.path.dirname(golden_filepath)
Copy link
Owner

Choose a reason for hiding this comment

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

type hints

test/conftest.py Outdated
Comment on lines 57 to 58
def update_golden_data(golden_filepath, key, value):
golden_frame_data = {}
Copy link
Owner

Choose a reason for hiding this comment

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

ditto

test/conftest.py Outdated
Comment on lines 77 to 78
def get_serialized_events():
tracer_events = []
Copy link
Owner

Choose a reason for hiding this comment

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

Please add a doc string with a simple example.

@laike9m
Copy link
Owner

laike9m commented Oct 7, 2021

Overall it looks good now. Just a few nits.

@laike9m
Copy link
Owner

laike9m commented Oct 7, 2021

Btw, once you're done with the modification, could you squash the commits? I've configured this repo to support it, but I'm not sure if you'll see a button or something else. Let me know if you met any issues.

Nvm, I just find that I can squash myself
image

@victorjzsun
Copy link
Collaborator Author

Nvm, I just find that I can squash myself

Only collaborators have merge access so I can't even merge it regardless.

@laike9m
Copy link
Owner

laike9m commented Oct 7, 2021

Perhaps I should add you as a collaborator then🙂 let me think about it

@laike9m
Copy link
Owner

laike9m commented Oct 7, 2021

Anything else you want to add? If not, Im going to merge it.

@victorjzsun
Copy link
Collaborator Author

victorjzsun commented Oct 7, 2021

Nope, go ahead 🙂

@laike9m laike9m changed the title Move assertions to golden files Move expected tracer.events from test code to golden files Oct 8, 2021
@laike9m laike9m merged commit 30957ad into laike9m:master Oct 8, 2021
@laike9m
Copy link
Owner

laike9m commented Oct 8, 2021

Merged. Thanks again for the great contribution!

@victorjzsun victorjzsun deleted the consolidate-tracer-asserts branch October 8, 2021 03:48
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