diff --git a/docs/sphinx/source/whatsnew/v0.7.0.rst b/docs/sphinx/source/whatsnew/v0.7.0.rst index a8b2f21ebd..a5251d9047 100644 --- a/docs/sphinx/source/whatsnew/v0.7.0.rst +++ b/docs/sphinx/source/whatsnew/v0.7.0.rst @@ -7,7 +7,7 @@ This is a major release that drops support for Python 2 and Python 3.4. We recommend all users of v0.6.3 upgrade to this release after checking API compatibility notes. -**Python 2.7 support ended on June 1, 2019**. (:issue:`501`) +**Python 2.7 support ended on June 1, 2019.** (:issue:`501`) **Minimum numpy version is now 1.10.4. Minimum pandas version is now 0.18.1.** Bug fixes @@ -18,6 +18,7 @@ Bug fixes Testing ~~~~~~~ * Added 30 minutes to timestamps in `test_psm3.csv` to match change in NSRDB (:issue:`733`) +* Added tests for methods in bifacial.py. Contributors @@ -25,3 +26,4 @@ Contributors * Mark Campanellli (:ghuser:`markcampanelli`) * Will Holmgren (:ghuser:`wholmgren`) * Oscar Dowson (:ghuser:`odow`) +* Alexander Morgan (:ghuser:`alexandermorgan`) diff --git a/pvlib/bifacial.py b/pvlib/bifacial.py index 79d634640b..85d60003ab 100644 --- a/pvlib/bifacial.py +++ b/pvlib/bifacial.py @@ -159,8 +159,7 @@ def build(report, pvarray): back surface of center pvrow (index=1)""" # Initialize the report as a dictionary if report is None: - list_keys = ['total_inc_back', 'total_inc_front'] - report = {key: [] for key in list_keys} + report = {'total_inc_back': [], 'total_inc_front': []} # Add elements to the report if pvarray is not None: pvrow = pvarray.pvrows[1] # use center pvrow @@ -177,13 +176,12 @@ def build(report, pvarray): @staticmethod def merge(reports): - """Works for dictionary reports""" + """Works for dictionary reports. Merges the reports list of + dictionaries in a single dictionary. The list of the first + dictionary are extended by those of all subsequent lists.""" report = reports[0] - # Merge only if more than 1 report - if len(reports) > 1: - keys_report = list(reports[0].keys()) - for other_report in reports[1:]: - if other_report is not None: - for key in keys_report: - report[key] += other_report[key] + keys_report = list(report.keys()) + for other_report in reports[1:]: # loop won't run if len(reports) < 2 + for key in keys_report: + report[key] += other_report[key] return report diff --git a/pvlib/test/test_bifacial.py b/pvlib/test/test_bifacial.py index e396a3575c..c33ae02474 100644 --- a/pvlib/test/test_bifacial.py +++ b/pvlib/test/test_bifacial.py @@ -1,6 +1,7 @@ import pandas as pd +import numpy as np from datetime import datetime -from pvlib.bifacial import pvfactors_timeseries +from pvlib.bifacial import pvfactors_timeseries, PVFactorsReportBuilder from conftest import requires_pvfactors import pytest @@ -107,3 +108,43 @@ def test_pvfactors_timeseries_pandas_inputs(run_parallel_calculations): pd.testing.assert_series_equal(ipoa_front, expected_ipoa_front) pd.testing.assert_series_equal(ipoa_back, expected_ipoa_back) + + +def test_build_1(): + """Test that build correctly instantiates a dictionary, when passed a Nones + for the report and pvarray arguments. + """ + report = None + pvarray = None + expected = {'total_inc_back': [np.nan], 'total_inc_front': [np.nan]} + assert expected == PVFactorsReportBuilder.build(report, pvarray) + + +def test_merge_1(): + """Test that merge correctly returns the first element of the reports + argument when there is only dictionary in reports. + """ + test_dict = {'total_inc_back': [1, 2, 3], 'total_inc_front': [4, 5, 6]} + reports = [test_dict] + assert test_dict == PVFactorsReportBuilder.merge(reports) + + +def test_merge_2(): + """Test that merge correctly combines two dictionary reports. + """ + test_dict_1 = {'total_inc_back': [1, 2], 'total_inc_front': [4, 5]} + test_dict_2 = {'total_inc_back': [3], 'total_inc_front': [6]} + expected = {'total_inc_back': [1, 2, 3], 'total_inc_front': [4, 5, 6]} + reports = [test_dict_1, test_dict_2] + assert expected == PVFactorsReportBuilder.merge(reports) + + +def test_merge_3(): + """Test that merge correctly combines three dictionary reports. + """ + test_dict_1 = {'total_inc_back': [1], 'total_inc_front': [4]} + test_dict_2 = {'total_inc_back': [2], 'total_inc_front': [5]} + test_dict_3 = {'total_inc_back': [3], 'total_inc_front': [6]} + expected = {'total_inc_back': [1, 2, 3], 'total_inc_front': [4, 5, 6]} + reports = [test_dict_1, test_dict_2, test_dict_3] + assert expected == PVFactorsReportBuilder.merge(reports)