9 use MogileFS::Util qw(error_code);
11 use MogileFS::Factory;
12 use MogileFS::Factory::Domain;
13 use MogileFS::Factory::Class;
17 use Data::Dumper qw/Dumper/;
19 my $sto = eval { temp_store(); };
23 plan skip_all => "Can't create temporary test database: $@";
27 # Fetch the factories.
28 my $domfac = MogileFS::Factory::Domain->get_factory;
29 ok($domfac, "got a domain factory");
30 my $classfac = MogileFS::Factory::Class->get_factory;
31 ok($classfac, "got a class factory");
33 # Ensure the inherited singleton is good.
34 ok($domfac != $classfac, "factories are not the same singleton");
37 # Add in a test domain.
38 my $dom = $domfac->set({ dmid => 1, namespace => 'toast'});
39 ok($dom, "made a new domain object");
40 is($dom->id, 1, "domain id is 1");
41 is($dom->name, 'toast', 'domain namespace is toast');
43 # Add in a test class.
44 my $cls = $classfac->set({ classid => 1, dmid => 1, mindevcount => 3,
45 replpolicy => '', classname => 'fried'});
46 ok($cls, "got a class object");
47 is($cls->id, 1, "class id is 1");
48 is($cls->name, 'fried', 'class name is fried');
49 is(ref($cls->domain), 'MogileFS::Domain',
50 'class can find a domain object');
53 # Add a few more classes and domains.
55 my $dom2 = $domfac->set({ dmid => 2, namespace => 'harro' });
56 $classfac->set({ classid => 1, dmid => 2, mindevcount => 2,
57 replpolicy => '', classname => 'red' });
58 $classfac->set({ classid => 2, dmid => 2, mindevcount => 3,
59 replpolicy => 'MultipleHosts(2)', classname => 'green' });
60 $classfac->set({ classid => 3, dmid => 2, mindevcount => 4,
61 replpolicy => 'MultipleHosts(5)', classname => 'blue' });
64 # Ensure the select and remove factory methods work.
66 my $dom = $domfac->get_by_id(1);
67 is($dom->name, 'toast', 'got the right domain from get_by_id');
71 my $dom = $domfac->get_by_name('harro');
72 is($dom->id, 2, 'got the right domain from get_by_name');
76 my @doms = $domfac->get_all;
77 is(scalar(@doms), 2, 'got two domains back from get_all');
79 is(ref($_), 'MogileFS::Domain', 'and both are domains');
81 isnt($doms[0]->id, $doms[1]->id, 'and both are not the same');
85 my $dom = $domfac->get_by_name('harro');
86 my $clsmap = $classfac->map_by_id($dom);
87 is(ref($clsmap), 'HASH', 'got a mapped class hash');
88 is($clsmap->{2}->name, 'green', 'got the right class set');
90 $classfac->remove($clsmap->{2});
92 my $cls = $classfac->get_by_name($dom, 'green');
93 ok(!$cls, "class removed from factory");
96 # Test the domain routines harder.
98 my $dom = $domfac->get_by_name('harro');
99 my @classes = $dom->classes;
100 # Magic "default" class is included
101 is(scalar(@classes), 3, 'found three classes');
103 ok($dom->class('blue'), 'found the blue class');
104 ok(!$dom->class('fried'), 'did not find the fried class');
107 # Test the class routines harder.
109 my $dom = $domfac->get_by_name('harro');
110 my $cls = $dom->class('blue');
111 my $polobj = $cls->repl_policy_obj;
112 ok($polobj, 'class can create policy object');
115 # Add a domain and two classes to the DB.
117 my $domid = $sto->create_domain('foo');
118 ok($domid, 'new domain stored in database: ' . $domid);
120 my $clsid1 = $sto->create_class($domid, 'bar');
121 my $clsid2 = $sto->create_class($domid, 'baz');
122 is($clsid1, 1, 'new class1 stored in database');
123 is($clsid2, 2, 'new class2 stored in database');
125 ok($sto->update_class_mindevcount(dmid => $domid, classid => $clsid2,
126 mindevcount => 3), 'can set mindevcount');
127 ok($sto->update_class_replpolicy(dmid => $domid, classid => $clsid2,
128 replpolicy => 'MultipleHosts(6)'), 'can set replpolicy');
129 ok($sto->update_class_name(dmid => $domid, classid => $clsid2,
130 classname => 'boo'), 'can rename class');
131 ok($sto->update_class_hashtype(dmid => $domid, classid => $clsid2,
132 hashtype => 1), 'can set checksum type');
133 ok($sto->update_class_hashtype(dmid => $domid, classid => $clsid2,
134 hashtype => undef), 'can unset checksum type');
138 # Reload from the DB and confirm they came back the way they went in.
139 my %domains = $sto->get_all_domains;
140 ok(exists $domains{foo}, 'domain foo exists');
141 is($domains{foo}, 1, 'and the id is 1');
142 my @classes = $sto->get_all_classes;
143 is_deeply($classes[0], {
144 'replpolicy' => undef,
147 'mindevcount' => '2',
148 'classname' => 'bar',
150 }, 'class bar came back');
151 # We edited class2 a bunch, make sure that all stuck.
152 is_deeply($classes[1], {
153 'replpolicy' => 'MultipleHosts(6)',
156 'mindevcount' => '3',
157 'classname' => 'boo',
159 }, 'class baz came back as boo');