File: 39_foreign_keys.t

package info (click to toggle)
libdbd-sqlite3-perl 1.76-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 11,004 kB
  • sloc: ansic: 167,715; perl: 1,788; pascal: 277; makefile: 9
file content (73 lines) | stat: -rw-r--r-- 2,324 bytes parent folder | download | duplicates (3)
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
use strict;
use warnings;
use lib "t/lib";
use SQLiteTest;
use Test::More;

BEGIN { requires_sqlite('3.6.19') }

use if -d ".git", "Test::FailWarnings";

# following tests are from http://www.sqlite.org/foreignkeys.html

my $dbh = connect_ok( RaiseError => 1, PrintError => 0, AutoCommit => 1 );

$dbh->do("PRAGMA foreign_keys = ON");

ok $dbh->do("CREATE TABLE artist (
    artistid    INTEGER PRIMARY KEY,
    artistname  TEXT
)");
ok $dbh->do("CREATE TABLE track (
    trackid     INTEGER PRIMARY KEY,
    trackname   TEXT,
    trackartist INTEGER,
    FOREIGN KEY(trackartist) REFERENCES artist(artistid)
)");

ok insert_artist(1, "Dean Martin");
ok insert_artist(2, "Frank Sinatra");

ok insert_track(11, "That's Amore", 1);
ok insert_track(12, "Christmas Blues", 1);
ok insert_track(13, "My Way", 2);

# This fails because the value inserted into the trackartist
# column (3) does not correspond to row in the artist table.

ok !insert_track(14, "Mr. Bojangles", 3);
ok $@ =~ qr/foreign key constraint failed/i;

# This succeeds because a NULL is inserted into trackartist. A
# corresponding row in the artist table is not required in this case.

ok insert_track(14, "Mr. Bojangles", undef);

# Trying to modify the trackartist field of the record after it has 
# been inserted does not work either, since the new value of 
# trackartist (3) still does not correspond to any row in the 
# artist table.

ok !update_track(3, "Mr. Bojangles");
ok $@ =~ /foreign key constraint failed/i;

# Insert the required row into the artist table. It is then possible
# to update the inserted row to set trackartist to 3 (since a
# corresponding row in the artist table now exists).

ok insert_artist(3, "Sammy Davis Jr.");
ok update_track(3, "Mr. Bojangles");

# Now that "Sammy Davis Jr." (artistid = 3) has been added to the
# database, it is possible to INSERT new tracks using this artist
# without violating the foreign key constraint:

ok insert_track(15, "Boogie Woogie", 3);

sub insert_artist { _do("INSERT INTO artist (artistid, artistname) VALUES (?, ?)", @_ ); }
sub insert_track {  _do("INSERT INTO track (trackid, trackname, trackartist) VALUES (?, ?, ?)", @_); }
sub update_track {  _do("UPDATE track SET trackartist = ? WHERE trackname = ?", @_); }

sub _do { eval { $dbh->do(shift, undef, @_) }; }

done_testing;