From f36de029655d338aab004f054499ff22cc2bf015 Mon Sep 17 00:00:00 2001 From: Anoop Kamath <115578705+AnoopKamath@users.noreply.github.com> Date: Thu, 5 Oct 2023 01:39:20 -0700 Subject: [PATCH] Create test_xcvr_api_factory.py Add pytest for xcvc_api_factory file --- tests/sonic_xcvr/test_xcvr_api_factory.py | 52 +++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/sonic_xcvr/test_xcvr_api_factory.py diff --git a/tests/sonic_xcvr/test_xcvr_api_factory.py b/tests/sonic_xcvr/test_xcvr_api_factory.py new file mode 100644 index 000000000..0597fabb0 --- /dev/null +++ b/tests/sonic_xcvr/test_xcvr_api_factory.py @@ -0,0 +1,52 @@ +from unittest.mock import patch +from mock import MagicMock +from io import BytesIO +import pytest +import traceback +import random +from sonic_platform_base.sonic_xcvr.api.public.cmis import CmisApi +from sonic_platform_base.sonic_xcvr.api.credo.aec_800g import CmisAec800gApi +from sonic_platform_base.sonic_xcvr.mem_maps.public.cmis import CmisMemMap +from sonic_platform_base.sonic_xcvr.mem_maps.credo.aec_800g import CmisAec800gMemMap +from sonic_platform_base.sonic_xcvr.xcvr_eeprom import XcvrEeprom +from sonic_platform_base.sonic_xcvr.codes.public.cmis import CmisCodes +from sonic_platform_base.sonic_xcvr.codes.credo.aec_800g import CmisAec800gCodes +from sonic_platform_base.sonic_xcvr.codes.public.sff8024 import Sff8024 +from sonic_platform_base.sonic_xcvr.fields import consts +from sonic_platform_base.sonic_xcvr.xcvr_api_factory import XcvrApiFactory + +class BytesMock(bytes): + def decode(self, encoding='utf-8', errors='strict'): + return 'DecodedCredo' + +class TestXcvrApiFactory(object): + read_eeprom = MagicMock + write_eeprom = MagicMock + api = XcvrApiFactory(read_eeprom, write_eeprom) + + def test_get_vendor_name(self): + self.api.reader = MagicMock() + self.api.reader.return_value = b'Credo' + with patch.object(BytesMock, 'decode', return_value='DecodedCredo'): + result = self.api._get_vendor_name() + assert result == 'Credo'.strip() + + def test_get_vendor_part_num(self): + self.api.reader = MagicMock() + self.api.reader.return_value = b'CAC81X321M2MC1MS' + with patch.object(BytesMock, 'decode', return_value='DecodedCAC81X321M2MC1MS'): + result = self.api._get_vendor_part_num() + assert result == 'CAC81X321M2MC1MS'.strip() + + def test_create_xcvrd_api(self): + self._get_id = MagicMock() + self._get_id.side_effect = 0x18 + self._get_vendor_name= MagicMock() + self._get_vendor_name.side_effect = 'Credo' + self._get_vendor_part_num = MagicMock() + self._get_vendor_part_num.side_effect = 'CAC81X321M2MC1MS' + CmisAec800gCodes = MagicMock() + CmisAec800gMemMap = MagicMock() + XcvrEeprom = MagicMock() + CmisAec800gApi = MagicMock() + self.api.create_xcvr_api()