first commit
This commit is contained in:
0
test/validators/__init__.py
Normal file
0
test/validators/__init__.py
Normal file
96
test/validators/csv_input_validator_test.py
Normal file
96
test/validators/csv_input_validator_test.py
Normal file
@@ -0,0 +1,96 @@
|
||||
import unittest
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from nose.tools import eq_
|
||||
|
||||
from helix.validators.csv_input_validator import CsvInputValidator
|
||||
from helix.constants.system_type import SystemType
|
||||
from helix.constants.file_validation_error import FileValidationError, FileValidationMessage
|
||||
|
||||
|
||||
class CsvInputValidatorTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.user_values = MagicMock()
|
||||
self.subject = CsvInputValidator(self.user_values)
|
||||
|
||||
def should_have_error(self, result, message, row):
|
||||
eq_(result, FileValidationError(message, row))
|
||||
|
||||
def test_reports_single_tilt_file_is_invalid_when_dual_tilt(self):
|
||||
self.user_values.system_type.return_value = SystemType.dualTilt
|
||||
with open('test/fixtures/input_single_tilt.csv', 'r', newline='') as csv_file:
|
||||
cad_input = csv_file.read()
|
||||
self.should_have_error(self.subject.validate(cad_input), FileValidationMessage.DualTiltWindZone, 1)
|
||||
|
||||
def test_reports_single_tilt_file_is_valid_when_single_tilt(self):
|
||||
self.user_values.system_type.return_value = SystemType.singleTilt
|
||||
with open('test/fixtures/input_single_tilt.csv', 'r', newline='') as csv_file:
|
||||
cad_input = csv_file.read()
|
||||
eq_(self.subject.validate(cad_input), None)
|
||||
|
||||
def test_reports_dual_tilt_file_is_valid_when_dual_tilt(self):
|
||||
self.user_values.system_type.return_value = SystemType.dualTilt
|
||||
with open('test/fixtures/input_dual_tilt.csv', 'r', newline='') as csv_file:
|
||||
cad_input = csv_file.read()
|
||||
eq_(self.subject.validate(cad_input), None)
|
||||
|
||||
def test_reports_dual_tilt_file_is_valid_when_single_tilt(self): # think about it.
|
||||
self.user_values.system_type.return_value = SystemType.singleTilt
|
||||
with open('test/fixtures/input_dual_tilt.csv', 'r', newline='') as csv_file:
|
||||
cad_input = csv_file.read()
|
||||
eq_(self.subject.validate(cad_input), None)
|
||||
|
||||
def test_reports_invalid_wind_zones_when_single_tilt(self):
|
||||
self.user_values.system_type.return_value = SystemType.singleTilt
|
||||
with open('test/fixtures/invalid_wind_zone.csv', 'r', newline='') as csv_file:
|
||||
cad_input = csv_file.read()
|
||||
self.should_have_error(self.subject.validate(cad_input), FileValidationMessage.SingleTiltWindZone, 8)
|
||||
|
||||
def test_reports_not_csv_files_as_invalid(self):
|
||||
self.should_have_error(self.subject.validate("What a Terrible File"), FileValidationMessage.InvalidHeaders, 0)
|
||||
self.should_have_error(self.subject.validate("What a Terrible File\r\nmalformed data!\r\n"), FileValidationMessage.InvalidHeaders, 0)
|
||||
|
||||
def test_reports_no_rows_as_invalid(self):
|
||||
self.should_have_error(self.subject.validate("A\tB\tC\tD\tE\t\r\n"), FileValidationMessage.InvalidRowCount, 0)
|
||||
|
||||
def test_reports_wrong_number_of_columns_in_a_row(self):
|
||||
self.user_values.system_type.return_value = SystemType.dualTilt
|
||||
with open('test/fixtures/invalid_coordinates.csv', 'r', newline='') as csv_file:
|
||||
cad_input = csv_file.read()
|
||||
self.should_have_error(self.subject.validate(cad_input), FileValidationMessage.Generic, 1)
|
||||
|
||||
def test_reports_invalid_panel_types_when_finds_panel_type_less_than_1(self):
|
||||
self.user_values.system_type.return_value = SystemType.singleTilt
|
||||
with open('test/fixtures/invalid_panel_types_not_in_1-4.csv', 'r', newline='') as csv_file:
|
||||
cad_input = csv_file.read()
|
||||
self.should_have_error(self.subject.validate(cad_input), FileValidationMessage.PanelTypeOutOfBounds, 13)
|
||||
|
||||
def test_reports_invalid_panel_types_when_finds_panel_type_greater_than_4(self):
|
||||
self.user_values.system_type.return_value = SystemType.singleTilt
|
||||
with open('test/fixtures/invalid_panel_types_more_than_4.csv', 'r', newline='') as csv_file:
|
||||
cad_input = csv_file.read()
|
||||
self.should_have_error(self.subject.validate(cad_input), FileValidationMessage.PanelTypeOutOfBounds, 14)
|
||||
|
||||
def test_reports_invalid_panel_types_when_finds_less_than_4_corner_panel_types_per_subarray_in_single_tilt(self):
|
||||
self.user_values.system_type.return_value = SystemType.singleTilt
|
||||
with open('test/fixtures/invalid_panel_types_less_than_4_corner_panels.csv', 'r', newline='') as csv_file:
|
||||
cad_input = csv_file.read()
|
||||
self.should_have_error(self.subject.validate(cad_input), FileValidationMessage.PanelTypeTooFewCornersSingleTilt, None)
|
||||
|
||||
def test_reports_invalid_panel_types_when_finds_less_than_2_corner_panel_types_per_subarray_in_dual_tilt(self):
|
||||
self.user_values.system_type.return_value = SystemType.dualTilt
|
||||
with open('test/fixtures/invalid_dual_tilt_panel_types_less_than_2_corner_panels.csv', 'r', newline='') as csv_file:
|
||||
cad_input = csv_file.read()
|
||||
self.should_have_error(self.subject.validate(cad_input), FileValidationMessage.PanelTypeTooFewCornersDualTilt, None)
|
||||
|
||||
def test_reports_valid_when_finds_at_least_2_corner_panels_per_subarray_in_dual_tilt(self):
|
||||
self.user_values.system_type.return_value = SystemType.dualTilt
|
||||
with open('test/fixtures/input_small_dual_tilt.csv', 'r',
|
||||
newline='') as csv_file:
|
||||
cad_input = csv_file.read()
|
||||
eq_(self.subject.validate(cad_input), None)
|
||||
|
||||
def test_reports_invalid_panel_types_when_finds_no_corner_panel_types_in_subarray(self):
|
||||
self.user_values.system_type.return_value = SystemType.dualTilt
|
||||
cad_input = "A\tB\tC\tD\tE\r\nhandle\tblock\tA\t3\t1\r\n"
|
||||
self.should_have_error(self.subject.validate(cad_input), FileValidationMessage.PanelTypeTooFewCornersDualTilt, None)
|
||||
39
test/validators/dxf_layer_validator_test.py
Normal file
39
test/validators/dxf_layer_validator_test.py
Normal file
@@ -0,0 +1,39 @@
|
||||
'''
|
||||
Created on Mar 23, 2017
|
||||
|
||||
@author: jvazquez
|
||||
'''
|
||||
import io
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
import dxfgrabber
|
||||
|
||||
from helix.Services.dxf_helper import DXFHelper
|
||||
from helix.models.dxf.dxf_error import OldDxfFormatException
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
|
||||
class DxfLayerValidatorTest(unittest.TestCase):
|
||||
def test_finds_new_format(self):
|
||||
with open('test/fixtures/dxf/input_dual_tilt_96_cell.dxf',
|
||||
'r', newline='') as file:
|
||||
dxf_file_contents = file.read()
|
||||
dxf = dxfgrabber.read(io.StringIO(dxf_file_contents, newline=None))
|
||||
dxf_helper = DXFHelper()
|
||||
dxf_helper.build_polygons(dxf.entities)
|
||||
is_new_format = dxf_helper.is_new_aurora_format()
|
||||
msg = "Expected new format true, got {}"\
|
||||
.format(dxf_helper.is_new_aurora_format())
|
||||
self.assertTrue(is_new_format, msg)
|
||||
|
||||
def test_finds_old_format(self):
|
||||
with open('test/fixtures/dxf/input_old_dual_tilt_96_cell.dxf',
|
||||
'r', newline='') as file:
|
||||
dxf_file_contents = file.read()
|
||||
dxf = dxfgrabber.read(io.StringIO(dxf_file_contents, newline=None))
|
||||
dxf_helper = DXFHelper()
|
||||
self.assertRaises(OldDxfFormatException,
|
||||
dxf_helper.build_polygons,
|
||||
dxf.entities)
|
||||
102
test/validators/file_validator_test.py
Normal file
102
test/validators/file_validator_test.py
Normal file
@@ -0,0 +1,102 @@
|
||||
from os.path import abspath, dirname, join
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, PropertyMock
|
||||
|
||||
from nose.tools import eq_
|
||||
from werkzeug.datastructures import FileStorage
|
||||
|
||||
from helix.constants.file_validation_error import FileValidationError\
|
||||
, FileValidationMessage
|
||||
from helix.constants.system_type import SystemType
|
||||
from helix.validators.file_validator import FileValidator, FileType, \
|
||||
InvalidMappingException
|
||||
|
||||
|
||||
class FileValidatorTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.user_values = MagicMock()
|
||||
self.subject = FileValidator(self.user_values)
|
||||
|
||||
def should_have_error(self, result, message, row):
|
||||
eq_(result, FileValidationError(message, row))
|
||||
|
||||
def test_csv_files_are_valid(self):
|
||||
fake_file = MagicMock()
|
||||
type(fake_file).filename = \
|
||||
PropertyMock(return_value="input_single_tilt.csv")
|
||||
self.user_values.system_type.return_value = SystemType.singleTilt
|
||||
with open('test/fixtures/input_single_tilt.csv', 'r',
|
||||
newline='') as file:
|
||||
cad_input = file.read()
|
||||
eq_(self.subject.validate(cad_input, fake_file, FileType.Csv), None)
|
||||
|
||||
def test_unknown_files_are_invalid(self):
|
||||
fake_file = MagicMock()
|
||||
type(fake_file).filename = PropertyMock(return_value="Hi")
|
||||
result = self.subject.validate("Hi", fake_file, FileType.Unknown)
|
||||
self.should_have_error(result,
|
||||
FileValidationMessage.UnknownFileUploaded, None)
|
||||
|
||||
def test_identifies_dxf_files_as_aurora_dxf(self):
|
||||
with open('test/fixtures/dxf/input_single_tilt_96_cell.dxf', 'r',
|
||||
newline='') as file:
|
||||
dxf_input = file.read()
|
||||
eq_(self.subject.identify_file_type(dxf_input), FileType.AuroraDxf)
|
||||
|
||||
def test_identifies_csv_files_as_csv(self):
|
||||
fake_file = MagicMock()
|
||||
fake_file.filename.return_value = "input_single_tilt.csv"
|
||||
|
||||
with open('test/fixtures/input_single_tilt.csv', 'r',
|
||||
newline='') as file:
|
||||
cad_input = file.read()
|
||||
eq_(self.subject.identify_file_type(cad_input), FileType.Csv)
|
||||
|
||||
def test_identifies_other_files_as_unknown(self):
|
||||
eq_(self.subject.identify_file_type("hello"), FileType.Unknown)
|
||||
|
||||
def test_read_invalid_file_is_captured(self):
|
||||
fname = join(abspath(dirname(__file__)),
|
||||
'../fixtures/images/expected_dual_tilt_pseries_image.png')
|
||||
fake_file = MagicMock()
|
||||
fake_file.read.return_value = open(fname, "rb").read()
|
||||
fake_file.filename.return_value = "expected_dual_tilt_pseries_image.png"
|
||||
stream = self.subject.obtain_stream(fake_file)
|
||||
self.should_have_error(self.subject.validate(stream, fake_file,
|
||||
FileType.AuroraDxf),
|
||||
FileValidationMessage.ExpectedDxfFile,
|
||||
None)
|
||||
|
||||
def test_read_file_passes(self):
|
||||
fname = join(abspath(dirname(__file__)),
|
||||
"../fixtures/expected_design_metadata.txt")
|
||||
stream = self.subject.obtain_stream(FileStorage(open(fname, "rb")))
|
||||
self.assertGreater(len(stream), 0)
|
||||
|
||||
def test_txt_validator_rejects_dxf_file(self):
|
||||
file_type = FileType.Csv
|
||||
self.assertRaises(InvalidMappingException,
|
||||
file_type.valid_mapping, "dxf")
|
||||
|
||||
def test_dxf_validator_rejects_txt_file(self):
|
||||
file_type = FileType.AuroraDxf
|
||||
self.assertRaises(InvalidMappingException,
|
||||
file_type.valid_mapping, "txt")
|
||||
self.assertRaises(InvalidMappingException,
|
||||
file_type.valid_mapping, "csv")
|
||||
self.assertRaises(InvalidMappingException,
|
||||
file_type.valid_mapping, "gif")
|
||||
|
||||
def test_unknown_validator_rejects_any_file(self):
|
||||
file_type = FileType.Unknown
|
||||
self.assertRaises(InvalidMappingException,
|
||||
file_type.valid_mapping,
|
||||
"exe")
|
||||
|
||||
def test_file_validator_obtain_extension(self):
|
||||
fake_file = MagicMock()
|
||||
fname = "Feb1717DT-dxf.1487698302339.dxf"
|
||||
type(fake_file).filename = PropertyMock(return_value=fname)
|
||||
validator = FileValidator(self.user_values)
|
||||
result = validator.obtain_extension(fake_file)
|
||||
self.assertEqual("dxf", result)
|
||||
41
test/validators/seismic_anchor_validator_test.py
Normal file
41
test/validators/seismic_anchor_validator_test.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import unittest
|
||||
from unittest.mock import MagicMock
|
||||
from nose.tools import eq_
|
||||
from helix.constants.panel_type import PanelType
|
||||
from helix.constants.seismic_anchor_validation_error import SeismicAnchorValidationError
|
||||
from helix.models.panel import Panel
|
||||
from helix.models.subarray import Subarray
|
||||
from helix.validators.seismic_anchor_validator import SeismicAnchorValidator
|
||||
|
||||
|
||||
class SeismicAnchorValidatorTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.calculator = MagicMock()
|
||||
self.calculator.subarray_summary.return_value = [
|
||||
Subarray(subarray_number=0, required_seismic_anchors=1, weight=1000)
|
||||
]
|
||||
self.subject = SeismicAnchorValidator(self.calculator)
|
||||
|
||||
def test_seismic_anchor_below_needed_seismic_anchors_is_invalid(self):
|
||||
panels = [
|
||||
Panel(panel_type=PanelType.Corner, wind_anchors=0, pressure=11.31, subarray=0, seismic_anchors=0),
|
||||
Panel(panel_type=PanelType.Corner, wind_anchors=0, pressure=11.31, subarray=0, seismic_anchors=0),
|
||||
Panel(panel_type=PanelType.Corner, wind_anchors=1, pressure=7.37, subarray=0, seismic_anchors=0),
|
||||
Panel(panel_type=PanelType.NorthSouth, wind_anchors=0, pressure=10.00, subarray=0, seismic_anchors=0),
|
||||
Panel(panel_type=PanelType.Middle, wind_anchors=0, pressure=12.44, subarray=0, seismic_anchors=0),
|
||||
Panel(panel_type=PanelType.NorthSouth, wind_anchors=0, pressure=10.00, subarray=0, seismic_anchors=0),
|
||||
Panel(panel_type=PanelType.NorthSouth, wind_anchors=1, pressure=5.19, subarray=0, seismic_anchors=0)
|
||||
]
|
||||
eq_(self.subject.validate(panels), SeismicAnchorValidationError.TooFewAnchors)
|
||||
|
||||
def test_seismic_anchor_at_needed_seismic_anchors_is_valid(self):
|
||||
panels =[
|
||||
Panel(panel_type=PanelType.Corner, wind_anchors=0, pressure=11.31, subarray=0, seismic_anchors=1),
|
||||
Panel(panel_type=PanelType.Corner, wind_anchors=0, pressure=11.31, subarray=0, seismic_anchors=0),
|
||||
Panel(panel_type=PanelType.Corner, wind_anchors=1, pressure=7.37, subarray=0, seismic_anchors=0),
|
||||
Panel(panel_type=PanelType.NorthSouth, wind_anchors=0, pressure=10.00, subarray=0, seismic_anchors=0),
|
||||
Panel(panel_type=PanelType.Middle, wind_anchors=0, pressure=12.44, subarray=0, seismic_anchors=0),
|
||||
Panel(panel_type=PanelType.NorthSouth, wind_anchors=0, pressure=10.00, subarray=0, seismic_anchors=0),
|
||||
Panel(panel_type=PanelType.NorthSouth, wind_anchors=1, pressure=5.19, subarray=0, seismic_anchors=0)
|
||||
]
|
||||
eq_(self.subject.validate(panels), None)
|
||||
120
test/validators/subarray_validator_test.py
Normal file
120
test/validators/subarray_validator_test.py
Normal file
@@ -0,0 +1,120 @@
|
||||
import unittest
|
||||
from nose.tools import eq_
|
||||
from helix.Services.dxf_helper import DXFHelper
|
||||
from helix.constants.system_type import SystemType
|
||||
from helix.models.coordinate import Coordinate
|
||||
from helix.models.dxf.dxf_error import DXFError
|
||||
from helix.models.panel import Panel
|
||||
from helix.validators.subarray_validator import SubarrayValidator
|
||||
|
||||
|
||||
class SubarrayValidatorTest(unittest.TestCase):
|
||||
def test_subarrays_taller_than_150_feet_are_invalid(self):
|
||||
panels = []
|
||||
for i in range(33):
|
||||
panels.append(Panel(coordinate=Coordinate(0, i * 60.0, rotation=0), subarray=1))
|
||||
panels.append(Panel(coordinate=Coordinate(62.15, i * 60.0, rotation=0), subarray=1))
|
||||
node_graph = DXFHelper.build_node_graph(panels, (62.15, 60.0))
|
||||
|
||||
try:
|
||||
SubarrayValidator.validate_subarray(node_graph, 1, SystemType.singleTilt)
|
||||
assert False
|
||||
except DXFError as error:
|
||||
eq_(error.message, "Array size is too big. Max is 150' by 150'.")
|
||||
|
||||
def test_subarrays_wider_than_150_feet_are_invalid(self):
|
||||
panels = []
|
||||
for i in range(30):
|
||||
panels.append(Panel(coordinate=Coordinate(i * 62.15, 0, rotation=0), subarray=1))
|
||||
panels.append(Panel(coordinate=Coordinate(i * 62.15, 60.0, rotation=0), subarray=1))
|
||||
node_graph = DXFHelper.build_node_graph(panels, (62.15, 60))
|
||||
|
||||
try:
|
||||
SubarrayValidator.validate_subarray(node_graph, 1, None)
|
||||
assert False
|
||||
except DXFError as error:
|
||||
eq_(error.message, "Array size is too big. Max is 150' by 150'.")
|
||||
|
||||
def test_single_file_horizontal_line_is_invalid_single_tilt(self):
|
||||
panels = [
|
||||
Panel(coordinate=Coordinate(10, 0, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(20, 0, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(30, 0, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(30, 10, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(40, 0, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(40, 10, rotation=0), subarray=1)
|
||||
]
|
||||
node_graph = DXFHelper.build_node_graph(panels, (10, 10))
|
||||
|
||||
try:
|
||||
SubarrayValidator.validate_subarray(node_graph, 1, SystemType.singleTilt)
|
||||
assert False
|
||||
except DXFError as error:
|
||||
eq_(error.message, "Error - Unsupported panel configuration in subarray 1.")
|
||||
|
||||
def test_single_file_vertical_line_is_invalid_single_tilt(self):
|
||||
panels = [
|
||||
Panel(coordinate=Coordinate(0, 10, rotation=0), subarray=2),
|
||||
Panel(coordinate=Coordinate(0, 20, rotation=0), subarray=2),
|
||||
Panel(coordinate=Coordinate(0, 30, rotation=0), subarray=2),
|
||||
Panel(coordinate=Coordinate(10, 30, rotation=0), subarray=2),
|
||||
Panel(coordinate=Coordinate(0, 40, rotation=0), subarray=2),
|
||||
Panel(coordinate=Coordinate(10, 40, rotation=0), subarray=2)
|
||||
]
|
||||
node_graph = DXFHelper.build_node_graph(panels, (10, 10))
|
||||
|
||||
try:
|
||||
SubarrayValidator.validate_subarray(node_graph, 2, SystemType.singleTilt)
|
||||
assert False
|
||||
except DXFError as error:
|
||||
eq_(error.message, "Error - Unsupported panel configuration in subarray 2.")
|
||||
|
||||
def test_other_single_tilt_subarrays_are_valid(self):
|
||||
panels = [
|
||||
Panel(coordinate=Coordinate(0, 0, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(0, 1, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(1, 0, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(1, 1, rotation=0), subarray=1)
|
||||
]
|
||||
node_graph = DXFHelper.build_node_graph(panels, (1, 1))
|
||||
|
||||
SubarrayValidator.validate_subarray(node_graph, 1, SystemType.singleTilt)
|
||||
|
||||
def test_single_file_horizontal_line_is_invalid_dual_tilt(self):
|
||||
panels = [
|
||||
Panel(coordinate=Coordinate(10, 0, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(20, 0, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(30, 0, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(30, 10, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(40, 0, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(40, 10, rotation=0), subarray=1)
|
||||
]
|
||||
node_graph = DXFHelper.build_node_graph(panels, (10, 10))
|
||||
|
||||
try:
|
||||
SubarrayValidator.validate_subarray(node_graph, 1, SystemType.dualTilt)
|
||||
assert False
|
||||
except DXFError as error:
|
||||
eq_(error.message, "Error - Unsupported panel configuration in subarray 1.")
|
||||
|
||||
def test_single_file_vertical_line_is_valid_dual_tilt(self):
|
||||
panels = [
|
||||
Panel(coordinate=Coordinate(0, 1, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(0, 2, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(0, 3, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(1, 3, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(0, 4, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(1, 4, rotation=0), subarray=1)
|
||||
]
|
||||
node_graph = DXFHelper.build_node_graph(panels, (1, 1))
|
||||
|
||||
SubarrayValidator.validate_subarray(node_graph, 1, SystemType.dualTilt)
|
||||
|
||||
def test_other_dual_tilt_subarrays_are_valid(self):
|
||||
panels = [
|
||||
Panel(coordinate=Coordinate(0, 0, rotation=0), subarray=1),
|
||||
Panel(coordinate=Coordinate(0, 1, rotation=0), subarray=1)
|
||||
]
|
||||
node_graph = DXFHelper.build_node_graph(panels, (1, 1))
|
||||
|
||||
SubarrayValidator.validate_subarray(node_graph, 1, SystemType.dualTilt)
|
||||
Reference in New Issue
Block a user