Skip to content

Commit

Permalink
Create test_xcvr_api_factory.py
Browse files Browse the repository at this point in the history
Add pytest for xcvc_api_factory file
  • Loading branch information
AnoopKamath authored Oct 5, 2023
1 parent 3005ba5 commit f36de02
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/sonic_xcvr/test_xcvr_api_factory.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit f36de02

Please sign in to comment.