spyglass-plugin-xls/tests/unit/test_excel_parser.py
Ian H. Pittwood b0f7b952b4 Combines all exceptions into a single file
This change moves spyglass-plugin-xls's necessary exception definitions
locally into the spyglass-plugin-xls project and changes their base
to the SpyglassBaseException class. This change must be merged before
Spyglass's exceptions may be consolidated.

Related Change: https://review.opendev.org/#/c/667240/

Change-Id: I81c5ca2d9083aece3641bc8b5405dfd44baec810
2019-07-03 10:22:40 -05:00

169 lines
6.6 KiB
Python

# Copyright 2019 AT&T Intellectual Property. All other rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import unittest
from openpyxl import Workbook
from openpyxl.worksheet.worksheet import Worksheet
import pytest
from spyglass_plugin_xls.exceptions import NoSpecMatched
import yaml
from spyglass_plugin_xls.excel_parser import ExcelParser
FIXTURE_DIR = os.path.join(
os.path.dirname(os.path.dirname(__file__)), 'shared')
EXCEL_SPEC_PATH = os.path.join(FIXTURE_DIR, 'excel_spec.yaml')
INVALID_EXCEL_SPEC_PATH = os.path.join(FIXTURE_DIR, 'invalid_excel_spec.yaml')
EXCEL_FILE_PATH = os.path.join(FIXTURE_DIR, 'SiteDesignSpec_v0.1.xlsx')
SITE_CONFIG_PATH = os.path.join(FIXTURE_DIR, 'site_config.yaml')
@pytest.mark.usefixtures('site_data')
class TestExcelParser(unittest.TestCase):
"""Tests for ExcelParser"""
def test___init__(self):
with open(EXCEL_SPEC_PATH, 'r') as f:
loaded_spec = yaml.safe_load(f)
result = ExcelParser(EXCEL_FILE_PATH, EXCEL_SPEC_PATH)
self.assertEqual(EXCEL_FILE_PATH, result.file_name)
self.assertDictEqual(loaded_spec, result.excel_specs)
self.assertIsInstance(result.wb_combined, Workbook)
self.assertEqual('xl_spec', result.spec)
def test_sanitize(self):
test_string = 'Hello THIS is A TeSt'
expected_output = 'hellothisisatest'
result = ExcelParser.sanitize(test_string)
self.assertEqual(expected_output, result)
def test_compare(self):
test_string1 = 'These strings are equal.'
test_string2 = 'These strIngs are Equal .'
obj = ExcelParser(EXCEL_FILE_PATH, EXCEL_SPEC_PATH)
result = obj.compare(test_string1, test_string2)
self.assertTrue(result)
def test_compare_false(self):
test_string1 = 'These strings are not equal.'
test_string2 = 'These strIngs are Equal.'
obj = ExcelParser(EXCEL_FILE_PATH, EXCEL_SPEC_PATH)
result = obj.compare(test_string1, test_string2)
self.assertFalse(result)
def test_validate_sheet(self):
obj = ExcelParser(EXCEL_FILE_PATH, EXCEL_SPEC_PATH)
result = obj.validate_sheet('xl_spec', 'Site-Information')
self.assertTrue(result)
def test_validate_sheet_invalid(self):
obj = ExcelParser(EXCEL_FILE_PATH, INVALID_EXCEL_SPEC_PATH)
result = obj.validate_sheet('xl_spec', 'Site-Information')
self.assertFalse(result)
def test_find_correct_spec(self):
obj = ExcelParser(EXCEL_FILE_PATH, EXCEL_SPEC_PATH)
result = obj.find_correct_spec()
self.assertEqual('xl_spec', result)
def test_find_correct_spec_no_spec_matched(self):
obj = ExcelParser(EXCEL_FILE_PATH, INVALID_EXCEL_SPEC_PATH)
with self.assertRaises(NoSpecMatched):
obj.find_correct_spec()
def test__get_workbook(self):
obj = ExcelParser(EXCEL_FILE_PATH, EXCEL_SPEC_PATH)
result = obj._get_workbook()
self.assertIsInstance(result, Worksheet)
def test_get_ipmi_data(self):
expected_hosts = self.site_data['ipmi_data'][1]
expected_ipmi_data = self.site_data['ipmi_data'][0]
obj = ExcelParser(EXCEL_FILE_PATH, EXCEL_SPEC_PATH)
result = obj.get_ipmi_data()
self.assertDictEqual(result[0], expected_ipmi_data)
self.assertEqual(result[1], expected_hosts)
def test_get_private_vlan_data(self):
expected_vlan_data = {
'vlan 23': 'iSCSI/Storage',
'vlan 21': 'PXE',
'vlan 22': 'Calico BGP peering addresses',
'vlan 24': 'Overlay',
'n/a': 'CNI Pod addresses'
}
obj = ExcelParser(EXCEL_FILE_PATH, EXCEL_SPEC_PATH)
result = obj.get_private_vlan_data(obj._get_workbook())
self.assertDictEqual(expected_vlan_data, result)
def test_get_private_network_data(self):
expected_network_data = self.site_data['network_data']['private']
obj = ExcelParser(EXCEL_FILE_PATH, EXCEL_SPEC_PATH)
result = obj.get_private_network_data()
self.assertDictEqual(expected_network_data, result)
def test_get_public_network_data(self):
expected_network_data = self.site_data['network_data']['public']
obj = ExcelParser(EXCEL_FILE_PATH, EXCEL_SPEC_PATH)
result = obj.get_public_network_data()
self.assertEqual(expected_network_data, result)
def test_get_site_info(self):
expected_site_info = self.site_data['site_info']
obj = ExcelParser(EXCEL_FILE_PATH, EXCEL_SPEC_PATH)
result = obj.get_site_info()
self.assertDictEqual(expected_site_info, result)
def test_get_location_data(self):
expected_location_data = self.site_data['site_info']['location']
obj = ExcelParser(EXCEL_FILE_PATH, EXCEL_SPEC_PATH)
result = obj.get_location_data()
self.assertEqual(expected_location_data, result)
def test_validate_sheet_names_with_spec(self):
obj = ExcelParser(EXCEL_FILE_PATH, EXCEL_SPEC_PATH)
self.assertIsNone(obj.validate_sheet_names_with_spec())
def test_validate_sheet_names_with_spec_invalid(self):
obj = ExcelParser(EXCEL_FILE_PATH, INVALID_EXCEL_SPEC_PATH)
with self.assertRaises(RuntimeError):
obj.validate_sheet_names_with_spec()
def test_get_data(self):
expected_data = self.site_data
obj = ExcelParser(EXCEL_FILE_PATH, EXCEL_SPEC_PATH)
result = obj.get_data()
self.assertDictEqual(expected_data, result)
def test_load_excel_data(self):
result = ExcelParser.load_excel_data(EXCEL_FILE_PATH)
self.assertIsInstance(result, Workbook)
def test_get_xl_obj_and_sheetname(self):
result = ExcelParser.get_xl_obj_and_sheetname('Site-Information')
self.assertEqual([None, 'Site-Information'], result)
def test_get_xl_obj_and_sheetname_file_specified(self):
sheet = EXCEL_FILE_PATH + ':Site-Information'
result = ExcelParser.get_xl_obj_and_sheetname(sheet)
self.assertIsInstance(result, list)
self.assertIsInstance(result[0], Workbook)
self.assertEqual(result[1], 'Site-Information')