Skip to content

Commit

Permalink
Merge d19bc27 into 6dc9a66
Browse files Browse the repository at this point in the history
  • Loading branch information
macintoshpie committed Jun 2, 2020
2 parents 6dc9a66 + d19bc27 commit 3eda5de
Show file tree
Hide file tree
Showing 8 changed files with 576 additions and 46 deletions.
15 changes: 15 additions & 0 deletions seed/building_sync/tests/data/buildingsync_v2_0_bricr_workflow.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,7 @@
</auc:Modeled>
</auc:CalculationMethod>
<auc:AnnualSavingsSiteEnergy>0.0</auc:AnnualSavingsSiteEnergy>
<auc:AnnualSavingsSourceEnergy>0.0</auc:AnnualSavingsSourceEnergy>
<auc:AnnualSavingsCost>0</auc:AnnualSavingsCost>
<auc:AnnualSavingsByFuels>
<auc:AnnualSavingsByFuel>
Expand All @@ -1936,6 +1937,7 @@
<auc:ResourceUnits>kBtu</auc:ResourceUnits>
<auc:AnnualFuelUseNativeUnits>2235075.9859244428</auc:AnnualFuelUseNativeUnits>
<auc:AnnualFuelUseConsistentUnits>2235.076</auc:AnnualFuelUseConsistentUnits>
<auc:AnnualPeakConsistentUnits>110.75706</auc:AnnualPeakConsistentUnits>
</auc:ResourceUse>
<auc:ResourceUse ID="Baseline-Resource2">
<auc:EnergyResource>Natural gas</auc:EnergyResource>
Expand Down Expand Up @@ -2166,6 +2168,9 @@
<auc:AllResourceTotal>
<auc:EndUse>All end uses</auc:EndUse>
<auc:SiteEnergyUse>3902.655949232491</auc:SiteEnergyUse>
<auc:SiteEnergyUseIntensity>52.8533358512101</auc:SiteEnergyUseIntensity>
<auc:SourceEnergyUse>2051986.1527934822</auc:SourceEnergyUse>
<auc:SourceEnergyUseIntensity>132.24988549512202</auc:SourceEnergyUseIntensity>
</auc:AllResourceTotal>
</auc:AllResourceTotals>
</auc:Scenario>
Expand All @@ -2183,6 +2188,7 @@
</auc:Modeled>
</auc:CalculationMethod>
<auc:AnnualSavingsSiteEnergy>758.5569977291548</auc:AnnualSavingsSiteEnergy>
<auc:AnnualSavingsSourceEnergy>1772.1336698497769</auc:AnnualSavingsSourceEnergy>
<auc:AnnualSavingsCost>15183</auc:AnnualSavingsCost>
<auc:AnnualSavingsByFuels>
<auc:AnnualSavingsByFuel>
Expand All @@ -2204,6 +2210,7 @@
<auc:ResourceUnits>kBtu</auc:ResourceUnits>
<auc:AnnualFuelUseNativeUnits>3206417.9271639367</auc:AnnualFuelUseNativeUnits>
<auc:AnnualFuelUseConsistentUnits>2064.179</auc:AnnualFuelUseConsistentUnits>
<auc:AnnualPeakConsistentUnits>110.75706</auc:AnnualPeakConsistentUnits>
</auc:ResourceUse>
<auc:ResourceUse ID="LED_Only-Resource2">
<auc:EnergyResource>Natural gas</auc:EnergyResource>
Expand Down Expand Up @@ -2434,6 +2441,9 @@
<auc:AllResourceTotal>
<auc:EndUse>All end uses</auc:EndUse>
<auc:SiteEnergyUse>3144.098951503336</auc:SiteEnergyUse>
<auc:SiteEnergyUseIntensity>52.8533358512101</auc:SiteEnergyUseIntensity>
<auc:SourceEnergyUse>2051986.1527934822</auc:SourceEnergyUse>
<auc:SourceEnergyUseIntensity>132.24988549512202</auc:SourceEnergyUseIntensity>
</auc:AllResourceTotal>
</auc:AllResourceTotals>
<auc:LinkedPremises>
Expand All @@ -2456,6 +2466,7 @@
</auc:Modeled>
</auc:CalculationMethod>
<auc:AnnualSavingsSiteEnergy>284.11765998512055</auc:AnnualSavingsSiteEnergy>
<auc:AnnualSavingsSourceEnergy>1772.1336698497769</auc:AnnualSavingsSourceEnergy>
<auc:AnnualSavingsCost>5194</auc:AnnualSavingsCost>
<auc:AnnualSavingsByFuels>
<auc:AnnualSavingsByFuel>
Expand All @@ -2477,6 +2488,7 @@
<auc:ResourceUnits>kBtu</auc:ResourceUnits>
<auc:AnnualFuelUseNativeUnits>2913836.260294419</auc:AnnualFuelUseNativeUnits>
<auc:AnnualFuelUseConsistentUnits>2913.836</auc:AnnualFuelUseConsistentUnits>
<auc:AnnualPeakConsistentUnits>110.75706</auc:AnnualPeakConsistentUnits>
</auc:ResourceUse>
<auc:ResourceUse ID="Electric_Appliance_30pct_Reduction-Resource2">
<auc:EnergyResource>Natural gas</auc:EnergyResource>
Expand Down Expand Up @@ -2707,6 +2719,9 @@
<auc:AllResourceTotal>
<auc:EndUse>All end uses</auc:EndUse>
<auc:SiteEnergyUse>3618.5382892473704</auc:SiteEnergyUse>
<auc:SiteEnergyUseIntensity>52.8533358512101</auc:SiteEnergyUseIntensity>
<auc:SourceEnergyUse>2051986.1527934822</auc:SourceEnergyUse>
<auc:SourceEnergyUseIntensity>132.24988549512202</auc:SourceEnergyUseIntensity>
</auc:AllResourceTotal>
</auc:AllResourceTotals>
<auc:LinkedPremises>
Expand Down
226 changes: 226 additions & 0 deletions seed/building_sync/tests/test_validation_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
# !/usr/bin/env python
# encoding: utf-8
"""
:copyright (c) 2014 - 2019, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Department of Energy) and contributors. All rights reserved. # NOQA
:author
"""
import os
from unittest.mock import patch

