la persistance fonctionne pour SqlCode : select() et save()
[bazdig.git] / test / SqlCodeTest.php
blob35da7832652250d0f8af229edae3a371f2288304
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';
21 function tearDown()
23 $tableName = SqlCode::get_table_name();
24 $id = $this->sqlCode->id;
25 $this->db->exec("delete from $tableName where id='$id'");
28 function testtoSQLinsert()
30 $result = $this->sqlCode->toSQLinsert();
31 $expected = "/insert/i";
32 $this->assertWantedPattern($expected, $result);
33 $expected = "/'md5:".$this->md5."'/";
34 $this->assertWantedPattern($expected, $result);
37 function testtoSQLselect()
39 $result = $this->sqlCode->toSQLselect();
40 $expected = "/select/i";
41 $this->assertWantedPattern($expected, $result);
42 $expected = "/id='md5:".$this->md5."'/";
43 $this->assertWantedPattern($expected, $result);
46 function testsql_select()
48 $options = " where code like 'select %'";
49 $result = SqlCode::sql_select($options);
50 $expected = "/$options/i";
51 $this->assertWantedPattern($expected, $result);
52 $expected = "/select.+id.+from/i";
53 $this->assertWantedPattern($expected, $result);
54 $expected = "/select.+code.+from/i";
55 $this->assertWantedPattern($expected, $result);
56 $expected = "/select.+date.+from/i";
57 $this->assertWantedPattern($expected, $result);
60 function testselect()
62 $result = SqlCode::select(" where id='test:001'");
63 $expected = array($this->sqlCode2);
64 $this->assertEqual($expected, $result);
67 function testsave()
69 $this->sqlCode->save();
70 $md5 = $this->sqlCode->id;
71 $result = $this->db->query("select code from sql where id='$md5'");
72 $result = $result->fetch();
73 $expected = "select * from test";
74 $this->assertEqual($expected, $result['code']);
75 $this->sqlCode->save();
76 $result = $this->db->query("select * from sql where id='$md5'");
77 $result = $result->fetchAll();
78 $expected = 1;
79 $this->assertEqual($expected, count($result));
83 // Running the test.
84 $test =& new SqlCodeTest;
85 $test->run(new HtmlReporter());