classe BDB et sa methode listTables()
[bazdig.git] / test / SqlCodeTest.php
blob7d4dee518295084e3007104c15e123208ddfac7d
1 <?php
3 require_once('simpletest/unit_tester.php');
4 require_once('simpletest/reporter.php');
6 class SqlCodeTest extends UnitTestCase {
8 function setUp()
10 require_once '../lib/code.php';
11 $this->query = 'select * from test';
12 $this->md5 = md5($this->query);
13 $this->sqlCode = new SqlCode($this->query);
14 $this->db =& new PDO("sqlite:bazdig-test.db");
15 SqlCode::set_db($this->db);
16 $this->sqlCode2 = new SqlCode("select * from test");
17 $this->sqlCode2->id = 'test:001';
18 $this->sqlCode2->date = '2008-01-25';
19 $this->markkitDB =& new PDO("sqlite2:markkit-test.db");
20 $this->markkitQuery3 = new SqlCode("select * from marks where pageUrl like '%x'");
23 function tearDown()
25 $tableName = SqlCode::get_table_name();
26 $id = $this->sqlCode->id;
27 $this->db->exec("delete from $tableName where id='$id'");
28 $id = $this->markkitQuery3->id;
29 $this->db->exec("delete from $tableName where id='$id'");
32 function test_search()
34 $result = SqlCode::search("test");
35 $expected = 2;
36 $this->assertEqual($expected, count($result));
37 $expected = $this->sqlCode2;
38 $this->assertTrue($expected == $result[0] || $expected == $result[1]);
39 $result = SqlCode::search("test", "limit 1");
40 $expected = 1;
41 $this->assertEqual($expected, count($result));
42 $result = SqlCode::search("test", "where id='test:005'");
43 $expected = 1;
44 $this->assertEqual($expected, count($result));
47 function test_exec()
49 $markkitQuery1 = new SqlCode("select * from marks where id='testid'");
50 $result = $markkitQuery1->exec($this->markkitDB)->fetchAll();
51 $expected = 'testuser';
52 $this->assertEqual($expected, $result[0]['owner']);
53 $markkitQuery2 = new SqlCode("select * from marks where id='testid4'");
54 $result = $markkitQuery2->exec($this->markkitDB)->fetchAll();
55 $expected = 'http://localhost/test/x';
56 $this->assertEqual($expected, $result[0]['pageUrl']);
57 $result = $this->markkitQuery3->exec($this->markkitDB)->fetchAll();
58 $expected = 'http://localhost/test/x';
59 $this->assertEqual($expected, $result[1]['pageUrl']);
62 function test_toSQLinsert()
64 $result = $this->sqlCode->toSQLinsert();
65 $expected = "/insert/i";
66 $this->assertWantedPattern($expected, $result);
67 $expected = "/'md5:".$this->md5."'/";
68 $this->assertWantedPattern($expected, $result);
71 function test_toSQLselect()
73 $result = $this->sqlCode->toSQLselect();
74 $expected = "/select/i";
75 $this->assertWantedPattern($expected, $result);
76 $expected = "/id='md5:".$this->md5."'/";
77 $this->assertWantedPattern($expected, $result);
80 function test_sql_select()
82 $options = " where code like 'select %'";
83 $result = SqlCode::sql_select($options);
84 $expected = "/$options/i";
85 $this->assertWantedPattern($expected, $result);
86 $expected = "/select.+id.+from/i";
87 $this->assertWantedPattern($expected, $result);
88 $expected = "/select.+code.+from/i";
89 $this->assertWantedPattern($expected, $result);
90 $expected = "/select.+date.+from/i";
91 $this->assertWantedPattern($expected, $result);
94 function test_select()
96 $result = SqlCode::select(" where id='test:001'");
97 $expected = array($this->sqlCode2);
98 $this->assertEqual($expected, $result);
101 function test_save()
103 $this->sqlCode->save();
104 $md5 = $this->sqlCode->id;
105 $result = $this->db->query("select code from sql where id='$md5'");
106 $rows = $result->fetchAll(PDO::FETCH_ASSOC);
107 $count = count($rows);
108 $expected = 1;
109 $this->assertEqual($expected, $count);
110 $expected = "select * from test";
111 $this->assertEqual($expected, $rows[0]['code']);
112 $this->sqlCode->save(); // save a second time
113 $result = $this->db->query("select * from sql where id='$md5'");
114 $rows = $result->fetchAll(PDO::FETCH_ASSOC);
115 $count = count($rows);
116 $expected = 1;
117 $this->assertEqual($expected, $count);
118 $this->markkitQuery3->save();
119 $id = $this->markkitQuery3->id;
120 $result = $this->db->query("select * from sql where id='$id'");
121 $rows = $result->fetchAll(PDO::FETCH_ASSOC);
122 $count = count($rows);
123 $expected = 1;
124 $this->assertEqual($expected, $count);
125 $expected = "/'%x'/";
126 $this->assertWantedPattern($expected, $rows[0]['code']);
127 $expected = $id;
128 $this->assertEqual($expected, $rows[0]['id']);
132 // Running the test.
133 $test =& new SqlCodeTest;
134 $test->run(new HtmlReporter());