Skip to content

Commit 39708b9

Browse files
SanjanaG-01faif
andauthored
Docs: Add ecosystem examples to CoR, add Factory tests (#466)
Co-authored-by: Sakis Kasampalis <faif@users.noreply.github.com>
1 parent de0e3f8 commit 39708b9

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

patterns/behavioral/chain_of_responsibility.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
As a variation some receivers may be capable of sending requests out
1515
in several directions, forming a `tree of responsibility`.
1616
17+
*Examples in Python ecosystem:
18+
Django Middleware: https://docs.djangoproject.com/en/stable/topics/http/middleware/
19+
The middleware components act as a chain where each processes the request/response.
20+
1721
*TL;DR
1822
Allow a request to pass down a chain of receivers until it is handled.
1923
"""

tests/creational/test_factory.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import unittest
2+
from patterns.creational.factory import get_localizer, GreekLocalizer, EnglishLocalizer
3+
4+
class TestFactory(unittest.TestCase):
5+
def test_get_localizer_greek(self):
6+
localizer = get_localizer("Greek")
7+
self.assertIsInstance(localizer, GreekLocalizer)
8+
self.assertEqual(localizer.localize("dog"), "σκύλος")
9+
self.assertEqual(localizer.localize("cat"), "γάτα")
10+
# Test unknown word returns the word itself
11+
self.assertEqual(localizer.localize("monkey"), "monkey")
12+
13+
def test_get_localizer_english(self):
14+
localizer = get_localizer("English")
15+
self.assertIsInstance(localizer, EnglishLocalizer)
16+
self.assertEqual(localizer.localize("dog"), "dog")
17+
self.assertEqual(localizer.localize("cat"), "cat")
18+
19+
def test_get_localizer_default(self):
20+
# Test default argument
21+
localizer = get_localizer()
22+
self.assertIsInstance(localizer, EnglishLocalizer)
23+
24+
def test_get_localizer_unknown_language(self):
25+
# Test fallback for unknown language if applicable,
26+
# or just verify what happens.
27+
# Based on implementation: localizers.get(language, EnglishLocalizer)()
28+
# It defaults to EnglishLocalizer for unknown keys.
29+
localizer = get_localizer("Spanish")
30+
self.assertIsInstance(localizer, EnglishLocalizer)

0 commit comments

Comments
 (0)