TestRecursiveGet   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A test_retrieves_none_in_failing_nested() 0 2 1
A test_retrieves_nested_key() 0 2 1
A test_retrieves_none_in_unexisting_key() 0 2 1
A call_and_assert_equal() 0 3 1
A test_retrieves_non_nested_key() 0 2 1
1
import unittest
2
from . import helpers as test_helpers
3
4
from couchdb_download_token import helpers
5
6
7
class TestGetDownloadToken(unittest.TestCase):
8
9
    def test_retrieves_existing_token(self):
10
        # Mock database and document retrieving
11
        result = helpers.get_download_token({"download_token": "abc123"})
12
        self.assertEqual(result, 'abc123')
13
14
    def test_returns_none_with_non_existing_token(self):
15
        # Mock database and document retrieving
16
        result = helpers.get_download_token({})
17
        self.assertIsNone(result)
18
19
    @test_helpers.mock_config('couchdb_download_token.helpers.config',
20
                              DOWNLOAD_TOKEN_KEY_NAME='token')
21
    def test_can_change_key_name_in_config(self):
22
        result = helpers.get_download_token({"token": "abc123"})
23
        self.assertEqual(result, 'abc123')
24
25
    @test_helpers.mock_config('couchdb_download_token.helpers.config',
26
                              DOWNLOAD_TOKEN_KEY_NAME='data.download_token')
27
    def test_config_with_nested_keys(self):
28
        result = helpers.get_download_token({"data":{"download_token": "123"}})
29
        self.assertEqual(result, '123')
30
31
32
class TestRecursiveGet(unittest.TestCase):
33
34
    def call_and_assert_equal(self, dict_, key, expected):
35
        result = helpers.recursive_get(dict_, key)
36
        self.assertEqual(result, expected)
37
38
    def test_retrieves_non_nested_key(self):
39
        self.call_and_assert_equal({"foo": "bar"}, "foo", "bar")
40
41
    def test_retrieves_nested_key(self):
42
        self.call_and_assert_equal({"a": {"b": {"c": "d"}}}, "a.b.c", "d")
43
44
    def test_retrieves_none_in_unexisting_key(self):
45
        self.call_and_assert_equal({"a": {"b": {"x": "d"}}}, "a.b.c", None)
46
47
    def test_retrieves_none_in_failing_nested(self):
48
        self.call_and_assert_equal({"a": {"b": 5}}, "a.b.c", None)
49
50