summaryrefslogtreecommitdiff
path: root/tests/it_dbi.t
blob: 8ef8cab6b6cb71b27ff25137bf6afe97d2de91ad (plain)
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
94
#!/www/server/bin/php -qC
<?php

#  Tests for it_dbi.class

require 'searchlib/search_test.class';

# Initialize DB
$db = array('db' => "lib_search_ch");
$dbi = new it_dbi($db);
$dbi->query('create temporary table it_dbi_test (
	ID int not null auto_increment,
	x int,
	foo varchar(42),
	primary key(ID)
);');

$record = new it_dbi($db + array('table' => "it_dbi_test"));

$record->insert(array('x' => 42, 'foo' => null));
$record->insert(array('foo' => "bar"));
$record->insert(array('x' => 64738, 'foo' => "q'uux"));

is(
	$record->ID,
	3,
	"auto_increment"
);

$record->read(1);
is(
	array($record->_key, $record->x, $record->foo),
	array(1, 42, null),
	"read"
);

$record->select(array('x' => 64738));
is(
	array($record->_key, $record->x, $record->foo),
	array(3, 64738, "q'uux"),
	"select"
);

$record->update(array('x' => 17));
is(
	array($record->_key, $record->x, $record->foo),
	array(3, 17, "q'uux"),
	"update"
);

$record->update(array('-x' => 'RAND() * 10'));
isnt(
	array($record->_key, $record->x, $record->foo),
	array(3, 17, "q'uux"),
	"update"
);

$rand = $record->x;
is (
	$record->_set(array('x' => $rand, 'foo' => "bar")),
	"SET foo='bar'",
	'update: _set optimization'
);

$record->update(array('foo' => "bar"));
$record->select(array('foo' => "bar"));
$record->iterate();
is(
	array($record->_key, $record->x, $record->foo),
	array(2, null, "bar"),
	"iterate record 2"
);
$record->update(array('foo' => "qux"));
$record->iterate();
is(
	array($record->_key, $record->x, $record->foo),
	array(3, $rand, "bar"),
	"iterate record 3"
);
$record->update(array('foo' => "quux"));

$record->read(2);
is(
	array($record->_key, $record->x, $record->foo),
	array(2, null, "qux"),
	"iterate update record 2"
);

$record->read(3);
is(
	array($record->_key, $record->x, $record->foo),
	array(3, $rand, "quux"),
	"iterate update record 3"
);