Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luciano-renzi committed Jan 24, 2018
1 parent 17c2c9c commit 912f381
Show file tree
Hide file tree
Showing 13 changed files with 564 additions and 45 deletions.
13 changes: 10 additions & 3 deletions golem/core/environment_manager.py
Expand Up @@ -6,8 +6,10 @@

def get_envs(workspace, project):
"""get the list of envs defined in the environments.json file"""
envs = []
env_data = get_environment_data(workspace, project)
envs = list(env_data.keys())
if env_data:
envs = list(env_data.keys())
return envs


Expand All @@ -16,7 +18,11 @@ def get_environment_data(workspace, project):
env_data = {}
env_json_path = os.path.join(workspace, 'projects', project, 'environments.json')
if os.path.exists(env_json_path):
env_data = utils.load_json_from_file(env_json_path)
json_data = utils.load_json_from_file(env_json_path,
ignore_failure=True,
default={})
if json_data:
env_data = json_data
return env_data


Expand All @@ -32,7 +38,8 @@ def get_environments_as_string(workspace, project):

def save_environments(workspace, project, env_data):
"""save environments.json file contents.
Returns a string with the error or empty otherwise"""
env_data must be a valid json string.
Returns a string with the error or empty string otherwise"""
error = ''
if len(env_data):
try:
Expand Down
14 changes: 9 additions & 5 deletions golem/core/file_manager.py
Expand Up @@ -139,11 +139,15 @@ def new_directory_of_type(root_path, project, parents, dir_name, dir_type):
If the directory is inside other directories, these should already exist.
parents is a list of parent directories.
dir_type should be in ['tests', 'suites', 'pages']"""
parents = os.sep.join(parents)
path = os.path.join(root_path, 'projects', project, dir_type, parents, dir_name)
errors = []
if os.path.exists(path):
errors.append('A directory with that name already exists')
if dir_type not in ['tests', 'suites', 'pages']:
errors.append('{} is not a valid dir_type'.format(dir_type))
else:
create_directory(path=path, add_init=True)
parents = os.sep.join(parents)
path = os.path.join(root_path, 'projects', project,
dir_type, parents, dir_name)
if os.path.exists(path):
errors.append('A directory with that name already exists')
else:
create_directory(path=path, add_init=True)
return errors
26 changes: 20 additions & 6 deletions golem/core/page_object.py
Expand Up @@ -105,8 +105,9 @@ def get_page_object_code(path):
"""Get the page object code as string given the full path
to the python file"""
code = ''
with open(path) as ff:
code = ff.read()
if os.path.isfile(path):
with open(path) as ff:
code = ff.read()
return code


