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

Allow global variables #507

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

gabrieltron
Copy link
Member

@gabrieltron gabrieltron commented Sep 12, 2021

Description

Allow global variables. When spec_vars is updated after performing a call, instead of updating the spec_vars of just the endpoint node, recursively update all parent endpoint nodes. When evaluating an expression, instead of searching for the variable in spec_vars of only the request's endpoint node, search all parent request nodes recursively.

Motivation behind this PR?

#265

What type of change is this?

Feature

Checklist

  • I have added a changelog entry / my PR does not need a new changelog entry. Instructions.
  • I have added/updated unit tests. Instructions.
  • New and existing unit tests pass locally with my changes. Instructions
  • I have self-documented code my changes by adding docstring(s) and comment(s). Instructions
  • Current PR does not significantly decrease the code coverage and docstring coverage.
  • My code follows the style guidelines of this project.
  • I have run ScanAPI locally and manually tested my changes. Instructions.
  • I have squashed my commits. Instructions.

Issue

Closes #265

Stannislav and others added 2 commits August 27, 2021 20:20
* Fix the --browser option

The problem was that webbrowser.open expects a URI and doesn't
work with relative local paths.

Solution: use pathlib to convert reporter.output_path to a URI
using pathlib.Path(reporter.output_path).resolve().as_uri().

* Add a changelog entry
@gabrieltron gabrieltron changed the title Global variables Allow global variables Sep 12, 2021
@gabrieltron gabrieltron force-pushed the global-variables branch 4 times, most recently from f13ac53 to 1d9cabf Compare September 12, 2021 17:26
@gabrieltron gabrieltron marked this pull request as ready for review September 12, 2021 17:32
@gabrieltron gabrieltron requested review from a team as code owners September 12, 2021 17:32
@gabrieltron gabrieltron force-pushed the global-variables branch 2 times, most recently from 26511c2 to 775c04a Compare September 12, 2021 19:26
Copy link
Member

@camilamaia camilamaia left a comment

Choose a reason for hiding this comment

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

Awesome PR @gabrieltron! This was a super desired feature that we were trying to deliver for a long time. Thank you very much!

Also, pretty elegant solution 🌟

I left only one small change request, let me know what do you think :)

@mark.context("when key exists in endpoint node")
@mark.it("should search endpoint node")
@mark.it("should return the value in endpoint node")
def test_should_return_list(self, spec_evaluator):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def test_should_return_list(self, spec_evaluator):
def test_should_return_list(self, mocker, spec_evaluator):

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

def test_should_return_list(self, spec_evaluator):
key = "requests"
expected_value = "value"
with mock.patch(
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
with mock.patch(
with mocker.patch(

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@@ -1,4 +1,5 @@
from pytest import fixture, mark, raises
from unittest import mock
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to import mock from unittest because we use the pytest-mock library. It already provides a mocker fixture which is a thin-wrapper around the patching API provided by the mock package:

Suggested change
from unittest import mock

The two next suggestions show how to do it using mocker fixture.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup! Fixed it. I forgot that the scope of a fixture is a function by default, so now I just used the existing mock from the fixture instead 😅

@@ -0,0 +1,29 @@
from pytest import mark
Copy link
Member

Choose a reason for hiding this comment

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

@gabrieltron can we have a test in which we have vars with the same name for both parent and child? To check that the inner value should be the one returned: the child's one.

For example:

endpoints:
  - name: snippets-api
    path: http://demo.scanapi.dev/api/v1/
    headers:
      Content-Type: application/json
    vars:
      my_var: one
    requests:
      - name: health
        path: health?${my_var}
    endpoints:
      - name: endpoint
        vars:
          my_var: two
        requests:
          - name: another health
            path: health?${my_var}

Request one should be: GET http://demo.scanapi.dev/api/v1/health?one
Request two should be: GET http://demo.scanapi.dev/api/v1/health?two

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice, I hadn't thought of that scenario. I changed the method so it only propagates the new vars.

I also changed it so it only writes if the variable doesn't exist. On the downside, this implies that there's no way of update global variables. On the upside, it further protects the variables already declared in the parents. Do you think this is a good approach? Or should request nodes be able to update global variables?

@gabrieltron
Copy link
Member Author

Any news on that? I'd appreciate feedback, even if it's that we should hold this up for now hahaha

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.

Global Variables
3 participants