1
+ import pandas as pd
2
+ import numpy as np
3
+ import xarray as xr
4
+ import pytest
5
+ from pysimdeum .tools .helper import _create_data
6
+
7
+ def setUp ():
8
+ # Mocking inputproperty for testing
9
+ class MockInputProperty :
10
+ def __init__ (self , consumption , users ):
11
+ self .consumption = consumption
12
+ self .users = users
13
+
14
+ # Mocking consumption data
15
+ time = pd .date_range (start = '2024-01-01' , periods = 10 , freq = 'H' )
16
+ patterns = ['pattern1' , 'pattern2' ]
17
+ users = ['user1' , 'user2' ]
18
+ data = np .random .rand (10 , 2 , 2 , 2 , 2 )
19
+ consumption = xr .DataArray (data , dims = ('time' , 'user' , 'enduse' , 'patterns' , 'flowtypes' ),
20
+ coords = {'time' : time , 'user' : users , 'enduse' : range (2 ),
21
+ 'patterns' : patterns , 'flowtypes' : ['totalflow' , 'hotflow' ]})
22
+ return MockInputProperty (consumption , users )
23
+
24
+ def test_create_data ():
25
+ # Call the function with the mocked inputproperty
26
+ input = setUp ()
27
+ appliance_data , total_water_usage , total_users , total_number_of_days , total_patterns = _create_data (input )
28
+
29
+ # Assertions for total water usage
30
+ expected_total_water_usage = np .sum (input .consumption .sel (flowtypes = 'totalflow' ).values )
31
+ assert pytest .approx (total_water_usage ) == expected_total_water_usage
32
+
33
+ # Assertions for total patterns
34
+ expected_total_patterns = len (input .consumption .patterns )
35
+ assert total_patterns == expected_total_patterns
36
+
37
+ # Assertions for total users
38
+ expected_total_users = len (input .users )
39
+ assert total_users == expected_total_users
40
+
41
+ # Assertions for appliance data
42
+ expected_appliance_data_total = input .consumption .sel (flowtypes = 'totalflow' ).sum ('user' ).sum ('time' ).sum ('patterns' ).to_dataframe ('total' )
43
+ expected_appliance_data_total ['percentage' ] = (expected_appliance_data_total ['total' ]/ total_water_usage )* 100
44
+ expected_appliance_data_total ['pp' ] = expected_appliance_data_total ['total' ]/ total_users
45
+ expected_appliance_data_total ['pppd' ] = (expected_appliance_data_total ['pp' ]/ total_patterns )/ total_number_of_days
46
+
47
+ pd .testing .assert_frame_equal (appliance_data , expected_appliance_data_total )
48
+
49
+ # Assertions for total number of days
50
+ expected_number_of_seconds = len (input .consumption )
51
+ expected_total_number_of_days = expected_number_of_seconds / (60 * 60 * 24 )
52
+ assert pytest .approx (total_number_of_days ) == expected_total_number_of_days
0 commit comments