from django.test import TestCase
from requests.models import Response
import json

from config.settings.common import BASE_DIR
from seed.building_sync.validation_client import validate_use_case, DEFAULT_USE_CASE


def responseFactory(status_code, body_dict):
the_response = Response()
the_response.status_code = status_code
the_response._content = json.dumps(body_dict).encode()
return the_response


class TestValidationClient(TestCase):
def setUp(self):
# NOTE: the contents of these files are not actually used, it's just convenient
# to use these files so we don't have to create tmp ones and clean them up
self.single_file = open(os.path.join(BASE_DIR, 'seed', 'building_sync', 'tests', 'data', 'buildingsync_v2_0_bricr_workflow.xml'))
self.zip_file = open(os.path.join(BASE_DIR, 'seed', 'building_sync', 'tests', 'data', 'ex_1_and_buildingsync_ex01_measures.zip'))

def test_validation_single_file_ok(self):
good_body = {
'success': True,
'validation_results': {
'schema': {
'valid': True
},
'use_cases': {
DEFAULT_USE_CASE: {
'errors': [],
'warnings': [],
}
}
}
}

with patch('seed.building_sync.validation_client._validation_api_post', return_value=responseFactory(200, good_body)):
all_files_valid, file_summaries = validate_use_case(self.single_file)

self.assertTrue(all_files_valid)
self.assertEqual([], file_summaries)

def test_validation_zip_file_ok(self):
good_body = {
'success': True,
'validation_results': [
{
'file': 'file1.xml',
'results': {
'schema': {
'valid': True
},
'use_cases': {
DEFAULT_USE_CASE: {
'errors': [],
'warnings': [],
}
}
}
}, {
'file': 'file2.xml',
'results': {
'schema': {
'valid': True
},
'use_cases': {
DEFAULT_USE_CASE: {
'errors': [],
'warnings': [],
}
}
}
}
]
}

with patch('seed.building_sync.validation_client._validation_api_post', return_value=responseFactory(200, good_body)):
all_files_valid, file_summaries = validate_use_case(self.zip_file)

self.assertTrue(all_files_valid)
self.assertEqual([], file_summaries)

def test_validation_fails_when_one_file_has_bad_schema(self):
bad_file_result = {
'file': 'bad.xml',
'results': {
'schema': {
# Set the schema as NOT valid
'valid': False,
'errors': ['schema was bad']
},
'use_cases': {
DEFAULT_USE_CASE: {
'errors': [],
'warnings': [],
}
}
}
}

body = {
'success': True,
'validation_results': [
{
'file': 'file1.xml',
'results': {
'schema': {
'valid': True
},
'use_cases': {
DEFAULT_USE_CASE: {
'errors': [],
'warnings': [],
}
}
}
},
bad_file_result,
]
}

with patch('seed.building_sync.validation_client._validation_api_post', return_value=responseFactory(200, body)):
all_files_valid, file_summaries = validate_use_case(self.zip_file)

self.assertFalse(all_files_valid)
bad_file_names = [f['file'] for f in file_summaries]
self.assertEqual([bad_file_result['file']], bad_file_names)

def test_validation_fails_when_one_file_fails_use_case(self):
bad_file_result = {
'file': 'bad.xml',
'results': {
'schema': {
'valid': True,
},
'use_cases': {
DEFAULT_USE_CASE: {
# Include a use case error
'errors': ['something was wrong'],
'warnings': [],
}
}
}
}

body = {
'success': True,
'validation_results': [
{
'file': 'file1.xml',
'results': {
'schema': {
'valid': True
},
'use_cases': {
DEFAULT_USE_CASE: {
'errors': [],
'warnings': [],
}
}
}
},
bad_file_result,
]
}

with patch('seed.building_sync.validation_client._validation_api_post', return_value=responseFactory(200, body)):
all_files_valid, file_summaries = validate_use_case(self.zip_file)

self.assertFalse(all_files_valid)
bad_file_names = [f['file'] for f in file_summaries]
self.assertEqual([bad_file_result['file']], bad_file_names)

def test_validation_zip_file_ok_when_warnings(self):
good_body = {
'success': True,
'validation_results': [
{
'file': 'file1.xml',
'results': {
'schema': {
'valid': True
},
'use_cases': {
DEFAULT_USE_CASE: {
'errors': [],
# Include a warning
'warnings': ['This is a warning!'],
}
}
}
}, {
'file': 'file2.xml',
'results': {
'schema': {
'valid': True
},
'use_cases': {
DEFAULT_USE_CASE: {
'errors': [],
# Include a warning
'warnings': ['This is another warning!'],
}
}
}
}
]
}

with patch('seed.building_sync.validation_client._validation_api_post', return_value=responseFactory(200, good_body)):
all_files_valid, file_summaries = validate_use_case(self.zip_file)

self.assertTrue(all_files_valid)
file_names = [f['file'] for f in file_summaries]
self.assertEqual(['file1.xml', 'file2.xml'], file_names)

0 comments on commit 3eda5de

Please sign in to comment.