Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 3 additions & 29 deletions sonic-thermalctld/tests/mock_swsscommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,7 @@
Mock implementation of swsscommon package for unit testing
'''

STATE_DB = ''
CHASSIS_STATE_DB = ''
from .mocked_libs.swsscommon import swsscommon
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these 3 lines of code directly to test_thermalctld.py as the file size is reduced now



class Table:
def __init__(self, db, table_name):
self.table_name = table_name
self.mock_dict = {}

def _del(self, key):
del self.mock_dict[key]
pass

def set(self, key, fvs):
self.mock_dict[key] = fvs.fv_dict
pass

def get(self, key):
if key in self.mock_dict:
return self.mock_dict[key]
return None

def get_size(self):
return (len(self.mock_dict))


class FieldValuePairs:
def __init__(self, fvs):
self.fv_dict = dict(fvs)
pass
Table = swsscommon.Table
FieldValuePairs = swsscommon.FieldValuePairs
4 changes: 4 additions & 0 deletions sonic-thermalctld/tests/mocked_libs/swsscommon/swsscommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from swsssdk import ConfigDBConnector, SonicDBConfig, SonicV2Connector

STATE_DB = ''
CHASSIS_STATE_DB = ''


class Table:
Expand All @@ -27,6 +28,9 @@ def get(self, key):

def get_size(self):
return (len(self.mock_dict))

Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace on line 31 should be removed to maintain code cleanliness.

Suggested change

Copilot uses AI. Check for mistakes.
def getKeys(self):
return list(self.mock_dict.keys())


class FieldValuePairs:
Expand Down
19 changes: 13 additions & 6 deletions sonic-thermalctld/tests/test_thermalctld.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import os
import sys
import multiprocessing
from imp import load_source # TODO: Replace with importlib once we no longer need to support Python 2
import importlib.machinery
import importlib.util

from unittest import mock

# TODO: Clean this up once we no longer need to support Python 2
if sys.version_info.major == 3:
from unittest import mock
else:
import mock

import pytest
tests_path = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -34,6 +32,15 @@
scripts_path = os.path.join(modules_path, 'scripts')
sys.path.insert(0, modules_path)

# Replacement for imp.load_source from the Python3.12 docs:
# https://docs.python.org/3/whatsnew/3.12.html#imp
def load_source(modname, filename):
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
sys.modules[module.__name__] = module
loader.exec_module(module)
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The load_source function does not return the loaded module. Add return module at the end of the function, as the original imp.load_source returned the loaded module object.

Suggested change
loader.exec_module(module)
loader.exec_module(module)
return module

Copilot uses AI. Check for mistakes.

load_source('thermalctld', os.path.join(scripts_path, 'thermalctld'))
import thermalctld

Expand Down
Loading