Expand All @@ -116,7 +117,6 @@ def save_page_object(root_path, project, full_page_name, elements,
full_page_name must be a dot path starting from /project/pages/
directory, (i.e.: 'module.sub_module.page_name_01')
"""

def format_element_string(name, selector, value, display_name):
formatted = ("\n\n{0} = ('{1}', \'{2}\', '{3}')"
.format(element['name'], element['selector'],
Expand All @@ -134,8 +134,15 @@ def format_element_string(name, selector, value, display_name):
# replace the spaces with underlines of the element name
if ' ' in element['name']:
element['name'] = element['name'].replace(' ', '_')
# escape quote characters
element['value'] = element['value'].replace('"', '\\"').replace("'", "\\'")
po_file.write(format_element_string(name, selector, value, display_name))
if not element['display_name']:
element['display_name'] = element['name']
formatted = format_element_string(element['name'],
element['selector'],
element['value'],
element['display_name'])
po_file.write(formatted)
for func in functions:
po_file.write('\n\n' + func)

Expand Down Expand Up @@ -170,7 +177,7 @@ def new_page_object(root_path, project, parents, page_name,
'pages', os.sep.join(parents))
if not os.path.exists(page_path):
if add_parents:
base_path = os.path.join(root_path, 'projects', project, 'pages')
base_path = pages_base_dir(root_path, project)
for parent in parents:
base_path = os.path.join(base_path, parent)
if not os.path.exists(base_path):
Expand All @@ -193,4 +200,11 @@ def generate_page_path(root_path, project, full_page_name):
page_name, parents = utils.separate_file_from_parents(full_page_name)
page_path = os.path.join(root_path, 'projects', project, 'pages',
os.sep.join(parents), '{}.py'.format(page_name))
return page_path
return page_path


def pages_base_dir(root_path, project):
"""Generate base dir for pages.
i.e.: <root_path>/projets/<project>/pages/
"""
return os.path.join(root_path, 'projects', project, 'pages')
13 changes: 9 additions & 4 deletions golem/core/utils.py
Expand Up @@ -272,15 +272,20 @@ def choose_driver_by_precedence(cli_drivers=None, suite_drivers=None,
return chosen_drivers


def load_json_from_file(filepath):
json_data = None
# TODO
def load_json_from_file(filepath, ignore_failure=False, default=None):
json_data = default
with open(filepath) as json_file:
try:
json_data = json.load(json_file)
contents = json_file.read()
if len(contents.strip()):
json_data = json.loads(contents)
except Exception as e:
msg = 'There was an error parsing file {}'.format(filepath)
print(msg)
print(traceback.format_exc())
raise Exception(msg).with_traceback(e.__traceback__)
if not ignore_failure:
raise Exception(msg).with_traceback(e.__traceback__)
return json_data


Expand Down
2 changes: 1 addition & 1 deletion golem/gui/__init__.py
Expand Up @@ -511,7 +511,7 @@ def rename_element():
def get_page_objects():
if request.method == 'POST':
project = request.form['project']
path = os.path.join(root_path, 'projects', project, 'pages')
path = page_object.pages_base_dir(root_path, project)
page_objects = file_manager.get_files_dot_path(path)
return json.dumps(page_objects)

Expand Down
1 change: 1 addition & 0 deletions golem/test_runner/start_execution.py
Expand Up @@ -163,6 +163,7 @@ def run_test_or_suite(workspace, project, test=None, suite=None, directory=None)
suite)
suite_drivers = suite_module.get_suite_browsers(workspace, project, suite)
suite_envs = suite_module.get_suite_environments(workspace, project, suite)
# TODO, get_before and get_after should be suite module functions
suite_imported_module = suite_module.get_suite_module(workspace, project, suite)
execution['suite_before'] = getattr(suite_imported_module, 'before', None)
execution['suite_after'] = getattr(suite_imported_module, 'after', None)
Expand Down
210 changes: 210 additions & 0 deletions tests/core/environment_manager_test.py
@@ -0,0 +1,210 @@
import os

from tests import helper_functions
from golem.core import environment_manager

from tests.fixtures import testdir_fixture, permanent_project_fixture


ENV_DATA = ('{\n'
' "test": {\n'
' "url": "http://localhost:8000"\n'
' },\n'
' "development": {\n'
' "url": "http://localhost:8001"\n'
' }\n'
'}')

ENV_DATA_INVALID_JSON = ('{\n'
' \'test\': {\n'
' "url": "http://localhost:8000"\n'
' }\n'
'}')


class Test_get_envs:

def test_get_envs(self, permanent_project_fixture):
project = permanent_project_fixture['name']
testdir = permanent_project_fixture['testdir']
env_json_path = os.path.join(testdir, 'projects',
project, 'environments.json')

with open(env_json_path, 'w') as env_json_file:
env_json_file.write(ENV_DATA)
envs = environment_manager.get_envs(testdir, project)
expected_envs = ['test', 'development']
assert envs == expected_envs


def test_get_envs_empty_file(self, permanent_project_fixture):
project = permanent_project_fixture['name']
testdir = permanent_project_fixture['testdir']
env_json_path = os.path.join(testdir, 'projects',
project, 'environments.json')
with open(env_json_path, 'w') as env_json_file:
env_json_file.write('')
envs = environment_manager.get_envs(testdir, project)
expected_envs = []
assert envs == expected_envs


def test_get_envs_invalid_json(self, permanent_project_fixture):
project = permanent_project_fixture['name']
testdir = permanent_project_fixture['testdir']
env_json_path = os.path.join(testdir, 'projects',
project, 'environments.json')
with open(env_json_path, 'w') as env_json_file:
env_json_file.write(ENV_DATA_INVALID_JSON)
envs = environment_manager.get_envs(testdir, project)
expected_envs = []
assert envs == expected_envs


def test_get_envs_file_not_exist(self, permanent_project_fixture):
project = permanent_project_fixture['name']
testdir = permanent_project_fixture['testdir']
env_json_path = os.path.join(testdir, 'projects',
project, 'environments.json')
if os.path.isfile(env_json_path):
os.remove(env_json_path)
envs = environment_manager.get_envs(testdir, project)
expected_envs = []
assert envs == expected_envs


class Test_get_environment_data:

def test_get_environment_data(self, permanent_project_fixture):
project = permanent_project_fixture['name']
testdir = permanent_project_fixture['testdir']
env_json_path = os.path.join(testdir, 'projects',
project, 'environments.json')
with open(env_json_path, 'w') as env_json_file:
env_json_file.write(ENV_DATA)
result = environment_manager.get_environment_data(testdir, project)
expected = {
"test": {
"url": "http://localhost:8000"
},
"development": {
"url": "http://localhost:8001"
}
}
assert result == expected


def test_get_environment_data_empty_file(self, permanent_project_fixture):
project = permanent_project_fixture['name']
testdir = permanent_project_fixture['testdir']
env_json_path = os.path.join(testdir, 'projects',
project, 'environments.json')
with open(env_json_path, 'w') as env_json_file:
env_json_file.write('')
result = environment_manager.get_environment_data(testdir, project)
expected = {}
assert result == expected


def test_get_environment_data_invalid_json(self, permanent_project_fixture):
project = permanent_project_fixture['name']
testdir = permanent_project_fixture['testdir']
env_json_path = os.path.join(testdir, 'projects',
project, 'environments.json')
with open(env_json_path, 'w') as env_json_file:
env_json_file.write(ENV_DATA_INVALID_JSON)
result = environment_manager.get_environment_data(testdir, project)
expected = {}
assert result == expected


def test_get_environment_data_file_not_exist(self, permanent_project_fixture):
project = permanent_project_fixture['name']
testdir = permanent_project_fixture['testdir']
env_json_path = os.path.join(testdir, 'projects',
project, 'environments.json')
if os.path.isfile(env_json_path):
os.remove(env_json_path)
result = environment_manager.get_environment_data(testdir, project)
expected = {}
assert result == expected


class Test_get_environments_as_string:

def test_get_environments_as_string(self, permanent_project_fixture):
project = permanent_project_fixture['name']
testdir = permanent_project_fixture['testdir']
env_json_path = os.path.join(testdir, 'projects',
project, 'environments.json')
with open(env_json_path, 'w') as env_json_file:
env_json_file.write(ENV_DATA)
result = environment_manager.get_environments_as_string(testdir, project)
assert result == ENV_DATA


def test_get_environments_as_string_empty_file(self, permanent_project_fixture):
project = permanent_project_fixture['name']
testdir = permanent_project_fixture['testdir']
env_json_path = os.path.join(testdir, 'projects',
project, 'environments.json')
with open(env_json_path, 'w') as env_json_file:
env_json_file.write('')
result = environment_manager.get_environments_as_string(testdir, project)
assert result == ''


def test_get_environments_as_string_file_not_exist(self, permanent_project_fixture):
project = permanent_project_fixture['name']
testdir = permanent_project_fixture['testdir']
env_json_path = os.path.join(testdir, 'projects',
project, 'environments.json')
if os.path.isfile(env_json_path):
os.remove(env_json_path)
result = environment_manager.get_environments_as_string(testdir, project)
assert result == ''


class Test_save_environments:

def test_save_environments(self, permanent_project_fixture):
project = permanent_project_fixture['name']
testdir = permanent_project_fixture['testdir']
env_json_path = os.path.join(testdir, 'projects',
project, 'environments.json')
error = environment_manager.save_environments(testdir, project, ENV_DATA)
assert error == ''
with open(env_json_path) as json_file:
file_content = json_file.read()
assert file_content == ENV_DATA


def test_save_environments_empty_env_data(self, permanent_project_fixture):
project = permanent_project_fixture['name']
testdir = permanent_project_fixture['testdir']
env_json_path = os.path.join(testdir, 'projects',
project, 'environments.json')
empty_env_data = ''
error = environment_manager.save_environments(testdir, project,
empty_env_data)
assert error == ''
with open(env_json_path) as json_file:
file_content = json_file.read()
assert file_content == empty_env_data


def test_save_environments_invalid_json(self, permanent_project_fixture):
project = permanent_project_fixture['name']
testdir = permanent_project_fixture['testdir']
env_json_path = os.path.join(testdir, 'projects',
project, 'environments.json')
original_json = '{"test": ""}'
with open(env_json_path, 'w') as json_file:
file_content = json_file.write(original_json)
error = environment_manager.save_environments(testdir, project,
ENV_DATA_INVALID_JSON)
assert error == 'must be valid JSON'
# assert the original environments.json file was not modified
with open(env_json_path) as json_file:
file_content = json_file.read()
assert file_content == original_json

0 comments on commit 912f381

Please sign in to comment.