Coverage for src/mafw/processor/utils.py: 100%

12 statements  

« prev     ^ index     » next       coverage.py v7.15.0, created at 2026-07-26 09:13 +0000

1# Copyright 2025–2026 European Union 

2# Author: Bulgheroni Antonio (antonio.bulgheroni@ec.europa.eu) 

3# SPDX-License-Identifier: EUPL-1.2 

4""" 

5Standalone utility helpers for the Processor package. 

6 

7This module provides validation helpers that are used across the processor 

8sub-package but do not belong to any specific class. Currently it contains 

9:func:`validate_database_conf`, which checks that a steering-file database 

10section carries the minimum required fields before a database connection 

11is attempted. 

12 

13.. versionadded:: 2.3 

14 Extracted from the monolithic ``processor.py`` module for improved 

15 maintainability and focused testing. 

16""" 

17 

18from __future__ import annotations 

19 

20from typing import Any 

21 

22 

23def validate_database_conf(database_conf: dict[str, Any] | None = None) -> dict[str, Any] | None: 

24 """ 

25 Validate the database configuration dictionary. 

26 

27 This function is called during processor initialisation to ensure that 

28 the supplied database configuration contains the minimum required fields 

29 (currently just ``URL``). If the configuration is a full steering file, 

30 the ``DBConfiguration`` section is extracted automatically. 

31 

32 :param database_conf: The input database configuration. Defaults to None. 

33 :type database_conf: dict, Optional 

34 :return: Either the validated database configuration or None if it is invalid. 

35 :rtype: dict, None 

36 """ 

37 if database_conf is None: 

38 return None 

39 

40 # dict is mutable — work on a shallow copy to avoid side effects on the caller's data. 

41 conf = database_conf.copy() 

42 

43 if 'DBConfiguration' in conf: 

44 # The caller passed the entire steering file; extract just the DB section. 

45 conf = conf['DBConfiguration'] 

46 

47 # A valid database configuration must contain at least a connection URL. 

48 required_fields = ['URL'] 

49 if all(field in conf for field in required_fields): 

50 return database_conf 

51 else: 

52 return None