Skip to content

Commit 80196df

Browse files
committed
CDA-75 adds unit test for pump flow unit conversion
CDA-75 adds new unit on retrieval of object CDA-75 Adds flow unit field to water user contract account object and updated unit conversion on store to use that if it exists.
1 parent 677dec6 commit 80196df

File tree

4 files changed

+217
-19
lines changed

4 files changed

+217
-19
lines changed

schema/src/cwms/cwms_water_supply_pkg_body.sql

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,7 @@ BEGIN
12551255
rec.phys_trans_type_tooltip,
12561256
rec.phys_trans_type_active),
12571257
rec.pump_flow * rec.factor + rec.offset,
1258+
nvl(p_units, 'cms'),
12581259
-- rec.units_id,
12591260
cwms_util.change_timezone(
12601261
rec.transfer_start_datetime,
@@ -1362,24 +1363,6 @@ BEGIN
13621363

13631364
-- dbms_output.put_line('wuc code: '|| l_contract_code);
13641365

1365-
--get the offset and factor
1366-
----------------------------------
1367-
-- get the unit conversion info --
1368-
----------------------------------
1369-
SELECT uc.factor,
1370-
uc.offset
1371-
INTO l_factor,
1372-
l_offset
1373-
from cwms_base_parameter bp,
1374-
cwms_unit_conversion uc,
1375-
cwms_unit u
1376-
WHERE bp.base_parameter_id = 'Flow'
1377-
and uc.to_unit_code = bp.unit_code
1378-
and uc.from_unit_code = u.unit_code
1379-
and u.unit_id = nvl(p_flow_unit_id,'cms');
1380-
1381-
-- dbms_output.put_line('unit conv: '|| l_factor ||', '||l_offset);
1382-
13831366
-- select count(*) into l_count from at_wat_usr_contract_accounting;
13841367
-- dbms_output.put_line('row count: '|| l_count);
13851368
-- delete existing data
@@ -1427,7 +1410,11 @@ BEGIN
14271410
l_contract_code contract_code,
14281411
acct_tab.pump_location_ref.get_location_code('F') pump_code,
14291412
ptt.phys_trans_type_code xfer_code,
1430-
acct_tab.pump_flow * l_factor + l_offset flow,
1413+
cwms_util.convert_units(
1414+
acct_tab.pump_flow,
1415+
nvl(acct_tab.pump_flow_unit, nvl(p_flow_unit_id,'cms')),
1416+
'cms'
1417+
) flow,
14311418
cwms_util.change_timezone(
14321419
acct_tab.transfer_start_datetime,
14331420
l_time_zone,

schema/src/cwms/types/wat_usr_contract_acct_obj_t.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CREATE type wat_usr_contract_acct_obj_t
88
* @member pump_location_ref The location of the pump for this accounting record
99
* @member physical_transfer_type Identifies the type of water transfer for this accounting record
1010
* @member pump_flow The pump flow for this accounting record
11+
* @member pump_flow_unit The unit of the pump flow for this accounting record
1112
* @member transfer_start_datetime The beginning time for the water transfer for this accounting record
1213
* @member accounting_remarks Remarks for this accounting record
1314
*/
@@ -18,6 +19,7 @@ AS
1819
pump_location_ref location_ref_t, --the contract pump that was used for this accounting.
1920
physical_transfer_type lookup_type_obj_t, --The type of transfer for this water movement. See AT_PHYSICAL_TRANSFER_TYPE_CODE.
2021
pump_flow binary_double, --Param: Flow. The flow associated with the water accounting record
22+
pump_flow_unit varchar2(16), -- The units used for pump_flow
2123
transfer_start_datetime date, --The date this water movement began, DATE includes the time zone.
2224
accounting_remarks varchar2(255 byte) --Any comments regarding this water accounting movement
2325
);
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
set verify off
2+
create or replace package &&cwms_schema..test_cwms_water_supply as
3+
--%suite(Test cwms units conversions for water supply accounting)
4+
5+
--%beforeall(setup)
6+
--%afterall(teardown)
7+
--%rollback(manual)
8+
9+
procedure setup;
10+
procedure teardown;
11+
12+
--%test(Roundtrip store/retrieve of pump accounting with per-record units)
13+
procedure test_roundtrip_pump_accounting_units;
14+
15+
end test_cwms_water_supply;
16+
/
17+
show errors;
18+
19+
create or replace package body &&cwms_schema..test_cwms_water_supply as
20+
21+
c_office_id varchar2(16) := '&&office_id';
22+
c_project_loc_id varchar2(57) := 'WSUnitsProj';
23+
c_pump_loc_id varchar2(57) := 'WSUnitsPump';
24+
c_water_user_name varchar2(64) := 'WS Units User';
25+
c_contract_name varchar2(64) := 'WS Units Contract';
26+
27+
procedure setup
28+
is
29+
begin
30+
-- Ensure clean state
31+
begin
32+
cwms_water_supply.delete_contract(
33+
p_contract_ref => water_user_contract_ref_t(
34+
water_user_obj_t(
35+
location_ref_t(c_project_loc_id, c_office_id),
36+
c_water_user_name,
37+
null),
38+
c_contract_name),
39+
p_delete_action => cwms_util.delete_all);
40+
exception when others then null; end;
41+
begin
42+
cwms_water_supply.delete_water_user(
43+
p_project_location_ref => location_ref_t(c_project_loc_id, c_office_id),
44+
p_entity_name => c_water_user_name,
45+
p_delete_action => cwms_util.delete_all);
46+
exception when others then null; end;
47+
begin
48+
cwms_loc.delete_location(c_pump_loc_id, cwms_util.delete_all, c_office_id);
49+
exception when others then null; end;
50+
begin
51+
cwms_loc.delete_location(c_project_loc_id, cwms_util.delete_all, c_office_id);
52+
exception when others then null; end;
53+
54+
-- Create needed locations
55+
cwms_loc.store_location(
56+
p_location_id => c_project_loc_id,
57+
p_time_zone_id => 'UTC',
58+
p_vertical_datum => null,
59+
p_db_office_id => c_office_id);
60+
61+
cwms_loc.store_location(
62+
p_location_id => c_pump_loc_id,
63+
p_time_zone_id => 'UTC',
64+
p_vertical_datum => null,
65+
p_db_office_id => c_office_id);
66+
67+
-- Create water user
68+
cwms_water_supply.store_water_user(
69+
p_water_user => water_user_obj_t(
70+
location_ref_t(c_project_loc_id, c_office_id),
71+
c_water_user_name,
72+
'TEST RIGHT'),
73+
p_fail_if_exists => 'F');
74+
75+
-- Create a simple contract
76+
cwms_water_supply.store_contracts(
77+
p_contracts => water_user_contract_tab_t(
78+
water_user_contract_obj_t(
79+
water_user_contract_ref_t(
80+
water_user_obj_t(
81+
location_ref_t(c_project_loc_id, c_office_id),
82+
c_water_user_name,
83+
'TEST RIGHT'),
84+
c_contract_name),
85+
lookup_type_obj_t('CWMS','Storage','', 'T'),
86+
null, -- effective
87+
null, -- expiration
88+
0, -- contracted_storage
89+
0, -- initial_use_allocation
90+
0, -- future_use_allocation
91+
'ac-ft', -- storage units
92+
null, -- future_use_percent_activated
93+
null, -- total_alloc_percent_activated
94+
null, -- pump_out_location
95+
null, -- pump_out_below_location
96+
null -- pump_in_location
97+
)
98+
),
99+
p_fail_if_exists => 'F');
100+
101+
commit;
102+
end setup;
103+
104+
procedure teardown
105+
is
106+
begin
107+
begin
108+
cwms_water_supply.delete_contract(
109+
p_contract_ref => water_user_contract_ref_t(
110+
water_user_obj_t(
111+
location_ref_t(c_project_loc_id, c_office_id),
112+
c_water_user_name,
113+
'TEST RIGHT'),
114+
c_contract_name),
115+
p_delete_action => cwms_util.delete_all);
116+
exception when others then null; end;
117+
begin
118+
cwms_water_supply.delete_water_user(
119+
p_project_location_ref => location_ref_t(c_project_loc_id, c_office_id),
120+
p_entity_name => c_water_user_name,
121+
p_delete_action => cwms_util.delete_all);
122+
exception when others then null; end;
123+
begin
124+
cwms_loc.delete_location(c_pump_loc_id, cwms_util.delete_all, c_office_id);
125+
exception when others then null; end;
126+
begin
127+
cwms_loc.delete_location(c_project_loc_id, cwms_util.delete_all, c_office_id);
128+
exception when others then null; end;
129+
commit;
130+
end teardown;
131+
132+
procedure test_roundtrip_pump_accounting_units
133+
is
134+
l_contract_ref water_user_contract_ref_t := water_user_contract_ref_t(
135+
water_user_obj_t(location_ref_t(c_project_loc_id, c_office_id), c_water_user_name, 'TEST RIGHT'),
136+
c_contract_name);
137+
l_tz varchar2(28) := 'UTC';
138+
l_start_time date := to_date('2025-01-01 00:00','yyyy-mm-dd hh24:mi');
139+
l_end_time date := to_date('2025-01-01 01:00','yyyy-mm-dd hh24:mi');
140+
l_flow_cfs binary_double := 100;
141+
l_expected_cms binary_double;
142+
l_set wat_usr_contract_acct_tab_t;
143+
begin
144+
-- Compute expected cms via database conversion logic to avoid hard-coding factors
145+
l_expected_cms := cwms_util.convert_units(l_flow_cfs, 'cfs', 'cms');
146+
147+
-- Store one accounting record with cfs unit
148+
cwms_water_supply.store_accounting_set(
149+
p_accounting_tab => wat_usr_contract_acct_tab_t(
150+
wat_usr_contract_acct_obj_t(
151+
l_contract_ref,
152+
location_ref_t(c_pump_loc_id, c_office_id),
153+
lookup_type_obj_t('CWMS','Pipeline','', 'T'),
154+
l_flow_cfs,
155+
'cfs',
156+
l_start_time,
157+
'Roundtrip units test')
158+
),
159+
p_contract_ref => l_contract_ref,
160+
p_pump_time_window_tab => loc_ref_time_window_tab_t(
161+
loc_ref_time_window_obj_t(location_ref_t(c_pump_loc_id, c_office_id), l_start_time, l_end_time)
162+
),
163+
p_time_zone => l_tz,
164+
p_flow_unit_id => null,
165+
p_store_rule => null,
166+
p_override_prot => 'F');
167+
168+
-- Retrieve in cfs and validate
169+
cwms_water_supply.retrieve_accounting_set(
170+
p_accounting_set => l_set,
171+
p_contract_ref => l_contract_ref,
172+
p_units => 'cfs',
173+
p_start_time => l_start_time,
174+
p_end_time => l_end_time,
175+
p_time_zone => l_tz,
176+
p_start_inclusive => 'T',
177+
p_end_inclusive => 'T',
178+
p_ascending_flag => 'T',
179+
p_row_limit => 10,
180+
p_transfer_type => null);
181+
182+
ut.expect(l_set.count).to_equal(1);
183+
ut.expect(abs(l_set(1).pump_flow - l_flow_cfs) < 1e-6).to_be_true();
184+
ut.expect(upper(l_set(1).pump_flow_unit)).to_equal('CFS');
185+
186+
-- Retrieve in cms and validate conversion
187+
cwms_water_supply.retrieve_accounting_set(
188+
p_accounting_set => l_set,
189+
p_contract_ref => l_contract_ref,
190+
p_units => 'cms',
191+
p_start_time => l_start_time,
192+
p_end_time => l_end_time,
193+
p_time_zone => l_tz,
194+
p_start_inclusive => 'T',
195+
p_end_inclusive => 'T',
196+
p_ascending_flag => 'T',
197+
p_row_limit => 10,
198+
p_transfer_type => null);
199+
200+
ut.expect(l_set.count).to_equal(1);
201+
ut.expect(abs(l_set(1).pump_flow - l_expected_cms) < 1e-6).to_be_true();
202+
ut.expect(upper(l_set(1).pump_flow_unit)).to_equal('CMS');
203+
204+
end test_roundtrip_pump_accounting_units;
205+
206+
end test_cwms_water_supply;
207+
/
208+
show errors;

schema/src/test/tests.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ show errors;
4848
@test_timeseries_snapping.sql;
4949
@test_cwms_cat.sql;
5050
@test_cwms_level.sql;
51+
@test_cwms_water_supply.sql;
5152
@test_cwms_display.sql;
5253
@test_cwms_data_dissem.sql;
5354
@test_cwms_fcst.sql;

0 commit comments

Comments
 (0)