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' },
13 use SL::DB::Helper::AttrDuration;
15 __PACKAGE__->attr_duration('dummy');
16 __PACKAGE__->attr_duration_minutes('inty');
20 use Test::More tests => 120;
29 use Support::TestSetup;
33 return AttrDurationTestDummy->new(@_);
36 Support::TestSetup::login();
39 $::locale = Locale->new('en');
43 # Wenn das Attribut undef ist:
44 is(new_item->dummy, undef, 'uninitialized: raw');
45 is(new_item->dummy_as_hours, 0, 'uninitialized: as_hours');
46 is(new_item->dummy_as_minutes, 0, 'uninitialized: as_minutes');
47 is(new_item->dummy_as_duration_string, undef, 'uninitialized: as_duration_string');
48 is(new_item->dummy_as_man_days, 0, 'uninitialized: as_man_days');
49 is(new_item->dummy_as_man_days_unit, 'h', 'uninitialized: as_man_days_unit');
50 is(new_item->dummy_as_man_days_string, '0,00', 'uninitialized: as_man_days_string');
52 # Auslesen kleiner 8 Stunden:
53 is(new_item(dummy => 2.75)->dummy, 2.75, 'initialized < 8: raw');
54 is(new_item(dummy => 2.75)->dummy_as_hours, 2, 'initialized < 8: as_hours');
55 is(new_item(dummy => 2.75)->dummy_as_minutes, 45, 'initialized < 8: as_minutes');
56 is(new_item(dummy => 2.75)->dummy_as_duration_string, '2,75', 'initialized < 8: as_duration_string');
57 is(new_item(dummy => 2.75)->dummy_as_man_days, 2.75, 'initialized < 8: as_man_days');
58 is(new_item(dummy => 2.75)->dummy_as_man_days_unit, 'h', 'initialized < 8: as_man_days_unit');
59 is(new_item(dummy => 2.75)->dummy_as_man_days_string, '2,75', 'initialized < 8: as_man_days_string');
61 # Auslesen größer 8 Stunden:
62 is(new_item(dummy => 12.5)->dummy, 12.5, 'initialized > 8: raw');
63 is(new_item(dummy => 12.5)->dummy_as_hours, 12, 'initialized > 8: as_hours');
64 is(new_item(dummy => 12.5)->dummy_as_minutes, 30, 'initialized > 8: as_minutes');
65 is(new_item(dummy => 12.5)->dummy_as_duration_string, '12,50', 'initialized > 8: as_duration_string');
66 is(new_item(dummy => 12.5)->dummy_as_man_days, 1.5625, 'initialized > 8: as_man_days');
67 is(new_item(dummy => 12.5)->dummy_as_man_days_unit, 'man_day', 'initialized > 8: as_man_days_unit');
68 is(new_item(dummy => 12.5)->dummy_as_man_days_string, '1,56', 'initialized > 8: as_man_days_string');
70 $item = new_item(dummy => 2.25); $item->dummy_as_duration_string(undef);
71 is($item->dummy, undef, 'write as_duration_string undef read raw');
72 is($item->dummy_as_minutes, 0, 'write as_duration_string undef read as_minutes');
73 is($item->dummy_as_hours, 0, 'write as_duration_string undef read as_hours');
74 is($item->dummy_as_duration_string, undef, 'write as_duration_string undef read as_duration_string');
76 $item = new_item(dummy => 2.25); $item->dummy_as_duration_string("4,80");
77 is($item->dummy, 4.8, 'write as_duration_string 4,80 read raw');
78 is($item->dummy_as_minutes, 48, 'write as_duration_string 4,80 read as_minutes');
79 is($item->dummy_as_hours, 4, 'write as_duration_string 4,80 read as_hours');
80 is($item->dummy_as_duration_string, "4,80", 'write as_duration_string 4,80 read as_duration_string');
82 $item = new_item(dummy => 2.25); $item->dummy_as_minutes(12);
83 is($item->dummy, 2.2, 'write as_minutes 12 read raw');
84 is($item->dummy_as_minutes, 12, 'write as_minutes 12 read as_minutes');
85 is($item->dummy_as_hours, 2, 'write as_minutes 12 read as_hours');
86 is($item->dummy_as_duration_string, "2,20", 'write as_minutes 12 read as_duration_string');
88 $item = new_item(dummy => 2.25); $item->dummy_as_hours(5);
89 is($item->dummy, 5.25, 'write as_hours 5 read raw');
90 is($item->dummy_as_minutes, 15, 'write as_hours 5 read as_minutes');
91 is($item->dummy_as_hours, 5, 'write as_hours 5 read as_hours');
92 is($item->dummy_as_duration_string, "5,25", 'write as_hours 5 read as_duration_string');
94 $item = new_item(dummy => undef);
95 is($item->dummy, undef, 'write raw undef read raw');
96 is($item->dummy_as_man_days, 0, 'write raw undef read as_man_days');
97 is($item->dummy_as_man_days_unit, 'h', 'write raw undef read as_man_days_unit');
98 is($item->dummy_as_man_days_string, '0,00', 'write raw undef read as_man_days_string');
100 $item = new_item(dummy => 4);
101 is($item->dummy, 4, 'write raw 4 read raw');
102 is($item->dummy_as_man_days, 4, 'write raw 4 read as_man_days');
103 is($item->dummy_as_man_days_unit, 'h', 'write raw 4 read as_man_days_unit');
104 is($item->dummy_as_man_days_string, '4,00', 'write raw 4 read as_man_days_string');
106 $item = new_item(dummy => 18);
107 is($item->dummy, 18, 'write raw 18 read raw');
108 is($item->dummy_as_man_days, 2.25, 'write raw 18 read as_man_days');
109 is($item->dummy_as_man_days_unit, 'man_day', 'write raw 18 read as_man_days_unit');
110 is($item->dummy_as_man_days_string, '2,25', 'write raw 18 read as_man_days_string');
112 $item = new_item(dummy => 4);
113 is($item->dummy, 4, 'should not change anything when writing undef: write raw 4 read raw');
114 is($item->dummy_as_man_days(undef), undef, 'should not change anything when writing undef: write as_man_days undef return undef');
115 is($item->dummy, 4, 'should not change anything when writing undef: read raw 2');
116 is($item->dummy_as_man_days_unit(undef), undef, 'should not change anything when writing undef: write as_man_days_unit undef return undef');
117 is($item->dummy, 4, 'should not change anything when writing undef: read raw 3');
118 is($item->dummy_as_man_days_string(undef), undef, 'should not change anything when writing undef: write as_man_days_string undef return undef');
119 is($item->dummy, 4, 'should not change anything when writing undef: read raw 4');
123 is($item->dummy(2), 2, 'parse less than a man day: write raw 2 read raw');
124 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');
125 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');
128 is($item->dummy(12), 12, 'parse more than a man day: write raw 12 read raw');
129 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');
130 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');
133 is($item->dummy(3.25), 3.25, 'parse less than a man day with unit h: write raw 3.25 read raw');
134 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');
135 is($item->dummy, 3.25, 'parse less than a man day with unit h: read raw');
138 is($item->dummy(3.25), 3.25, 'parse less than a man day with unit hour: write raw 3.25 read raw');
139 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');
140 is($item->dummy, 3.25, 'parse less than a man day with unit hour: read raw');
143 is($item->dummy(3.25), 3.25, 'parse more than a man day with unit man_day: write raw 3.25 read raw');
144 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');
145 is($item->dummy, 26, 'parse more than a man day with unit man_day: read raw');
147 is(new_item->assign_attributes(dummy_as_man_days => 3, dummy_as_man_days_unit => 'h')->dummy, 3, 'assign_attributes hash 3h');
148 is(new_item->assign_attributes(dummy_as_man_days_unit => 'h', dummy_as_man_days => 3 )->dummy, 3, 'assign_attributes hash h3');
150 is(new_item->assign_attributes(dummy_as_man_days => 3, dummy_as_man_days_unit => 'man_day')->dummy, 24, 'assign_attributes hash 3man_day');
151 is(new_item->assign_attributes(dummy_as_man_days_unit => 'man_day', dummy_as_man_days => 3 )->dummy, 24, 'assign_attributes hash man_day3');
153 is(new_item->assign_attributes('dummy_as_man_days', 3, 'dummy_as_man_days_unit', 'h')->dummy, 3, 'assign_attributes array 3h');
154 is(new_item->assign_attributes('dummy_as_man_days_unit', 'h', 'dummy_as_man_days', 3 )->dummy, 3, 'assign_attributes array h3');
156 is(new_item->assign_attributes('dummy_as_man_days', 3, 'dummy_as_man_days_unit', 'man_day')->dummy, 24, 'assign_attributes array 3man_day');
157 is(new_item->assign_attributes('dummy_as_man_days_unit', 'man_day', 'dummy_as_man_days', 3 )->dummy, 24, 'assign_attributes array man_day3');
159 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');
160 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');
162 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');
163 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');
165 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');
166 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');
168 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');
169 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');
171 # Parametervalidierung
172 throws_ok { new_item()->dummy_as_man_days_unit('invalid') } qr/unknown.*unit/i, 'unknown unit';
173 lives_ok { new_item()->dummy_as_man_days_unit('h') } 'known unit h';
174 lives_ok { new_item()->dummy_as_man_days_unit('hour') } 'known unit hour';
175 lives_ok { new_item()->dummy_as_man_days_unit('man_day') } 'known unit man_day';
177 ### attr_duration_minutes
179 # Wenn das Attribut undef ist:
180 is(new_item->inty, undef, 'uninitialized: raw');
181 is(new_item->inty_as_hours, 0, 'uninitialized: as_hours');
182 is(new_item->inty_as_minutes, 0, 'uninitialized: as_minutes');
183 is(new_item->inty_as_duration_string, undef, 'uninitialized: as_duration_string');
185 # Auslesen kleiner 60 Minuten:
186 is(new_item(inty => 37)->inty, 37, 'initialized < 60: raw');
187 is(new_item(inty => 37)->inty_as_hours, 0, 'initialized < 60: as_hours');
188 is(new_item(inty => 37)->inty_as_minutes, 37, 'initialized < 60: as_minutes');
189 is(new_item(inty => 37)->inty_as_duration_string, '0:37', 'initialized < 60: as_duration_string');
191 # Auslesen größer 60 Minuten:
192 is(new_item(inty => 145)->inty, 145, 'initialized > 60: raw');
193 is(new_item(inty => 145)->inty_as_hours, 2, 'initialized > 60: as_hours');
194 is(new_item(inty => 145)->inty_as_minutes, 25, 'initialized > 60: as_minutes');
195 is(new_item(inty => 145)->inty_as_duration_string, '2:25', 'initialized > 60: as_duration_string');
197 $item = new_item(inty => 145); $item->inty_as_duration_string(undef);
198 is($item->inty, undef, 'write as_duration_string undef read raw');
199 is($item->inty_as_minutes, 0, 'write as_duration_string undef read as_minutes');
200 is($item->inty_as_hours, 0, 'write as_duration_string undef read as_hours');
201 is($item->inty_as_duration_string, undef, 'write as_duration_string undef read as_duration_string');
203 $item = new_item(inty => 145); $item->inty_as_duration_string('');
204 is($item->inty, undef, 'write as_duration_string "" read raw');
205 is($item->inty_as_minutes, 0, 'write as_duration_string "" read as_minutes');
206 is($item->inty_as_hours, 0, 'write as_duration_string "" read as_hours');
207 is($item->inty_as_duration_string, undef, 'write as_duration_string "" read as_duration_string');
209 $item = new_item(inty => 145); $item->inty_as_duration_string("3:21");
210 is($item->inty, 201, 'write as_duration_string 3:21 read raw');
211 is($item->inty_as_minutes, 21, 'write as_duration_string 3:21 read as_minutes');
212 is($item->inty_as_hours, 3, 'write as_duration_string 3:21 read as_hours');
213 is($item->inty_as_duration_string, "3:21", 'write as_duration_string 3:21 read as_duration_string');
215 $item = new_item(inty => 145); $item->inty_as_duration_string("03:1");
216 is($item->inty, 181, 'write as_duration_string 03:1 read raw');
217 is($item->inty_as_minutes, 1, 'write as_duration_string 03:1 read as_minutes');
218 is($item->inty_as_hours, 3, 'write as_duration_string 03:1 read as_hours');
219 is($item->inty_as_duration_string, "3:01", 'write as_duration_string 03:1 read as_duration_string');
221 # Parametervalidierung
222 throws_ok { new_item()->inty_as_duration_string('invalid') } qr/invalid.*format/i, 'invalid duration format';