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

Prevent certain resources from being generated #111

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ramses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

def includeme(config):
from .generators import generate_server, generate_models
from .utils import split_ignored_routes
Settings = dictset(config.registry.settings)
config.include('nefertari.engine')

Expand Down Expand Up @@ -56,6 +57,7 @@ def includeme(config):
config.include('nefertari.elasticsearch')

log.info('Starting server generation')
split_ignored_routes(config)
generate_server(raml_root, config)

log.info('Running nefertari.engine.setup_database')
Expand Down
4 changes: 3 additions & 1 deletion ramses/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .utils import (
is_dynamic_uri, resource_view_attrs, generate_model_name,
dynamic_part_name, attr_subresource, singular_subresource,
get_static_parent)
get_static_parent, is_ignored_resource)


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -132,6 +132,8 @@ def generate_server(raml_root, config):
for raml_resource in raml_root.resources:
if raml_resource.path in generated_resources:
continue
if is_ignored_resource(config, raml_resource):
continue

# Get Nefertari parent resource
parent_resource = _get_nefertari_parent_resource(
Expand Down
32 changes: 32 additions & 0 deletions ramses/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import six
import inflection

from nefertari.utils import split_strip


log = logging.getLogger(__name__)

Expand Down Expand Up @@ -337,3 +339,33 @@ def patch_view_model(view_cls, model_cls):
yield
finally:
view_cls.Model = original_model


def split_ignored_routes(config):
""" Helper function to convert param string into list.

:param config: Pyramid Configurator instance.
"""
param = 'ramses.ignored_routes'
val = config.registry.settings.get(param, '')
delim = ',' if ',' in val else '\n'
config.registry.settings[param] = split_strip(val, delim)


def is_ignored_resource(config, raml_resource):
""" Check if resource should be ignored.

Route is considered ignored when its path starts with one of
paths from 'ramses.ignored_routes' config param.

:param config: Pyramid Configurator instance.
:param raml_resource: Instance of ramlfications.raml.ResourceNode.
:returns bool: Indicating whether resource should be ignored.
"""
ignored_routes = config.registry.settings[
'ramses.ignored_routes']
res_path = raml_resource.path
for path in ignored_routes:
if path and res_path.startswith(path):
return True
return False
23 changes: 21 additions & 2 deletions tests/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ def test_generate_server_no_resources(self, mock_gen):
generators.generate_server(Mock(resources=None), 'foo')
assert not mock_gen.called

@patch.object(generators, 'is_ignored_resource')
@patch.object(generators, '_get_nefertari_parent_resource')
@patch.object(generators, 'generate_resource')
def test_generate_server_resources_generated(
self, mock_gen, mock_get):
self, mock_gen, mock_get, mock_check):
mock_check.return_value = False
config = Mock()
resources = [
Mock(path='/foo'),
Expand All @@ -49,10 +51,12 @@ def test_generate_server_resources_generated(
call(config, resources[1], mock_get()),
])

@patch.object(generators, 'is_ignored_resource')
@patch.object(generators, '_get_nefertari_parent_resource')
@patch.object(generators, 'generate_resource')
def test_generate_server_call_per_path(
self, mock_gen, mock_get):
self, mock_gen, mock_get, mock_check):
mock_check.return_value = False
config = Mock()
resources = [
Mock(path='/foo'),
Expand All @@ -62,6 +66,21 @@ def test_generate_server_call_per_path(
assert mock_get.call_count == 1
mock_gen.assert_called_once_with(config, resources[0], mock_get())

@patch.object(generators, 'is_ignored_resource')
@patch.object(generators, '_get_nefertari_parent_resource')
@patch.object(generators, 'generate_resource')
def test_generate_server_ignored_resources(
self, mock_gen, mock_get, mock_check):
mock_check.return_value = True
config = Mock()
resources = [
Mock(path='/foo'),
Mock(path='/bar'),
]
generators.generate_server(Mock(resources=resources), config)
assert not mock_gen.called
assert not mock_get.called


@pytest.mark.usefixtures('engine_mock')
class TestGenerateModels(object):
Expand Down
40 changes: 40 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,43 @@ def test_patch_view_model(self):
assert view_cls.Model is model1
assert not model1.called
model2.assert_called_once_with()

def test_split_ignored_routes_no_val(self):
config = Mock()
config.registry.settings = {}
utils.split_ignored_routes(config)
assert config.registry.settings['ramses.ignored_routes'] == []

def test_split_ignored_routes(self):
config = Mock()
config.registry.settings = {
'ramses.ignored_routes': '/users/self,/stories/{id}'}
utils.split_ignored_routes(config)
assert set(config.registry.settings['ramses.ignored_routes']) == {
'/users/self', '/stories/{id}'}

def test_split_ignored_routes_newline_delim(self):
config = Mock()
config.registry.settings = {
'ramses.ignored_routes': '/users/self\n/stories/{id}'}
utils.split_ignored_routes(config)
assert set(config.registry.settings['ramses.ignored_routes']) == {
'/users/self', '/stories/{id}'}

def test_is_ignored_resource_true(self):
config = Mock()
config.registry.settings = {'ramses.ignored_routes': ['/users']}
raml_res = Mock(path='/users')
assert utils.is_ignored_resource(config, raml_res)

def test_is_ignored_resource_parent_true(self):
config = Mock()
config.registry.settings = {'ramses.ignored_routes': ['/users']}
raml_res = Mock(path='/users/{id}')
assert utils.is_ignored_resource(config, raml_res)

def test_is_ignored_resource_false(self):
config = Mock()
config.registry.settings = {'ramses.ignored_routes': ['/users']}
raml_res = Mock(path='/stories')
assert not utils.is_ignored_resource(config, raml_res)