File: test_configuration.py

package info (click to toggle)
pywps 4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,016 kB
  • sloc: python: 8,846; xml: 723; makefile: 106
file content (64 lines) | stat: -rw-r--r-- 2,428 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
##################################################################
# Copyright 2018 Open Source Geospatial Foundation and others    #
# licensed under MIT, Please consult LICENSE.txt for details     #
##################################################################

"""Tests for the configuration."""

from basic import TestBase
import os

import random

from pywps import configuration


class TestEnvInterpolation(TestBase):
    """Test cases for env variable interpolation within the configuration."""
    test_value = "SOME_RANDOM_VALUE"

    def setUp(self) -> None:
        super().setUp()
        # Generate an unused environment key
        self.test_key = "SOME_RANDOM_KEY"
        while self.test_key in os.environ:
            self.test_key = "".join(random.choices("ABCDEFGHIJKLMNOPQRSTUVWXYZ", k=32))
        os.environ[self.test_key] = self.test_value

    def tearDown(self) -> None:
        del os.environ[self.test_key]
        super().tearDown()

    def test_expand_user(self):
        """Ensure we can parse a value with the $USER entry."""
        configuration.CONFIG.read_string(f"[envinterpolationsection]\nuser=${self.test_key}")
        assert self.test_value == configuration.CONFIG["envinterpolationsection"]["user"]

    def test_expand_user_with_some_text(self):
        """Ensure we can parse a value with the $USER entry and some more text."""
        new_user = "new_" + self.test_value
        configuration.CONFIG.read_string(f"[envinterpolationsection]\nuser=new_${{{self.test_key}}}")
        assert new_user == configuration.CONFIG["envinterpolationsection"]["user"]

    def test_dont_expand_value_without_env_variable(self):
        """
        Ensure we don't expand values that are no env variables.

        Could be an important case for existing config keys that need to
        contain the $symbol.
        """
        key = "$example_key_that_hopefully_will_never_be_a_real_env_variable"
        configuration.CONFIG.read_string("[envinterpolationsection]\nuser=" + key)
        assert key == configuration.CONFIG["envinterpolationsection"]["user"]


def load_tests(loader=None, tests=None, pattern=None):
    """Load the tests and return the test suite for this file."""
    import unittest

    if not loader:
        loader = unittest.TestLoader()
    suite_list = [
        loader.loadTestsFromTestCase(TestEnvInterpolation),
    ]
    return unittest.TestSuite(suite_list)