Skip to content

Commit

Permalink
First SQLite test and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen authored and jhthorsen committed Feb 16, 2023
1 parent 4fc9c12 commit be2afa6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
33 changes: 21 additions & 12 deletions lib/Convos/Core/Backend/SQLite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@ use Convos::Date qw(dt);
use Mojo::JSON qw(false true);

has home => sub { Carp::confess('home() cannot be built') };
has sqlite => sub { Mojo::SQLite->new('sqlite:' . shift->home->child('convos.sqlite')) };
has sqlite => sub {
my $self = shift;
$self->home->make_path unless -d $self->home;

my $sqlite = Mojo::SQLite->new('sqlite:' . $self->home->child('convos.sqlite'));
$sqlite->migrations->from_file(...)->migrate;
return $sqlite;
};

sub connections_p {
my ($self, $user) = @_;

return $self->sqlite->db->select_p('convos_connections')->then(sub {
return $self->_db->select_p('convos_connections')->then(sub {
return shift->hashes->to_array;
});
}

sub delete_messages_p {
my ($self, $obj) = @_;
return Mojo::Promise->reject('Unknown target.') unless $obj and $obj->connection;
return $self->sqlite->db->delete_p(convos_messages => {conversation_id => $obj->id})
->then(sub {$obj});
return $self->_db->delete_p(convos_messages => {conversation_id => $obj->id})->then(sub {$obj});
}

sub delete_object_p {
Expand All @@ -30,13 +36,13 @@ sub delete_object_p {
$obj->unsubscribe($_) for qw(conversation message state);
}

return $self->delete_p($self->_obj_to_table($obj), {id => $obj->id})->then(sub {$obj});
return $self->_db->delete_p($self->_obj_to_table($obj), {id => $obj->id})->then(sub {$obj});
}

sub load_object_p {
my ($self, $obj) = @_;

return $self->select_p($self->_obj_to_table($obj), {id => $obj->id})->then(sub {
return $self->_db->select_p($self->_obj_to_table($obj), {id => $obj->id})->then(sub {
return shift->hash;
});
}
Expand Down Expand Up @@ -68,7 +74,7 @@ sub messages_p {
push @{$where{ts}}, {$gt => dt $query->{after}} if $query->{after};
push @{$where{ts}}, {$lt => dt $query->{before}} if $query->{before};

return $self->select_p(convos_messages => \%where, \%extra)->then(sub {
return $self->_db->select_p(convos_messages => \%where, \%extra)->then(sub {
return shift->hashes->to_array;
});
}
Expand All @@ -79,21 +85,22 @@ sub notifications_p {
my %extra = (limit => $query->{limit} || 60);
$extra{order_by} = {-desc => 'ts'};

return $self->select_p(convos_notifications => {}, \%extra)->then(sub {
return $self->_db->select_p(convos_notifications => {}, \%extra)->then(sub {
return shift->hashes->to_array;
});
}

sub save_object_p {
my ($self, $obj) = @_;

return $self->insert_p($self->_obj_to_table($obj), $obj->TO_JSON('private'))->then(sub {$obj});
return $self->_db->insert_p($self->_obj_to_table($obj), $obj->TO_JSON('private'))
->then(sub {$obj});
}

sub users_p {
my $self = shift;

return $self->sqlite->db->select_p('convos_users')->then(sub {
return $self->_db->select_p('convos_users')->then(sub {
return shift->hashes->sort(sub {
$a->{registered} cmp $b->{registered} || $a->{email} cmp $b->{email};
})->to_array;
Expand All @@ -103,7 +110,7 @@ sub users_p {
sub _add_message_p {
my ($self, $target, $msg) = @_;

return $self->sqlite->db->insert_p(
return $self->_db->insert_p(
convos_notifications => {
connection_id => $target->connection->id,
conversation_id => $target->id,
Expand All @@ -119,7 +126,7 @@ sub _add_message_p {
sub _add_notification_p {
my ($self, $target, $msg) = @_;

return $self->sqlite->db->insert_p(
return $self->_db->insert_p(
convos_notifications => {
connection_id => $target->connection->id,
conversation_id => $target->id,
Expand All @@ -131,6 +138,8 @@ sub _add_notification_p {
);
}

sub _db { shift->sqlite->db }

sub _obj_to_table {
my ($self, $obj) = @_;
return 'convos_connections' if $obj->isa('Convos::Core::Connection');
Expand Down
42 changes: 42 additions & 0 deletions t/backend-sqlite-basic.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!perl
use lib '.';
use t::Helper;
use Convos::Core::Backend::SQLite;
use Convos::Core::User;

my $backend = Convos::Core::Backend::SQLite->new(home => Mojo::File->new($ENV{CONVOS_HOME}));
my $user = Convos::Core::User->new(email => 'jhthorsen@cpan.org', uid => 42);

my $users;
$backend->users_p->then(sub { $users = shift })->$wait_success('users_p');
is_deeply $users, [], 'no users';

my $saved;
$backend->save_object_p($user)->then(sub { $saved = shift })->$wait_success('save_object_p');
is $saved, $user, 'save_object_p';

my $connections;
$backend->connections_p($user)->then(sub { $connections = shift })->$wait_success('connections_p');
is_deeply $connections, [], 'no connections';

my $loaded;
$backend->load_object_p($user)->then(sub { $loaded = shift; $loaded->{registered} = 'ts', })
->$wait_success('load_object_p');
is_deeply $loaded,
{
email => 'jhthorsen@cpan.org',
highlight_keywords => [],
password => '',
registered => 'ts',
remote_address => '127.0.0.1',
roles => [],
uid => 42,
unread => 0
},
'load_object_p';

my $deleted;
$backend->delete_object_p($user)->then(sub { $deleted = shift })->$wait_success('delete_object_p');
is $deleted, $user, 'delete_object_p';

done_testing;

0 comments on commit be2afa6

Please sign in to comment.