From 5d49d707eb9d168a82c4f609b151b9b2ee36aec1 Mon Sep 17 00:00:00 2001 From: David Blum Date: Wed, 31 May 2023 07:07:52 -0700 Subject: [PATCH 1/7] Implement test to reproduce error (for #533) --- testing/test_testcase.py | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 testing/test_testcase.py diff --git a/testing/test_testcase.py b/testing/test_testcase.py new file mode 100644 index 000000000..7dfbf53c3 --- /dev/null +++ b/testing/test_testcase.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed May 31 06:45:55 2023 + +@author: dhbubu18 +""" + +import utilities +import os +import unittest +import testcase + +class Advance(unittest.TestCase): + '''Unit tests for the testcase.TestCase.advance API. + + ''' + + def test_check_key(self): + '''Test that an overwrite value of 0 given in float is used. + + ''' + + self.testcase.set_step(60) + # Advance first wih fan speed of 0.5, then with fan speed of 0.0. + u = {'fcu_oveFan_u': 0.5, + 'fcu_oveFan_activate':1} + status, message, payload = self.testcase.advance(u) + u = {'fcu_oveFan_u': 0.0, + 'fcu_oveFan_activate':1} + status, message, payload = self.testcase.advance(u) + # Get results + status, message, payload = self.testcase.get_results(['fcu_reaPFan_y', 'fcu_oveFan_u'], + 0, + payload['time']) + print(payload) + + def setUp(self): + '''Set up for unit tests. Uses bestest_air. + + ''' + + self.testcase = testcase.TestCase(fmupath='testcases/bestest_air/models/wrapped.fmu') + +if __name__ == '__main__': + utilities.run_tests(os.path.basename(__file__)) From 09bf61ccb7b3e5e3919c78f7c316fef86ed33d0e Mon Sep 17 00:00:00 2001 From: David Blum Date: Wed, 31 May 2023 07:11:17 -0700 Subject: [PATCH 2/7] Make fix --- testcase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testcase.py b/testcase.py index de6b5a847..287f1fcf1 100644 --- a/testcase.py +++ b/testcase.py @@ -276,7 +276,7 @@ def advance(self, u): message = "Unexpected input variable: {}.".format(key) logging.error(message) return status, message, payload - if key != 'time' and u[key]: + if (key != 'time' and (u[key] != None)): if '_activate' in key: try: if float(u[key]) == 1: From 1f5a31bd30fafcb87b01de917b2f7f8680acaddd Mon Sep 17 00:00:00 2001 From: David Blum Date: Wed, 31 May 2023 07:30:35 -0700 Subject: [PATCH 3/7] Add reference results --- .../testcase/check_input_for_zero.csv | 6 ++++++ testing/test_testcase.py | 17 ++++++++++++----- testing/utilities.py | 2 +- 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 testing/references/testcase/check_input_for_zero.csv diff --git a/testing/references/testcase/check_input_for_zero.csv b/testing/references/testcase/check_input_for_zero.csv new file mode 100644 index 000000000..9c0d0c8b8 --- /dev/null +++ b/testing/references/testcase/check_input_for_zero.csv @@ -0,0 +1,6 @@ +time,fcu_oveFan_u,fcu_reaPFan_y +0.0,0.0,0.0 +30.0,0.5,21.394367146435748 +60.0,0.5,21.63003284649045 +90.0,0.0,6.50350841614713e-05 +120.0,0.0,4.2485279262958664e-11 diff --git a/testing/test_testcase.py b/testing/test_testcase.py index 7dfbf53c3..2d55684d8 100644 --- a/testing/test_testcase.py +++ b/testing/test_testcase.py @@ -5,17 +5,20 @@ @author: dhbubu18 """ -import utilities import os import unittest +import pandas as pd import testcase +import utilities + +reference_result_directory = 'testcase' -class Advance(unittest.TestCase): +class Advance(unittest.TestCase, utilities.partialChecks): '''Unit tests for the testcase.TestCase.advance API. ''' - def test_check_key(self): + def test_check_input_for_zero(self): '''Test that an overwrite value of 0 given in float is used. ''' @@ -32,14 +35,18 @@ def test_check_key(self): status, message, payload = self.testcase.get_results(['fcu_reaPFan_y', 'fcu_oveFan_u'], 0, payload['time']) - print(payload) + # Test results + df = pd.DataFrame(payload).set_index('time') + ref_filepath = os.path.join(utilities.get_root_path(), 'testing', 'references', reference_result_directory, 'check_input_for_zero.csv') + self.compare_ref_timeseries_df(df, ref_filepath) def setUp(self): '''Set up for unit tests. Uses bestest_air. ''' - self.testcase = testcase.TestCase(fmupath='testcases/bestest_air/models/wrapped.fmu') + self.testcase_name = 'bestest_air' + self.testcase = testcase.TestCase(fmupath='testcases/'+self.testcase_name+'/models/wrapped.fmu') if __name__ == '__main__': utilities.run_tests(os.path.basename(__file__)) diff --git a/testing/utilities.py b/testing/utilities.py index a09431528..28f08d051 100644 --- a/testing/utilities.py +++ b/testing/utilities.py @@ -155,7 +155,7 @@ def compare_ref_timeseries_df(self, df, ref_filepath): df : pandas DataFrame Test dataframe with "time" as index. ref_filepath : str - Reference file path relative to testing directory. + Reference file path. Returns ------- From 6e960c5cb09cb967936927f41255ac120f665521 Mon Sep 17 00:00:00 2001 From: David Blum Date: Wed, 31 May 2023 08:07:49 -0700 Subject: [PATCH 4/7] Setup unit test using docker,make and add to travis --- .travis.yml | 3 +++ testing/makefile | 24 +++++++++++++++++++ .../testcase/check_input_for_zero.csv | 8 +++---- testing/test_testcase.py | 3 +-- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3ff5a637c..be609d71a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,6 +35,9 @@ jobs: - python: 3.9 install: pip install --upgrade pip && pip install pandas==1.2.5 numpy==1.20.2 matplotlib==3.3.4 requests==2.25.1 script: cd testing && make build_jm_image && make test_kpis + - python: 3.9 + install: pip install --upgrade pip && pip install pandas==1.2.5 numpy==1.20.2 matplotlib==3.3.4 requests==2.25.1 + script: cd testing && make build_jm_image && make test_testcase - python: 3.9 install: pip install --upgrade pip && pip install pandas==1.2.5 numpy==1.20.2 matplotlib==3.3.4 requests==2.25.1 script: cd testing && make build_jm_image && make test_readme_commands diff --git a/testing/makefile b/testing/makefile index 12cf3629b..0b3fe89a3 100644 --- a/testing/makefile +++ b/testing/makefile @@ -36,6 +36,15 @@ copy_testcase_to_jm: docker cp ../version.txt ${IMG_NAME}:/usr/local/testing/${TESTCASE} make exec_jm ARGS="touch ${TESTCASE}/__init__.py" +copy_framework_with_testcase_to_jm: + make copy_to_jm ARGS=data + make copy_to_jm ARGS=kpis + make copy_to_jm ARGS=forecast + docker cp ../testcase.py ${IMG_NAME}:/usr/local/testing + docker cp ../version.txt ${IMG_NAME}:/usr/local/testing + docker exec -it ${IMG_NAME} /bin/bash -c "mkdir /usr/local/testing/testcases && exit" + docker cp ../testcases/${TESTCASE} ${IMG_NAME}:/usr/local/testing/testcases/${TESTCASE} + copy_from_jm: docker cp ${IMG_NAME}:/usr/local/testing/${ARGS} ../${ARGS} @@ -208,6 +217,20 @@ test_kpis: # Report test results python report.py +test_testcase: +# Run jm docker container + make run_jm +# Copy the required files and folders for the test + make copy_framework_with_testcase_to_jm TESTCASE=bestest_air +# Run test_kpis.py + make exec_jm ARGS="python test_testcase.py" + docker cp ${IMG_NAME}:/usr/local/testing/references/testcase ./references + docker cp ${IMG_NAME}:/usr/local/testing/test_testcase.log ./test_testcase.log +# Stop jm docker container + make stop_jm +# Report test results + python report.py + test_readme_commands: # Test readme commands work right after instantiation of test case container cd .. && TESTCASE=testcase2 docker-compose up -d @@ -250,6 +273,7 @@ test_all: make test_data make test_forecast make test_kpis + make test_testcase make test_readme_commands make test_testcase1 make test_testcase2 diff --git a/testing/references/testcase/check_input_for_zero.csv b/testing/references/testcase/check_input_for_zero.csv index 9c0d0c8b8..9c818c86c 100644 --- a/testing/references/testcase/check_input_for_zero.csv +++ b/testing/references/testcase/check_input_for_zero.csv @@ -1,6 +1,6 @@ time,fcu_oveFan_u,fcu_reaPFan_y 0.0,0.0,0.0 -30.0,0.5,21.394367146435748 -60.0,0.5,21.63003284649045 -90.0,0.0,6.50350841614713e-05 -120.0,0.0,4.2485279262958664e-11 +30.0,0.5,21.3939783147 +60.0,0.5,21.6302109413 +90.0,0.0,6.50343645203e-05 +120.0,0.0,4.24847777137e-11 diff --git a/testing/test_testcase.py b/testing/test_testcase.py index 2d55684d8..899eba573 100644 --- a/testing/test_testcase.py +++ b/testing/test_testcase.py @@ -45,8 +45,7 @@ def setUp(self): ''' - self.testcase_name = 'bestest_air' - self.testcase = testcase.TestCase(fmupath='testcases/'+self.testcase_name+'/models/wrapped.fmu') + self.testcase = testcase.TestCase(fmupath='testcases/bestest_air/models/wrapped.fmu') if __name__ == '__main__': utilities.run_tests(os.path.basename(__file__)) From e2d15effdfbdf487ed5d34bfad6b3e955d276547 Mon Sep 17 00:00:00 2001 From: David Blum Date: Wed, 31 May 2023 08:31:21 -0700 Subject: [PATCH 5/7] Update release notes --- releasenotes.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/releasenotes.md b/releasenotes.md index 218bb74de..a39cf6f57 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -11,6 +11,10 @@ Released on xx/xx/xxxx. - In unit test checking fetching of single forecast variable, specify specific forecast point to check for each test case. This is for [#529](https://github.com/ibpsa/project1-boptest/issues/529). - Update ``KPI_Calculator.get_computational_time_ratio`` to return ``None`` if no simulation steps have been processed. This is for [#540](https://github.com/ibpsa/project1-boptest/issues/540). +**The following changes are backwards-compatible, but might change benchmark results:** + +- Fix check on control input overwrite in ``testcase.TestCase.advance`` when overwriting a value of 0. This will change results if using BOPTEST-Service and delivering control input overwrites, with value of 0, in an /advance request using the ``json`` attribute with the python requests library. This is for [#533](https://github.com/ibpsa/project1-boptest/issues/533). + ## BOPTEST v0.4.0 From 8b3132db7513c4fed59695ad0b0bfbe1dca62784 Mon Sep 17 00:00:00 2001 From: David Blum Date: Wed, 31 May 2023 08:37:13 -0700 Subject: [PATCH 6/7] Edit TestCase.advance doctring --- testcase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testcase.py b/testcase.py index 287f1fcf1..a1e639b58 100644 --- a/testcase.py +++ b/testcase.py @@ -227,7 +227,7 @@ def advance(self, u): u : dict Defines the control input data to be used for the step. {_activate : bool, int, float, or str convertable to 1 or 0 - _u : int or float} + _u : int or float, or str convertable to float} Returns ------- From 7b4a23fabbebe8c22b2ca61b86d91aad917b857b Mon Sep 17 00:00:00 2001 From: David Blum Date: Wed, 31 May 2023 08:40:21 -0700 Subject: [PATCH 7/7] Update test_testcase.py module docstring --- testing/test_testcase.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/testing/test_testcase.py b/testing/test_testcase.py index 899eba573..03c06d705 100644 --- a/testing/test_testcase.py +++ b/testing/test_testcase.py @@ -1,8 +1,7 @@ # -*- coding: utf-8 -*- """ -Created on Wed May 31 06:45:55 2023 +This module implements unit tests testcase.py. -@author: dhbubu18 """ import os