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

testing env support #288

Merged
merged 6 commits into from
Oct 7, 2022
Merged

testing env support #288

merged 6 commits into from
Oct 7, 2022

Conversation

Shending-Help
Copy link
Contributor

Description

This PR tests the parser function in env_populator.py from the previous PR, and also tests if variables are set correctly

This PR fixes #287

@netlify
Copy link

netlify bot commented Oct 6, 2022

Deploy Preview for robyn canceled.

Name Link
🔨 Latest commit b6b2c55
🔍 Latest deploy log https://app.netlify.com/sites/robyn/deploys/634062565931e60008a21f68

Copy link
Member

@sansyrox sansyrox left a comment

Choose a reason for hiding this comment

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

Hi @Shending-Help ,

The test structure looks great. I just have some suggestions regarding the implementation.

path = pathlib.Path(__file__).parent

#create a directory before test and delete it after test
def test_create_file():
Copy link
Member

Choose a reason for hiding this comment

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

You can use fixtures(https://docs.pytest.org/en/6.2.x/fixture.html) instead of depending on the execution order of tests.

You would write the test like this

@pytest.fixture
def env_file():
    dir = path / "test_dir"
    env_file = dir / "robyn.env"
    env_file.write_text(CONTENT)
    yield
    env_file.unlink()

def test_parser(env_file):
     ...

integration_tests/test_env_populator.py Outdated Show resolved Hide resolved
integration_tests/test_env_populator.py Outdated Show resolved Hide resolved
Comment on lines 81 to 82
os.environ["ROBYN_PORT"] = os.environ["PORT"]
Copy link
Member

Choose a reason for hiding this comment

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

Try setting ROBYN_URL and ROBYN_PORT directly through load_vars()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but shouldn't that be left to the user, it won't always be the case that a user will provide a PORT and a URL in robyn.env but even if he did those vars will be loaded automatically and he can just assign them to a variable and use them

Copy link
Member

Choose a reason for hiding this comment

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

@Shending-Help , actually the way the env file is supposed to behave in my mind is that if the user sets a config then they don't have to worry about altering the code.

Copy link
Contributor Author

@Shending-Help Shending-Help Oct 7, 2022

Choose a reason for hiding this comment

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

but that wouldn't be the case for other variables such as a token or db credentials so should we just auto invoke the variables that are relevant for robyn and other variables get left for the user to decide what to do with them?

Copy link
Member

Choose a reason for hiding this comment

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

No no. What I mean is if you set ROBYN_PORT in the test then it will be automatically present in the environment. That way you don't need to set that variable in the fixture.

If that still doesn't make sense then we can hop on a quick call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think i understand what you mean i will get on it and check with you afterwards

Copy link
Member

Choose a reason for hiding this comment

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

Perfect!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think this should be it, I changed the content written in robyn.env to include ROBYN_PORT and ROBYN_URL and it worked, i also deleted the fixture i created in conftest.py since it is of no use now.

@@ -12,7 +12,7 @@
#create robyn.env before test and delete it after test
@pytest.fixture
def env_file():
CONTENT = "PORT=8080"
CONTENT = "ROBYN_PORT=8080" + "\n" + "ROBYN_URL=127.0.1.1"
Copy link
Member

Choose a reason for hiding this comment

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

You can use python multiline strings(https://www.w3schools.com/python/gloss_python_multi_line_strings.asp) here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

env_file.write_text(CONTENT)
yield
env_file.unlink()
os.unsetenv("PORT")
Copy link
Member

Choose a reason for hiding this comment

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

This should be ROBYN_PORT and ROBYN_URL now

def test_parser(env_file):
dir = path / "test_dir"
env_file = dir / "robyn.env"
assert list(parser(config_path = env_file)) == [['PORT', '8080']]
Copy link
Member

Choose a reason for hiding this comment

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

Same here

dir = path / "test_dir"
env_file = dir / "robyn.env"
load_vars(variables = parser(config_path = env_file))
assert os.environ['PORT'] == '8080'
Copy link
Member

Choose a reason for hiding this comment

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

same here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

@sansyrox sansyrox left a comment

Choose a reason for hiding this comment

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

All looks good. Just one final change before we merge it.

Copy link
Member

@sansyrox sansyrox left a comment

Choose a reason for hiding this comment

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

All looks good to me! Great work! :D

@sansyrox sansyrox merged commit 7d35952 into sparckles:main Oct 7, 2022
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