From 73e418362efad93f76cbbff21a26bf7956a80592 Mon Sep 17 00:00:00 2001 From: Marcos Prieto Date: Wed, 5 Jul 2023 09:04:33 +0200 Subject: [PATCH] Remove the unused view predicates infrastructure --- lms/views/__init__.py | 1 - lms/views/predicates.py | 52 ----------------------- tests/unit/lms/views/predicates_test.py | 55 ------------------------- 3 files changed, 108 deletions(-) delete mode 100644 lms/views/predicates.py delete mode 100644 tests/unit/lms/views/predicates_test.py diff --git a/lms/views/__init__.py b/lms/views/__init__.py index fcb20eb25f..8c7b0b5c0c 100644 --- a/lms/views/__init__.py +++ b/lms/views/__init__.py @@ -1,3 +1,2 @@ def includeme(config): # pragma: no cover config.scan(__name__) - config.include("lms.views.predicates") diff --git a/lms/views/predicates.py b/lms/views/predicates.py deleted file mode 100644 index 41a6fe9563..0000000000 --- a/lms/views/predicates.py +++ /dev/null @@ -1,52 +0,0 @@ -""" -Custom Pyramid view predicates for the Basic LTI Launch view. - -See: -https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/hooks.html#view-and-route-predicates -""" - -from dataclasses import dataclass -from functools import partial -from typing import Any - -from lms.views.lti.basic_launch import has_document_url - - -@dataclass -class Predicate: - """A predicate which wraps a simple function and compares it to a value.""" - - value: Any - info: Any - name: str - comparison: callable - - def text(self): - """Get a string describing the behavior of this predicate.""" - - # Used by Pyramid in error messages. - return f"{self.name} = {self.value}" - - def phash(self): - """Get a string uniquely identifying the name and value.""" - - # Used by Pyramid for view configuration constraints handling. - return self.text() - - def __call__(self, context, request): - """Get whether our comparison matches our configuration.""" - - value = self.comparison(context, request) - - # If you set a predicate to None, Pyramid disables it, so to match this - # we will accept matches on anything falsy as well as matching values. - return (value == self.value) or (not value and not self.value) - - -def includeme(config): - config.add_view_predicate( - name="has_document_url", - factory=partial( - Predicate, name="has_document_url", comparison=has_document_url - ), - ) diff --git a/tests/unit/lms/views/predicates_test.py b/tests/unit/lms/views/predicates_test.py deleted file mode 100644 index c1f4fea642..0000000000 --- a/tests/unit/lms/views/predicates_test.py +++ /dev/null @@ -1,55 +0,0 @@ -from functools import partial -from unittest.mock import Mock, call, sentinel - -import pytest -from h_matchers import Any - -from lms.views.lti.basic_launch import has_document_url -from lms.views.predicates import Predicate, includeme - - -class TestPredicate: - def test_text(self, predicate): - assert predicate.text() == "name = sentinel.value" - - def test_phash(self, predicate): - assert predicate.phash() == "name = sentinel.value" - - @pytest.mark.parametrize("value", (True, False)) - @pytest.mark.parametrize("comparison_return", (True, False)) - def test__call__(self, predicate, value, comparison_return): - predicate.value = value - predicate.comparison.return_value = comparison_return - - result = predicate(sentinel.context, sentinel.request) - - predicate.comparison.assert_called_once_with(sentinel.context, sentinel.request) - assert result == (value == comparison_return) - - def test__call__matches_None_with_False(self, predicate): - predicate.value = False - predicate.comparison.return_value = None - - assert predicate(sentinel.context, sentinel.request) - - @pytest.fixture - def predicate(self): - return Predicate( - value=sentinel.value, info=sentinel.info, name="name", comparison=Mock() - ) - - -def test_includeme(): - config = Mock(spec_set=["add_view_predicate"]) - - includeme(config) - - predicate_partial = Any.object.of_type(partial).with_attrs( - { - "func": Predicate, - "keywords": {"name": "has_document_url", "comparison": has_document_url}, - } - ) - config.add_view_predicate.assert_has_calls( - [call(name="has_document_url", factory=predicate_partial)] - )