summaryrefslogtreecommitdiff
path: root/tests/simple_unit/DbCommon/PgsqlColumnTest.php
blob: 5859c115f1a199fa634ef099d5e049242c666579 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php

Prado::using('System.Data.*');
Prado::using('System.Data.Common.Pgsql.TPgsqlMetaData');
class PgsqlColumnTest extends UnitTestCase
{
	function create_meta_data()
	{
		$conn = new TDbConnection('pgsql:host=localhost;dbname=test', 'test','test');
		return new TPgsqlMetaData($conn);
	}

	function test_text_column_def()
	{
		$table = $this->create_meta_data()->getTableInfo('public.address');
		$this->assertEqual(count($table->getColumns()), 14);

		$columns['id'] = array(
			'ColumnName'       => '"id"',
			'ColumnSize'       => null,
			'ColumnIndex'      => 0,
			'DbType'           => 'integer',
			'AllowNull'        => false,
			'DefaultValue'     => TDbTableColumn::UNDEFINED_VALUE,
			'NumericPrecision' => null,
			'NumericScale'     => null,
			'IsPrimaryKey'     => true,
			'IsForeignKey'     => false,
			'SequenceName'     => 'public.address_id_seq',
		);

		$columns['username'] = array(
			'ColumnName'       => '"username"',
			'ColumnSize'       => 128,
			'ColumnIndex'      => 1,
			'DbType'           => 'character varying',
			'AllowNull'        => false,
			'DefaultValue'     => TDbTableColumn::UNDEFINED_VALUE,
			'NumericPrecision' => null,
			'NumericScale'     => null,
			'IsPrimaryKey'     => false,
			'IsForeignKey'     => false,
			'SequenceName'     => null,
		);

		$columns['phone'] = array(
			'ColumnName'       => '"phone"',
			'ColumnSize'       => 40,
			'ColumnIndex'      => 2,
			'DbType'           => 'character',
			'AllowNull'        => false,
			'DefaultValue'     => "'hello'::bpchar",
			'NumericPrecision' => null,
			'NumericScale'     => null,
			'IsPrimaryKey'     => false,
			'IsForeignKey'     => false,
			'SequenceName'     => null,
		);

		$columns['field1_boolean'] = array(
			'ColumnName'       => '"field1_boolean"',
			'ColumnSize'       => null,
			'ColumnIndex'      => 3,
			'DbType'           => 'boolean',
			'AllowNull'        => false,
			'DefaultValue'     => TDbTableColumn::UNDEFINED_VALUE,
			'NumericPrecision' => null,
			'NumericScale'     => null,
			'IsPrimaryKey'     => false,
			'IsForeignKey'     => false,
			'SequenceName'     => null,
		);

		$columns['field4_integer'] = array(
			'ColumnName'       => '"field4_integer"',
			'ColumnSize'       => null,
			'ColumnIndex'      => 6,
			'DbType'           => 'integer',
			'AllowNull'        => false,
			'DefaultValue'     => "1",
			'NumericPrecision' => null,
			'NumericScale'     => null,
			'IsPrimaryKey'     => false,
			'IsForeignKey'     => true,
			'SequenceName'     => null,
		);

		$columns['field7_timestamp'] = array(
			'ColumnName'       => '"field7_timestamp"',
			'ColumnSize'       => 2,
			'ColumnIndex'      => 9,
			'DbType'           => 'timestamp without time zone',
			'AllowNull'        => false,
			'DefaultValue'     => TDbTableColumn::UNDEFINED_VALUE,
			'NumericPrecision' => 6,
			'NumericScale'     => null,
			'IsPrimaryKey'     => false,
			'IsForeignKey'     => false,
			'SequenceName'     => null,
		);

		$columns['field9_numeric'] = array(
			'ColumnName'       => '"field9_numeric"',
			'ColumnSize'       => 393220,
			'ColumnIndex'      => 11,
			'DbType'           => 'numeric',
			'AllowNull'        => false,
			'DefaultValue'     => TDbTableColumn::UNDEFINED_VALUE,
			'NumericPrecision' => 6,
			'NumericScale'     => 4,
			'IsPrimaryKey'     => false,
			'IsForeignKey'     => false,
			'SequenceName'     => null,
		);
		$this->assertColumn($columns, $table);

		$this->assertEqual('public', $table->getSchemaName());
		$this->assertEqual('address', $table->getTableName());
		$this->assertEqual(array('id'), $table->getPrimaryKeys());
	}

	function assertColumn($columns, $table)
	{
		foreach($columns as $id=>$asserts)
		{
			$column = $table->Columns[$id];
			foreach($asserts as $property=>$assert)
			{
				$ofAssert= var_export($assert,true);
				$value = $column->{$property};
				$ofValue = var_export($value, true);
				$this->assertEqual($value, $assert,
					"Column [{$id}] {$property} value {$ofValue} did not match {$ofAssert}");
			}
		}
	}
}