File: crud.t

package info (click to toggle)
libmojo-sqlite-perl 3.009-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 324 kB
  • sloc: perl: 610; sql: 11; makefile: 6
file content (93 lines) | stat: -rw-r--r-- 3,224 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use Mojo::Base -strict;

BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }

use Test::More;
use Mojo::SQLite;
use Mojo::IOLoop;

my $sql = Mojo::SQLite->new;

{
  my $db = $sql->db;
  $db->query(
    'create table if not exists crud_test (
       id   integer primary key autoincrement,
       name text
     )'
  );

  subtest 'Create' => sub {
    $db->insert('crud_test', {name => 'foo'});
    is_deeply $db->select('crud_test')->hashes->to_array,
      [{id => 1, name => 'foo'}], 'right structure';
    is $db->insert('crud_test', {name => 'bar'})->last_insert_id,
      2, 'right value';
    is_deeply $db->select('crud_test')->hashes->to_array,
      [{id => 1, name => 'foo'}, {id => 2, name => 'bar'}], 'right structure';
  };

  subtest 'Read' => sub {
    is_deeply $db->select('crud_test')->hashes->to_array,
      [{id => 1, name => 'foo'}, {id => 2, name => 'bar'}], 'right structure';
    is_deeply $db->select('crud_test', ['name'])->hashes->to_array,
      [{name => 'foo'}, {name => 'bar'}], 'right structure';
    is_deeply $db->select('crud_test', ['name'], {name => 'foo'})->hashes->to_array,
      [{name => 'foo'}], 'right structure';
    is_deeply $db->select('crud_test', ['name'], undef, {-desc => 'id'})
      ->hashes->to_array, [{name => 'bar'}, {name => 'foo'}], 'right structure';
  };

  subtest 'Non-blocking read' => sub {
    my $result;
    $db->select_p('crud_test')->then(sub {
      $result = shift->hashes->to_array;
    })->wait;
    is_deeply $result, [{id => 1, name => 'foo'}, {id => 2, name => 'bar'}],
      'right structure';
    $result = undef;
    $db->select_p('crud_test', undef, undef, {-desc => 'id'})->then(sub {
      $result = shift->hashes->to_array;
    })->wait;
    is_deeply $result, [{id => 2, name => 'bar'}, {id => 1, name => 'foo'}],
      'right structure';
  };

  subtest 'Update' => sub {
    $db->update('crud_test', {name => 'baz'}, {name => 'foo'});
    is_deeply $db->select('crud_test', undef, undef, {-asc => 'id'})
      ->hashes->to_array, [{id => 1, name => 'baz'}, {id => 2, name => 'bar'}],
      'right structure';
  };

  subtest 'Delete' => sub {
    $db->delete('crud_test', {name => 'baz'});
    is_deeply $db->select('crud_test', undef, undef, {-asc => 'id'})
      ->hashes->to_array, [{id => 2, name => 'bar'}], 'right structure';
    $db->delete('crud_test');
    is_deeply $db->select('crud_test')->hashes->to_array, [], 'right structure';
  };

  subtest 'Quoting' => sub {
    $db->query(
      'create table if not exists crud_test2 (
         id   integer primary key autoincrement,
         "t e s t" text
       )'
    );
    $db->insert('crud_test2',      {'t e s t' => 'foo'});
    $db->insert('main.crud_test2', {'t e s t' => 'bar'});
    is_deeply $db->select('main.crud_test2')->hashes->to_array,
      [{id => 1, 't e s t' => 'foo'}, {id => 2, 't e s t' => 'bar'}],
      'right structure';
  };

  subtest 'Unresolved identifier' => sub {
    is_deeply $db->select('main.crud_test2', undef, {'t e s t' => 'foo'})
      ->hashes->to_array, [{id => 1, 't e s t' => 'foo'}], 'right structure';
    ok !eval { $db->select('main.crud_test2', undef, {'test' => 'foo'}); 1 },
      'unknown column';
  };
}

done_testing();