From 5c201316089311e9350cdef54c466e0a2e380d1b Mon Sep 17 00:00:00 2001 From: Shending-Help Date: Fri, 7 Oct 2022 00:58:32 +0200 Subject: [PATCH 1/6] testing env support --- integration_tests/test_env_populator.py | 31 +++++++++++++++++++++++++ robyn/env_populator.py | 6 ++--- 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 integration_tests/test_env_populator.py diff --git a/integration_tests/test_env_populator.py b/integration_tests/test_env_populator.py new file mode 100644 index 000000000..0ab6adea0 --- /dev/null +++ b/integration_tests/test_env_populator.py @@ -0,0 +1,31 @@ +from robyn.env_populator import load_vars, parser +import pathlib +import os + +# content of test_tmp_path.py +CONTENT = "PORT=8080" + +path = pathlib.Path(__file__).parent + +#create a directory before test and delete it after test +def test_create_file(): + d = path / "test_dir" + p = d / "robyn.env" + p.write_text(CONTENT) + +def test_parser(): + d = path / "test_dir" + p = d / "robyn.env" + assert list(parser(config_path = p)) == [['PORT', '8080']] + +def test_load_vars(): + d = path / "test_dir" + p = d / "robyn.env" + load_vars(variables = parser(config_path = p)) + assert os.environ['PORT'] == '8080' + +#delete the file after test +def test_delete_file(): + d = path / "test_dir" + p = d / "robyn.env" + p.unlink() \ No newline at end of file diff --git a/robyn/env_populator.py b/robyn/env_populator.py index e4d0f6ec8..8aaf218cc 100644 --- a/robyn/env_populator.py +++ b/robyn/env_populator.py @@ -29,10 +29,8 @@ def parser(config_path=CONFIG_PATH): def load_vars(variables = None): """Main function""" - variables = parser() - if variables is None: - return + variables = parser() for var in variables: if var[0] in os.environ: @@ -43,6 +41,6 @@ def load_vars(variables = None): logger.info(f" Variable {var[0]} set to {var[1]}") - + From 2aeed3e1d825e8e616450cc3661267cdffa7c1d1 Mon Sep 17 00:00:00 2001 From: Shending-Help Date: Fri, 7 Oct 2022 16:54:58 +0200 Subject: [PATCH 2/6] verify that a connection can be established with the port --- integration_tests/conftest.py | 14 ++++++- integration_tests/test_env_populator.py | 54 +++++++++++++++---------- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/integration_tests/conftest.py b/integration_tests/conftest.py index 54d9230ce..4745bf370 100644 --- a/integration_tests/conftest.py +++ b/integration_tests/conftest.py @@ -74,4 +74,16 @@ def dev_session(): process = spawn_process(command) time.sleep(5) yield - kill_process(process) \ No newline at end of file + kill_process(process) + +@pytest.fixture(scope="session") +def test_session(): + os.environ["ROBYN_URL"] = "127.0.0.1" + os.environ["ROBYN_PORT"] = os.environ["PORT"] + current_file_path = pathlib.Path(__file__).parent.resolve() + base_routes = os.path.join(current_file_path, "./base_routes.py") + command = ["python3", base_routes, "--dev"] + process = spawn_process(command) + time.sleep(5) + yield + kill_process(process) \ No newline at end of file diff --git a/integration_tests/test_env_populator.py b/integration_tests/test_env_populator.py index 0ab6adea0..bdd5d24af 100644 --- a/integration_tests/test_env_populator.py +++ b/integration_tests/test_env_populator.py @@ -1,31 +1,41 @@ +from integration_tests.conftest import test_session from robyn.env_populator import load_vars, parser import pathlib import os +import pytest +import requests + -# content of test_tmp_path.py -CONTENT = "PORT=8080" path = pathlib.Path(__file__).parent -#create a directory before test and delete it after test -def test_create_file(): - d = path / "test_dir" - p = d / "robyn.env" - p.write_text(CONTENT) - -def test_parser(): - d = path / "test_dir" - p = d / "robyn.env" - assert list(parser(config_path = p)) == [['PORT', '8080']] - -def test_load_vars(): - d = path / "test_dir" - p = d / "robyn.env" - load_vars(variables = parser(config_path = p)) +#create robyn.env before test and delete it after test +@pytest.fixture +def env_file(): + CONTENT = "PORT=8080" + dir = path / "test_dir" + env_file = dir / "robyn.env" + env_file.write_text(CONTENT) + yield + env_file.unlink() + +def test_parser(env_file): + dir = path / "test_dir" + env_file = dir / "robyn.env" + assert list(parser(config_path = env_file)) == [['PORT', '8080']] + +def test_load_vars(env_file): + dir = path / "test_dir" + env_file = dir / "robyn.env" + load_vars(variables = parser(config_path = env_file)) assert os.environ['PORT'] == '8080' -#delete the file after test -def test_delete_file(): - d = path / "test_dir" - p = d / "robyn.env" - p.unlink() \ No newline at end of file + +def test_get_request(test_session, env_file): + dir = path / "test_dir" + env_file = dir / "robyn.env" + load_vars(variables = parser(config_path = env_file)) + port = os.environ['PORT'] + BASE_URL = f"http://127.0.0.1:{port}" + res = requests.get(f"{BASE_URL}") + assert res.status_code == 200 From 90e4221a5325e398c1e7c647c6c4350c734c601b Mon Sep 17 00:00:00 2001 From: Shending-Help Date: Fri, 7 Oct 2022 17:32:00 +0200 Subject: [PATCH 3/6] moving unit tests to a seperate folder, reseting variables set during testing --- integration_tests/test_env_populator.py | 16 +++---------- unit_tests/test_env_pop.py | 31 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 unit_tests/test_env_pop.py diff --git a/integration_tests/test_env_populator.py b/integration_tests/test_env_populator.py index bdd5d24af..492549875 100644 --- a/integration_tests/test_env_populator.py +++ b/integration_tests/test_env_populator.py @@ -18,20 +18,10 @@ def env_file(): env_file.write_text(CONTENT) yield env_file.unlink() + os.unsetenv("PORT") -def test_parser(env_file): - dir = path / "test_dir" - env_file = dir / "robyn.env" - assert list(parser(config_path = env_file)) == [['PORT', '8080']] - -def test_load_vars(env_file): - dir = path / "test_dir" - env_file = dir / "robyn.env" - load_vars(variables = parser(config_path = env_file)) - assert os.environ['PORT'] == '8080' - - -def test_get_request(test_session, env_file): +# this tests if a connection can be made to the server with the correct port imported from the env file +def test_env_population(test_session, env_file): dir = path / "test_dir" env_file = dir / "robyn.env" load_vars(variables = parser(config_path = env_file)) diff --git a/unit_tests/test_env_pop.py b/unit_tests/test_env_pop.py new file mode 100644 index 000000000..7be7b8f9e --- /dev/null +++ b/unit_tests/test_env_pop.py @@ -0,0 +1,31 @@ +from robyn.env_populator import load_vars, parser +import pathlib +import os +import pytest + + + + +path = pathlib.Path(__file__).parent + +#create robyn.env before test and delete it after test +@pytest.fixture +def env_file(): + CONTENT = "PORT=8080" + dir = path / "test_dir" + env_file = dir / "robyn.env" + env_file.write_text(CONTENT) + yield + env_file.unlink() + os.unsetenv("PORT") + +def test_parser(env_file): + dir = path / "test_dir" + env_file = dir / "robyn.env" + assert list(parser(config_path = env_file)) == [['PORT', '8080']] + +def test_load_vars(env_file): + dir = path / "test_dir" + env_file = dir / "robyn.env" + load_vars(variables = parser(config_path = env_file)) + assert os.environ['PORT'] == '8080' \ No newline at end of file From abe8a2aec71c19d3ff50773fb87e28f0733c7d62 Mon Sep 17 00:00:00 2001 From: Shending-Help Date: Fri, 7 Oct 2022 18:54:00 +0200 Subject: [PATCH 4/6] auto importing robyn settings --- integration_tests/conftest.py | 2 +- integration_tests/test_env_populator.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/integration_tests/conftest.py b/integration_tests/conftest.py index 4745bf370..ad2c0b75b 100644 --- a/integration_tests/conftest.py +++ b/integration_tests/conftest.py @@ -79,7 +79,7 @@ def dev_session(): @pytest.fixture(scope="session") def test_session(): os.environ["ROBYN_URL"] = "127.0.0.1" - os.environ["ROBYN_PORT"] = os.environ["PORT"] + os.environ["ROBYN_PORT"] = "5000" current_file_path = pathlib.Path(__file__).parent.resolve() base_routes = os.path.join(current_file_path, "./base_routes.py") command = ["python3", base_routes, "--dev"] diff --git a/integration_tests/test_env_populator.py b/integration_tests/test_env_populator.py index 492549875..346f3e8e3 100644 --- a/integration_tests/test_env_populator.py +++ b/integration_tests/test_env_populator.py @@ -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" dir = path / "test_dir" env_file = dir / "robyn.env" env_file.write_text(CONTENT) @@ -25,7 +25,8 @@ def test_env_population(test_session, env_file): dir = path / "test_dir" env_file = dir / "robyn.env" load_vars(variables = parser(config_path = env_file)) - port = os.environ['PORT'] - BASE_URL = f"http://127.0.0.1:{port}" + PORT = os.environ['ROBYN_PORT'] + HOST = os.environ['ROBYN_URL'] + BASE_URL = f"http://{HOST}:{PORT}" res = requests.get(f"{BASE_URL}") assert res.status_code == 200 From f2c974f04381f129350cb47b138138eddc87dd16 Mon Sep 17 00:00:00 2001 From: Shending-Help Date: Fri, 7 Oct 2022 18:58:08 +0200 Subject: [PATCH 5/6] deleting the test_session fixture not needed --- integration_tests/conftest.py | 11 ----------- integration_tests/test_env_populator.py | 4 ++-- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/integration_tests/conftest.py b/integration_tests/conftest.py index ad2c0b75b..7dc969566 100644 --- a/integration_tests/conftest.py +++ b/integration_tests/conftest.py @@ -76,14 +76,3 @@ def dev_session(): yield kill_process(process) -@pytest.fixture(scope="session") -def test_session(): - os.environ["ROBYN_URL"] = "127.0.0.1" - os.environ["ROBYN_PORT"] = "5000" - current_file_path = pathlib.Path(__file__).parent.resolve() - base_routes = os.path.join(current_file_path, "./base_routes.py") - command = ["python3", base_routes, "--dev"] - process = spawn_process(command) - time.sleep(5) - yield - kill_process(process) \ No newline at end of file diff --git a/integration_tests/test_env_populator.py b/integration_tests/test_env_populator.py index 346f3e8e3..a498ca453 100644 --- a/integration_tests/test_env_populator.py +++ b/integration_tests/test_env_populator.py @@ -1,4 +1,4 @@ -from integration_tests.conftest import test_session +# from integration_tests.conftest import test_session from robyn.env_populator import load_vars, parser import pathlib import os @@ -21,7 +21,7 @@ def env_file(): os.unsetenv("PORT") # this tests if a connection can be made to the server with the correct port imported from the env file -def test_env_population(test_session, env_file): +def test_env_population(dev_session, env_file): dir = path / "test_dir" env_file = dir / "robyn.env" load_vars(variables = parser(config_path = env_file)) From b6b2c5544cedd48280d0d0f5a8a77e6c5c7c7623 Mon Sep 17 00:00:00 2001 From: Shending-Help Date: Fri, 7 Oct 2022 19:30:47 +0200 Subject: [PATCH 6/6] changing unit tests accordingly --- integration_tests/test_env_populator.py | 3 ++- unit_tests/test_env_pop.py | 11 +++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/integration_tests/test_env_populator.py b/integration_tests/test_env_populator.py index a498ca453..5f6d24bdf 100644 --- a/integration_tests/test_env_populator.py +++ b/integration_tests/test_env_populator.py @@ -12,7 +12,8 @@ #create robyn.env before test and delete it after test @pytest.fixture def env_file(): - CONTENT = "ROBYN_PORT=8080" + "\n" + "ROBYN_URL=127.0.1.1" + CONTENT = """ROBYN_PORT=8080 + ROBYN_URL=127.0.1.1""" dir = path / "test_dir" env_file = dir / "robyn.env" env_file.write_text(CONTENT) diff --git a/unit_tests/test_env_pop.py b/unit_tests/test_env_pop.py index 7be7b8f9e..c552ddb87 100644 --- a/unit_tests/test_env_pop.py +++ b/unit_tests/test_env_pop.py @@ -11,21 +11,24 @@ #create robyn.env before test and delete it after test @pytest.fixture def env_file(): - CONTENT = "PORT=8080" + CONTENT = """ROBYN_PORT=8080 + ROBYN_URL=127.0.1.1""" dir = path / "test_dir" env_file = dir / "robyn.env" env_file.write_text(CONTENT) yield env_file.unlink() - os.unsetenv("PORT") + os.unsetenv("ROBYN_PORT") + os.unsetenv("ROBYN_URL") def test_parser(env_file): dir = path / "test_dir" env_file = dir / "robyn.env" - assert list(parser(config_path = env_file)) == [['PORT', '8080']] + assert list(parser(config_path = env_file)) == [['ROBYN_PORT', '8080'], ['ROBYN_URL', '127.0.1.1']] def test_load_vars(env_file): dir = path / "test_dir" env_file = dir / "robyn.env" load_vars(variables = parser(config_path = env_file)) - assert os.environ['PORT'] == '8080' \ No newline at end of file + assert os.environ['ROBYN_PORT'] == '8080' + assert os.environ['ROBYN_URL'] == '127.0.1.1' \ No newline at end of file