Translate incoming typing notifications from libpurple into chatstates.
[thrasher.git] / perl / tests / dbi_backend_mysql_innodb.pl
blob52f881026c7582a2a4e71481ead2c75b92806463
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 # This tests the MySQL InnoDB backend by directly manipulating it.
7 # Be aware that this will involve the random creation and
8 # destruction of a MySQL database called
9 # "dbi_backend_mysql_innodb_test", which is given a klunky name
10 # on purpose. If this test runs to completion, it will clean
11 # up after itself, but if it crashes it won't. This is of
12 # course done so the last state of the DB can be examined.
14 use Test::More 'no_plan';
15 use Data::Dumper;
16 use IPC::Run qw(run);
17 use Test::Deep;
19 BEGIN {
20 use_ok 'Thrasher::Test', qw(:all);
21 use_ok 'Thrasher::Backend::DBI';
24 my ($username, $password) = get_mysql_username_password;
26 my $db_name = 'dbi_backend_mysql_innodb_test';
28 my $args_hash =
29 {dbi_data_source => "dbi:mysql:$db_name",
30 username => $username, password => $password,
31 db_driver => 'mysql_innodb',
32 database_name => $db_name,
33 transport_name => 'test'};
35 VERIFY_ALL_REQUIRED_PARAMS: {
36 for my $key (keys %$args_hash) {
37 my %hash_copy = %$args_hash;
38 delete $hash_copy{$key};
39 dies {
40 new Thrasher::Backend::DBI(\%hash_copy);
41 } 'missing', "required param $key is required";
45 DESTROY_DATABASE: {
46 my $out;
47 run(["mysql",
48 $username ? ("--user=$username") : (),
49 $password ? ("--password=$password") : (),
50 "-e", "DROP DATABASE $db_name"],
51 '>&', \$out);
53 if ($out =~ /exist/) {
54 pass("$db_name didn't exist");
55 } elsif (!$out) {
56 pass("$db_name existed and was dropped");
57 } else {
58 die "$db_name was neither seen to be nonexistant, nor "
59 ."dropped. Out: $out";
63 my $db;
65 REAL_DB_LOAD: {
66 $db = new Thrasher::Backend::DBI($args_hash);
68 # Let's verify all the tables exist.
69 for my $table (@Thrasher::Backend::DBI::tables) {
70 my $sql = $db->sql('detect_table');
71 # Have to manually do this since 'tablename' doesn't
72 # work, which is what ? normally would expand to
73 $sql =~ s/\?/$table/;
74 my ($val) = $db->{dbh}->selectrow_array($sql);
75 if (!$val) {
76 # die, since there's not much point continuing on.
77 die "Table $table was not created correctly.";
79 pass("Database table $table created");
83 my $jid = "romeo\@montague.lit";
85 REGISTRATION: {
86 my $jid_id = $db->jid_id($jid);
87 is($jid_id, undef, 'user initially not found');
89 my $registration_info = $db->registered($jid);
90 is($registration_info, undef,
91 'user initially not registered');
92 $db->register('romeo@montague.lit',
93 {username => 'romeo@montague.lit',
94 password => 'starcrossed'});
95 $registration_info = $db->registered($jid);
96 cmp_deeply($registration_info,
97 {username => 'romeo@montague.lit',
98 password => 'starcrossed'},
99 'properly registered romeo');
101 $jid_id = $db->jid_id($jid);
102 isnt($jid_id, undef, 'romeo has an id');
104 $db->register('romeo@montague.lit',
105 {username => 'romeo@montague.lit',
106 password => 'moo'});
107 $registration_info = $db->registered($jid);
108 cmp_deeply($registration_info,
109 {username => 'romeo@montague.lit',
110 password => 'moo'},
111 'new registration information used');
113 my $new_jid_id = $db->jid_id($jid);
114 is($new_jid_id, $jid_id, 'same JID id used');
116 # Verify that registering with too much info works;
117 # we had a real problem with this with our client.
118 ok($db->register($jid,
119 {username => 'romeo@montague.lit',
120 password => 'ILoveJuliet',
121 nick => "RomeoMyRomeo"}));
122 $registration_info = $db->registered($jid);
123 cmp_deeply($registration_info,
124 {username => 'romeo@montague.lit',
125 password => 'ILoveJuliet'},
126 "fields the DB doesn't understand are correctly "
127 ."filtered out, instead of killing everything");
130 NAME_MAPPING: {
131 # Verify the three basic name-mapping cases:
132 # * Incoming new legacy name
133 # * Correctly-mapped legacy to JID
134 # * Blindly-mapped JID to legacy.
136 my $jid1 = $db->legacy_name_to_jid($jid, 'A@B',
137 'aim.transport');
138 is($jid1, 'a%b@aim.transport',
139 'correctly translates legacy names as expected');
141 $jid1 = $db->legacy_name_to_jid($jid, 'A@B',
142 'aim.transport');
143 is($jid1, 'a%b@aim.transport',
144 'correctly looks up legacy names');
146 my $legacy = $db->jid_to_legacy_name($jid, 'a%b@aim.transport');
147 is($legacy, 'A@B', 'jid_to_legacy_name can correctly look things up');
149 # Something we haven't seen yet
150 my $legacy2 = $db->jid_to_legacy_name($jid,
151 'me%ga@aim.transport');
152 is($legacy2, 'me%ga', 'correctly forged a legacy name');
154 # For somebody else, we get a%b
155 $legacy = $db->jid_to_legacy_name('bill@montague.lit',
156 'a%b@aim.transport');
157 is($legacy, 'a%b', 'correct separation by JID');
160 AVATARS: {
161 my $avatar1 = "EBCDIC";
162 my $avatar2 = "ASCII";
164 is($db->get_avatar($jid, 'a@b'), undef,
165 'no avatar initially');
167 $db->set_avatar($jid, 'a@b', $avatar1);
168 is($db->get_avatar($jid, 'a@b'), $avatar1,
169 'can correctly store and retrieve an avatar');
171 $db->set_avatar($jid, 'a@b', $avatar2);
172 is($db->get_avatar($jid, 'a@b'), $avatar2,
173 'correctly retrieves the changed avatar');
175 $db->set_avatar($jid, 'a@b', undef);
176 is($db->get_avatar($jid, 'a@b'), undef,
177 'can delete avatar');
179 $db->set_avatar($jid, 'a@b', $avatar2);
182 ROSTER: {
183 my $roster = $db->get_roster($jid);
185 cmp_deeply($roster, {}, 'has no initial roster');
187 $db->set_roster_user_state($jid, 'a@b', $db->want_subscribe);
188 $db->set_roster_user_state($jid, 'b@b', $db->subscribed);
190 $roster = $db->get_roster($jid);
191 cmp_deeply($roster, {'A@B' => $db->want_subscribe,
192 'b@b' => $db->subscribed},
193 'can get roster');
195 $db->set_roster_user_state($jid, 'b@b', $db->unsubscribed);
196 $roster = $db->get_roster($jid);
197 cmp_deeply($roster, {'A@B' => $db->want_subscribe},
198 'setting people to unsubscribed deletes that entry');
201 MISC: {
202 is($db->get_misc($jid, 'test'), undef,
203 'no value yet');
204 $db->set_misc($jid, 'test', 'moo');
205 is($db->get_misc($jid, 'test'), 'moo',
206 'can store misc values');
209 ALL_OF_VARIOUS_THINGS: {
210 cmp_deeply($db->all_jids, [$jid],
211 'all_jids works correctly');
212 cmp_deeply($db->all_mappings('romeo@montague.lit'),
213 {'me%ga' => 'me%ga@aim.transport',
214 'A@B' => 'a%b@aim.transport'},
215 'can get all name mappings for a user');
216 cmp_deeply($db->all_avatars('romeo@montague.lit'),
217 {'A@B' => 'ASCII'},
218 'can retrieve all avatars');
219 cmp_deeply($db->all_misc('romeo@montague.lit'),
220 {test => 'moo'}, 'can retrieve all misc');
223 UNREGISTER: {
224 ok($db->registered('romeo@montague.lit'),
225 'romeo is currently registered.');
226 $db->remove('romeo@montague.lit');
227 is($db->registered('romeo@montague.lit'), undef,
228 'romeo is no longer registered.');
231 DROP_DATABASE: {
232 last;
233 my $out;
234 run(["mysql",
235 $username ? ("--user=$username") : (),
236 $password ? ("--password=$password") : (),
237 "-e", "DROP DATABASE $db_name"],
238 '>&', \$out);