1 package AttrDurationTestDummy;
3 use base qw(SL::DB::Object);
5 __PACKAGE__->meta->setup(
7 columns => [ dummy => { type => 'numeric', precision => 2, scale => 12 }, ]
10 use SL::DB::Helper::AttrDuration;
12 __PACKAGE__->attr_duration('dummy');
16 use Test::More tests => 91;
25 use Support::TestSetup;
28 return AttrDurationTestDummy->new(@_);
31 Support::TestSetup::login();
34 # Wenn das Attribut undef ist:
35 is(new_item->dummy, undef, 'uninitialized: raw');
36 is(new_item->dummy_as_hours, 0, 'uninitialized: as_hours');
37 is(new_item->dummy_as_minutes, 0, 'uninitialized: as_minutes');
38 is(new_item->dummy_as_duration_string, undef, 'uninitialized: as_duration_string');
39 is(new_item->dummy_as_man_days, 0, 'uninitialized: as_man_days');
40 is(new_item->dummy_as_man_days_unit, 'h', 'uninitialized: as_man_days_unit');
41 is(new_item->dummy_as_man_days_string, '0,00', 'uninitialized: as_man_days_string');
43 # Auslesen kleiner 8 Stunden:
44 is(new_item(dummy => 2.75)->dummy, 2.75, 'initialized < 8: raw');
45 is(new_item(dummy => 2.75)->dummy_as_hours, 2, 'initialized < 8: as_hours');
46 is(new_item(dummy => 2.75)->dummy_as_minutes, 45, 'initialized < 8: as_minutes');
47 is(new_item(dummy => 2.75)->dummy_as_duration_string, '2,75', 'initialized < 8: as_duration_string');
48 is(new_item(dummy => 2.75)->dummy_as_man_days, 2.75, 'initialized < 8: as_man_days');
49 is(new_item(dummy => 2.75)->dummy_as_man_days_unit, 'h', 'initialized < 8: as_man_days_unit');
50 is(new_item(dummy => 2.75)->dummy_as_man_days_string, '2,75', 'initialized < 8: as_man_days_string');
52 # Auslesen größer 8 Stunden:
53 is(new_item(dummy => 12.5)->dummy, 12.5, 'initialized > 8: raw');
54 is(new_item(dummy => 12.5)->dummy_as_hours, 12, 'initialized > 8: as_hours');
55 is(new_item(dummy => 12.5)->dummy_as_minutes, 30, 'initialized > 8: as_minutes');
56 is(new_item(dummy => 12.5)->dummy_as_duration_string, '12,50', 'initialized > 8: as_duration_string');
57 is(new_item(dummy => 12.5)->dummy_as_man_days, 1.5625, 'initialized > 8: as_man_days');
58 is(new_item(dummy => 12.5)->dummy_as_man_days_unit, 'man_day', 'initialized > 8: as_man_days_unit');
59 is(new_item(dummy => 12.5)->dummy_as_man_days_string, '1,56', 'initialized > 8: as_man_days_string');
61 $item = new_item(dummy => 2.25); $item->dummy_as_duration_string(undef);
62 is($item->dummy, undef, 'write as_duration_string undef read raw');
63 is($item->dummy_as_minutes, 0, 'write as_duration_string undef read as_minutes');
64 is($item->dummy_as_hours, 0, 'write as_duration_string undef read as_hours');
65 is($item->dummy_as_duration_string, undef, 'write as_duration_string undef read as_duration_string');
67 $item = new_item(dummy => 2.25); $item->dummy_as_duration_string("4,80");
68 is($item->dummy, 4.8, 'write as_duration_string 4,80 read raw');
69 is($item->dummy_as_minutes, 48, 'write as_duration_string 4,80 read as_minutes');
70 is($item->dummy_as_hours, 4, 'write as_duration_string 4,80 read as_hours');
71 is($item->dummy_as_duration_string, "4,80", 'write as_duration_string 4,80 read as_duration_string');
73 $item = new_item(dummy => 2.25); $item->dummy_as_minutes(12);
74 is($item->dummy, 2.2, 'write as_minutes 12 read raw');
75 is($item->dummy_as_minutes, 12, 'write as_minutes 12 read as_minutes');
76 is($item->dummy_as_hours, 2, 'write as_minutes 12 read as_hours');
77 is($item->dummy_as_duration_string, "2,20", 'write as_minutes 12 read as_duration_string');
79 $item = new_item(dummy => 2.25); $item->dummy_as_hours(5);
80 is($item->dummy, 5.25, 'write as_hours 5 read raw');
81 is($item->dummy_as_minutes, 15, 'write as_hours 5 read as_minutes');
82 is($item->dummy_as_hours, 5, 'write as_hours 5 read as_hours');
83 is($item->dummy_as_duration_string, "5,25", 'write as_hours 5 read as_duration_string');
85 $item = new_item(dummy => undef);
86 is($item->dummy, undef, 'write raw undef read raw');
87 is($item->dummy_as_man_days, 0, 'write raw undef read as_man_days');
88 is($item->dummy_as_man_days_unit, 'h', 'write raw undef read as_man_days_unit');
89 is($item->dummy_as_man_days_string, '0,00', 'write raw undef read as_man_days_string');
91 $item = new_item(dummy => 4);
92 is($item->dummy, 4, 'write raw 4 read raw');
93 is($item->dummy_as_man_days, 4, 'write raw 4 read as_man_days');
94 is($item->dummy_as_man_days_unit, 'h', 'write raw 4 read as_man_days_unit');
95 is($item->dummy_as_man_days_string, '4,00', 'write raw 4 read as_man_days_string');
97 $item = new_item(dummy => 18);
98 is($item->dummy, 18, 'write raw 18 read raw');
99 is($item->dummy_as_man_days, 2.25, 'write raw 18 read as_man_days');
100 is($item->dummy_as_man_days_unit, 'man_day', 'write raw 18 read as_man_days_unit');
101 is($item->dummy_as_man_days_string, '2,25', 'write raw 18 read as_man_days_string');
103 $item = new_item(dummy => 4);
104 is($item->dummy, 4, 'should not change anything when writing undef: write raw 4 read raw');
105 is($item->dummy_as_man_days(undef), undef, 'should not change anything when writing undef: write as_man_days undef return undef');
106 is($item->dummy, 4, 'should not change anything when writing undef: read raw 2');
107 is($item->dummy_as_man_days_unit(undef), undef, 'should not change anything when writing undef: write as_man_days_unit undef return undef');
108 is($item->dummy, 4, 'should not change anything when writing undef: read raw 3');
109 is($item->dummy_as_man_days_string(undef), undef, 'should not change anything when writing undef: write as_man_days_string undef return undef');
110 is($item->dummy, 4, 'should not change anything when writing undef: read raw 4');
114 is($item->dummy(2), 2, 'parse less than a man day: write raw 2 read raw');
115 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');
116 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');
119 is($item->dummy(12), 12, 'parse more than a man day: write raw 12 read raw');
120 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');
121 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');
124 is($item->dummy(3.25), 3.25, 'parse less than a man day with unit h: write raw 3.25 read raw');
125 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');
126 is($item->dummy, 3.25, 'parse less than a man day with unit h: read raw');
129 is($item->dummy(3.25), 3.25, 'parse less than a man day with unit hour: write raw 3.25 read raw');
130 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');
131 is($item->dummy, 3.25, 'parse less than a man day with unit hour: read raw');
134 is($item->dummy(3.25), 3.25, 'parse more than a man day with unit man_day: write raw 3.25 read raw');
135 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');
136 is($item->dummy, 26, 'parse more than a man day with unit man_day: read raw');
138 is(new_item->assign_attributes(dummy_as_man_days => 3, dummy_as_man_days_unit => 'h')->dummy, 3, 'assign_attributes hash 3h');
139 is(new_item->assign_attributes(dummy_as_man_days_unit => 'h', dummy_as_man_days => 3 )->dummy, 3, 'assign_attributes hash h3');
141 is(new_item->assign_attributes(dummy_as_man_days => 3, dummy_as_man_days_unit => 'man_day')->dummy, 24, 'assign_attributes hash 3man_day');
142 is(new_item->assign_attributes(dummy_as_man_days_unit => 'man_day', dummy_as_man_days => 3 )->dummy, 24, 'assign_attributes hash man_day3');
144 is(new_item->assign_attributes('dummy_as_man_days', 3, 'dummy_as_man_days_unit', 'h')->dummy, 3, 'assign_attributes array 3h');
145 is(new_item->assign_attributes('dummy_as_man_days_unit', 'h', 'dummy_as_man_days', 3 )->dummy, 3, 'assign_attributes array h3');
147 is(new_item->assign_attributes('dummy_as_man_days', 3, 'dummy_as_man_days_unit', 'man_day')->dummy, 24, 'assign_attributes array 3man_day');
148 is(new_item->assign_attributes('dummy_as_man_days_unit', 'man_day', 'dummy_as_man_days', 3 )->dummy, 24, 'assign_attributes array man_day3');
150 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');
151 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');
153 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');
154 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');
156 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');
157 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');
159 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');
160 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');
162 # Parametervalidierung
163 throws_ok { new_item()->dummy_as_man_days_unit('invalid') } qr/unknown.*unit/i, 'unknown unit';
164 lives_ok { new_item()->dummy_as_man_days_unit('h') } 'known unit h';
165 lives_ok { new_item()->dummy_as_man_days_unit('hour') } 'known unit hour';
166 lives_ok { new_item()->dummy_as_man_days_unit('man_day') } 'known unit man_day';