Coverage for base.py: 97%

1#!/usr/bin/env python

2# -*- coding: utf-8 -*-

3"""

4:Purpose: This module provides the superclass which is to be inherited

5 by the test-specific modules.

6

7:Platform: Linux/Windows | Python 3.6+

8:Developer: J Berendt

9:Email: development@s3dev.uk

10

11:Reminder: The testing suite must **not** be deployed into production

12 as it contains sensitive information for the development

13 environment.

14

15:Example:

16 Example code use::

17

18 # Run all tests via the shell script.

19 ./run.sh

20

21 # Run all tests using unittest.

22 python -m unittest discover

23

24 # Run a single test.

25 python -m unittest test_search.py

26

27"""

28# pylint: disable=wrong-import-position

29

30 import hashlib

31 import os

32 import re

33 import sys

34 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

35 import unittest

36 import warnings

37

38

39 class TestBase(unittest.TestCase):

40 """Private generalised base-testing class.

41

42 This class is designed to be inherited by each test-specific class.

43

44 """

45

46 _DIR_ROOT = os.path.realpath(os.path.dirname(__file__))

47 _DIR_RESC = os.path.join(_DIR_ROOT, 'resources')

48 _DIR_DATA = os.path.join(_DIR_RESC, 'data')

49 _DIR_FILES = os.path.join(_DIR_RESC, 'files')

50 _DIR_FILES_MD = os.path.join(_DIR_FILES, 'md')

51

52 @classmethod

53 def setUpClass(cls):

54 """Setup the testing class once, for all tests."""

55

56 @classmethod

57 def tearDownClass(cls):

58 """Teardown the testing class once all tests are complete."""

59

60 @staticmethod

61 def get_checksum(path: str, remove_data_run_id: bool=False) -> str:

62 """Calculate the MD5 checksum for a given file.

63

64 Args:

65 path (str): Full path to the file to be checksummed.

66 remove_data_run_id (bool, optional): When the GitHub API math

67 renderer is used, a ``data_run_id`` tag is placed in the

68 HTML which includes a dynamic hash string. This must be

69 removed for checksum comparisons. Defaults to False.

70

71 Returns:

72 str: A string containing the MD5 checksum for the given file.

73

74 """

75 with open(path, 'r', encoding='utf-8') as f:

76 content = f.read()

77 if remove_data_run_id:

78 # beautifulsoup is not used to keep dependencies to a minimum.

79 content = re.sub('data-run-id="(.*)"', 'data-run-id="[REMOVED]"', content)

80 return hashlib.md5(content.encode()).hexdigest()

81

82 @classmethod

83 def ignore_warnings(cls):

84 """Ignore (spurious) warnings for methods under test."""

85

86 # def read_pickle(self, module: str, method: str) -> object:

87 # """Read the named pickle file.

88

89 # Args:

90 # module (str): Name of the module (as this is the path).

91 # method (str): Name of the caller method (as this is the filename).

92

93 # Returns:

94 # object: An object containing the contents of the serialised

95 # file.

96

97 # """

98 # path = os.path.join(self._DIR_DATA, module, f'{method}.p')

99 # with open(path, 'rb') as f:

100 # data = pickle.load(f)

101 # return data

102

103 @classmethod

104 def reset_warnings(cls):

105 """Reset warnings which have been ignored."""

106 warnings.resetwarnings()