1 package AttrDurationTestDummy;
3 use base qw(SL::DB::Object);
5 __PACKAGE__->meta->setup(
8 dummy => { type => 'numeric', precision => 2, scale => 12 },
9 inty => { type => 'integer' },
10 miny => { type => 'integer' },
14 use SL::DB::Helper::AttrDuration;
16 __PACKAGE__->attr_duration('dummy');
17 __PACKAGE__->attr_duration_minutes('inty', 'miny');
21 use Test::More tests => 130;
30 use Support::TestSetup;
34 return AttrDurationTestDummy->new(@_);
37 Support::TestSetup::login();
40 $::locale = Locale->new('en');
44 # Wenn das Attribut undef ist:
45 is(new_item->dummy, undef, 'uninitialized: raw');
46 is(new_item->dummy_as_hours, 0, 'uninitialized: as_hours');
47 is(new_item->dummy_as_minutes, 0, 'uninitialized: as_minutes');
48 is(new_item->dummy_as_duration_string, undef, 'uninitialized: as_duration_string');
49 is(new_item->dummy_as_man_days, 0, 'uninitialized: as_man_days');
50 is(new_item->dummy_as_man_days_unit, 'h', 'uninitialized: as_man_days_unit');
51 is(new_item->dummy_as_man_days_string, '0,00', 'uninitialized: as_man_days_string');
53 # Auslesen kleiner 8 Stunden:
54 is(new_item(dummy => 2.75)->dummy, 2.75, 'initialized < 8: raw');
55 is(new_item(dummy => 2.75)->dummy_as_hours, 2, 'initialized < 8: as_hours');
56 is(new_item(dummy => 2.75)->dummy_as_minutes, 45, 'initialized < 8: as_minutes');
57 is(new_item(dummy => 2.75)->dummy_as_duration_string, '2,75', 'initialized < 8: as_duration_string');
58 is(new_item(dummy => 2.75)->dummy_as_man_days, 2.75, 'initialized < 8: as_man_days');
59 is(new_item(dummy => 2.75)->dummy_as_man_days_unit, 'h', 'initialized < 8: as_man_days_unit');
60 is(new_item(dummy => 2.75)->dummy_as_man_days_string, '2,75', 'initialized < 8: as_man_days_string');
62 # Auslesen größer 8 Stunden:
63 is(new_item(dummy => 12.5)->dummy, 12.5, 'initialized > 8: raw');
64 is(new_item(dummy => 12.5)->dummy_as_hours, 12, 'initialized > 8: as_hours');
65 is(new_item(dummy => 12.5)->dummy_as_minutes, 30, 'initialized > 8: as_minutes');
66 is(new_item(dummy => 12.5)->dummy_as_duration_string, '12,50', 'initialized > 8: as_duration_string');
67 is(new_item(dummy => 12.5)->dummy_as_man_days, 1.5625, 'initialized > 8: as_man_days');
68 is(new_item(dummy => 12.5)->dummy_as_man_days_unit, 'man_day', 'initialized > 8: as_man_days_unit');
69 is(new_item(dummy => 12.5)->dummy_as_man_days_string, '1,56', 'initialized > 8: as_man_days_string');
71 $item = new_item(dummy => 2.25); $item->dummy_as_duration_string(undef);
72 is($item->dummy, undef, 'write as_duration_string undef read raw');
73 is($item->dummy_as_minutes, 0, 'write as_duration_string undef read as_minutes');
74 is($item->dummy_as_hours, 0, 'write as_duration_string undef read as_hours');
75 is($item->dummy_as_duration_string, undef, 'write as_duration_string undef read as_duration_string');
77 $item = new_item(dummy => 2.25); $item->dummy_as_duration_string("4,80");
78 is($item->dummy, 4.8, 'write as_duration_string 4,80 read raw');
79 is($item->dummy_as_minutes, 48, 'write as_duration_string 4,80 read as_minutes');
80 is($item->dummy_as_hours, 4, 'write as_duration_string 4,80 read as_hours');
81 is($item->dummy_as_duration_string, "4,80", 'write as_duration_string 4,80 read as_duration_string');
83 $item = new_item(dummy => 2.25); $item->dummy_as_minutes(12);
84 is($item->dummy, 2.2, 'write as_minutes 12 read raw');
85 is($item->dummy_as_minutes, 12, 'write as_minutes 12 read as_minutes');
86 is($item->dummy_as_hours, 2, 'write as_minutes 12 read as_hours');
87 is($item->dummy_as_duration_string, "2,20", 'write as_minutes 12 read as_duration_string');
89 $item = new_item(dummy => 2.25); $item->dummy_as_hours(5);
90 is($item->dummy, 5.25, 'write as_hours 5 read raw');
91 is($item->dummy_as_minutes, 15, 'write as_hours 5 read as_minutes');
92 is($item->dummy_as_hours, 5, 'write as_hours 5 read as_hours');
93 is($item->dummy_as_duration_string, "5,25", 'write as_hours 5 read as_duration_string');
95 $item = new_item(dummy => undef);
96 is($item->dummy, undef, 'write raw undef read raw');
97 is($item->dummy_as_man_days, 0, 'write raw undef read as_man_days');
98 is($item->dummy_as_man_days_unit, 'h', 'write raw undef read as_man_days_unit');
99 is($item->dummy_as_man_days_string, '0,00', 'write raw undef read as_man_days_string');
101 $item = new_item(dummy => 4);
102 is($item->dummy, 4, 'write raw 4 read raw');
103 is($item->dummy_as_man_days, 4, 'write raw 4 read as_man_days');
104 is($item->dummy_as_man_days_unit, 'h', 'write raw 4 read as_man_days_unit');
105 is($item->dummy_as_man_days_string, '4,00', 'write raw 4 read as_man_days_string');
107 $item = new_item(dummy => 18);
108 is($item->dummy, 18, 'write raw 18 read raw');
109 is($item->dummy_as_man_days, 2.25, 'write raw 18 read as_man_days');
110 is($item->dummy_as_man_days_unit, 'man_day', 'write raw 18 read as_man_days_unit');
111 is($item->dummy_as_man_days_string, '2,25', 'write raw 18 read as_man_days_string');
113 $item = new_item(dummy => 4);
114 is($item->dummy, 4, 'should not change anything when writing undef: write raw 4 read raw');
115 is($item->dummy_as_man_days(undef), undef, 'should not change anything when writing undef: write as_man_days undef return undef');
116 is($item->dummy, 4, 'should not change anything when writing undef: read raw 2');
117 is($item->dummy_as_man_days_unit(undef), undef, 'should not change anything when writing undef: write as_man_days_unit undef return undef');
118 is($item->dummy, 4, 'should not change anything when writing undef: read raw 3');
119 is($item->dummy_as_man_days_string(undef), undef, 'should not change anything when writing undef: write as_man_days_string undef return undef');
120 is($item->dummy, 4, 'should not change anything when writing undef: read raw 4');
124 is($item->dummy(2), 2, 'parse less than a man day: write raw 2 read raw');
125 is($item->dummy_as_man_days(0.75), 0.75, 'parse less than a man day: write as_man_days 0.75 read as_man_days');
126 is($item->dummy_as_man_days_string('0,5'), '0,50', 'parse less than a man day: write as_man_days_string 0,5 read read as_man_days_string');
129 is($item->dummy(12), 12, 'parse more than a man day: write raw 12 read raw');
130 is($item->dummy_as_man_days(13.25), 1.65625, 'parse more than a man day: write as_man_days 13.25 read as_man_days');
131 is($item->dummy_as_man_days_string('13,5'), '1,69', 'parse more than a man day: write as_man_days_string 13,5 read read as_man_days_string');
134 is($item->dummy(3.25), 3.25, 'parse less than a man day with unit h: write raw 3.25 read raw');
135 is($item->dummy_as_man_days_unit('h'), 'h', 'parse less than a man day with unit h: write as_man_days_unit h read as_man_days_unit');
136 is($item->dummy, 3.25, 'parse less than a man day with unit h: read raw');
139 is($item->dummy(3.25), 3.25, 'parse less than a man day with unit hour: write raw 3.25 read raw');
140 is($item->dummy_as_man_days_unit('hour'), 'h', 'parse less than a man day with unit hour: write as_man_days_unit hour read as_man_days_unit');
141 is($item->dummy, 3.25, 'parse less than a man day with unit hour: read raw');
144 is($item->dummy(3.25), 3.25, 'parse more than a man day with unit man_day: write raw 3.25 read raw');
145 is($item->dummy_as_man_days_unit('man_day'), 'man_day', 'parse more than a man day with unit man_day: write as_man_days_unit man_day read as_man_days_unit');
146 is($item->dummy, 26, 'parse more than a man day with unit man_day: read raw');
148 is(new_item->assign_attributes(dummy_as_man_days => 3, dummy_as_man_days_unit => 'h')->dummy, 3, 'assign_attributes hash 3h');
149 is(new_item->assign_attributes(dummy_as_man_days_unit => 'h', dummy_as_man_days => 3 )->dummy, 3, 'assign_attributes hash h3');
151 is(new_item->assign_attributes(dummy_as_man_days => 3, dummy_as_man_days_unit => 'man_day')->dummy, 24, 'assign_attributes hash 3man_day');
152 is(new_item->assign_attributes(dummy_as_man_days_unit => 'man_day', dummy_as_man_days => 3 )->dummy, 24, 'assign_attributes hash man_day3');
154 is(new_item->assign_attributes('dummy_as_man_days', 3, 'dummy_as_man_days_unit', 'h')->dummy, 3, 'assign_attributes array 3h');
155 is(new_item->assign_attributes('dummy_as_man_days_unit', 'h', 'dummy_as_man_days', 3 )->dummy, 3, 'assign_attributes array h3');
157 is(new_item->assign_attributes('dummy_as_man_days', 3, 'dummy_as_man_days_unit', 'man_day')->dummy, 24, 'assign_attributes array 3man_day');
158 is(new_item->assign_attributes('dummy_as_man_days_unit', 'man_day', 'dummy_as_man_days', 3 )->dummy, 24, 'assign_attributes array man_day3');
160 is(new_item->assign_attributes(dummy_as_man_days_string => '5,25', dummy_as_man_days_unit => 'h' )->dummy, 5.25, 'assign_attributes hash string 5,25h');
161 is(new_item->assign_attributes(dummy_as_man_days_unit => 'h', dummy_as_man_days_string => '5,25')->dummy, 5.25, 'assign_attributes hash string h5,25');
163 is(new_item->assign_attributes(dummy_as_man_days_string => '5,25', dummy_as_man_days_unit => 'man_day')->dummy, 42, 'assign_attributes hash string 5,25man_day');
164 is(new_item->assign_attributes(dummy_as_man_days_unit => 'man_day', dummy_as_man_days_string => '5,25' )->dummy, 42, 'assign_attributes hash string man_day5,25');
166 is(new_item->assign_attributes('dummy_as_man_days_string', '5,25', 'dummy_as_man_days_unit', 'h' )->dummy, 5.25, 'assign_attributes array 5,25h');
167 is(new_item->assign_attributes('dummy_as_man_days_unit', 'h', 'dummy_as_man_days_string', '5,25')->dummy, 5.25, 'assign_attributes array h5,25');
169 is(new_item->assign_attributes('dummy_as_man_days_string', '5,25', 'dummy_as_man_days_unit', 'man_day')->dummy, 42, 'assign_attributes array 5,25man_day');
170 is(new_item->assign_attributes('dummy_as_man_days_unit', 'man_day', 'dummy_as_man_days_string', '5,25' )->dummy, 42, 'assign_attributes array man_day5,25');
172 # Parametervalidierung
173 throws_ok { new_item()->dummy_as_man_days_unit('invalid') } qr/unknown.*unit/i, 'unknown unit';
174 lives_ok { new_item()->dummy_as_man_days_unit('h') } 'known unit h';
175 lives_ok { new_item()->dummy_as_man_days_unit('hour') } 'known unit hour';
176 lives_ok { new_item()->dummy_as_man_days_unit('man_day') } 'known unit man_day';
178 ### attr_duration_minutes
180 # Wenn das Attribut undef ist:
181 is(new_item->inty, undef, 'uninitialized: raw');
182 is(new_item->inty_as_hours, 0, 'uninitialized: as_hours');
183 is(new_item->inty_as_minutes, 0, 'uninitialized: as_minutes');
184 is(new_item->inty_as_duration_string, undef, 'uninitialized: as_duration_string');
186 # Auslesen kleiner 60 Minuten:
187 is(new_item(inty => 37)->inty, 37, 'initialized < 60: raw');
188 is(new_item(inty => 37)->inty_as_hours, 0, 'initialized < 60: as_hours');
189 is(new_item(inty => 37)->inty_as_minutes, 37, 'initialized < 60: as_minutes');
190 is(new_item(inty => 37)->inty_as_duration_string, '0:37', 'initialized < 60: as_duration_string');
192 # Auslesen größer 60 Minuten:
193 is(new_item(inty => 145)->inty, 145, 'initialized > 60: raw');
194 is(new_item(inty => 145)->inty_as_hours, 2, 'initialized > 60: as_hours');
195 is(new_item(inty => 145)->inty_as_minutes, 25, 'initialized > 60: as_minutes');
196 is(new_item(inty => 145)->inty_as_duration_string, '2:25', 'initialized > 60: as_duration_string');
198 $item = new_item(inty => 145); $item->inty_as_duration_string(undef);
199 is($item->inty, undef, 'write as_duration_string undef read raw');
200 is($item->inty_as_minutes, 0, 'write as_duration_string undef read as_minutes');
201 is($item->inty_as_hours, 0, 'write as_duration_string undef read as_hours');
202 is($item->inty_as_duration_string, undef, 'write as_duration_string undef read as_duration_string');
204 $item = new_item(inty => 145); $item->inty_as_duration_string('');
205 is($item->inty, undef, 'write as_duration_string "" read raw');
206 is($item->inty_as_minutes, 0, 'write as_duration_string "" read as_minutes');
207 is($item->inty_as_hours, 0, 'write as_duration_string "" read as_hours');
208 is($item->inty_as_duration_string, undef, 'write as_duration_string "" read as_duration_string');
210 $item = new_item(inty => 145); $item->inty_as_duration_string("3:21");
211 is($item->inty, 201, 'write as_duration_string 3:21 read raw');
212 is($item->inty_as_minutes, 21, 'write as_duration_string 3:21 read as_minutes');
213 is($item->inty_as_hours, 3, 'write as_duration_string 3:21 read as_hours');
214 is($item->inty_as_duration_string, "3:21", 'write as_duration_string 3:21 read as_duration_string');
216 $item = new_item(inty => 145); $item->inty_as_duration_string("03:1");
217 is($item->inty, 181, 'write as_duration_string 03:1 read raw');
218 is($item->inty_as_minutes, 1, 'write as_duration_string 03:1 read as_minutes');
219 is($item->inty_as_hours, 3, 'write as_duration_string 03:1 read as_hours');
220 is($item->inty_as_duration_string, "3:01", 'write as_duration_string 03:1 read as_duration_string');
222 local %::myconfig = (numberformat => "1.000,00");
224 $item = new_item(miny_in_hours => 2.5);
225 is($item->miny, 150, 'write in_hours 2.5 read raw');
226 is($item->miny_as_minutes, 30, 'write in_hours 2.5 read as_minutes');
227 is($item->miny_as_hours, 2, 'write in_hours 2.5 read as_hours');
228 is($item->miny_in_hours, 2.5, 'write in_hours 2.5 read in_hours');
229 is($item->miny_in_hours_as_number, '2,50', 'write in_hours 2.5 read in_hours_as_number');
231 $item = new_item(miny_in_hours_as_number => '4,25');
232 is($item->miny, 255, 'write in_hours_as_number 4,25 read raw');
233 is($item->miny_as_minutes, 15, 'write in_hours_as_number 4,25 read as_minutes');
234 is($item->miny_as_hours, 4, 'write in_hours_as_number 4,25 read as_hours');
235 is($item->miny_in_hours, 4.25, 'write in_hours_as_number 4,25 read in_hours');
236 is($item->miny_in_hours_as_number, '4,25', 'write in_hours_as_number 4,25 read in_hours_as_number');
238 # Parametervalidierung
239 throws_ok { new_item()->inty_as_duration_string('invalid') } qr/invalid.*format/i, 'invalid duration format';