-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added tests for sequence helpers module
- Loading branch information
davyd_davyd
committed
May 17, 2024
1 parent
6a31814
commit 1fe45b8
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from unittest import TestCase | ||
|
||
from dddesign.utils.sequence_helpers import get_safe_element | ||
|
||
|
||
class TestGetSafeElement(TestCase): | ||
def test_valid_index(self): | ||
self.assertEqual(get_safe_element(('a', 'b', 'c'), 0), 'a') | ||
self.assertEqual(get_safe_element(['a', 'b', 'c'], 1), 'b') | ||
self.assertEqual(get_safe_element('abc', 2), 'c') | ||
|
||
def test_index_out_of_range(self): | ||
self.assertIsNone(get_safe_element(('a', 'b', 'c'), -4)) | ||
self.assertIsNone(get_safe_element(['a', 'b', 'c'], 5)) | ||
self.assertIsNone(get_safe_element('abc', 10)) | ||
|
||
def test_empty_sequence(self): | ||
self.assertIsNone(get_safe_element([], 0)) | ||
self.assertIsNone(get_safe_element((), 0)) | ||
self.assertIsNone(get_safe_element('', 0)) | ||
|
||
def test_negative_index(self): | ||
self.assertEqual(get_safe_element(('a', 'b', 'c'), -1), 'c') | ||
self.assertEqual(get_safe_element(['a', 'b', 'c'], -2), 'b') | ||
self.assertEqual(get_safe_element('abc', -3), 'a') | ||
|
||
def test_non_sequence(self): | ||
with self.assertRaises(TypeError): | ||
get_safe_element(123, 1) | ||
with self.assertRaises(TypeError): | ||
get_safe_element(None, 0) |