1<?php
2
3class TestLogFiles extends UnitTestCase {
4	var $syslogParser;
5	var $logStream;
6
7	function setup() {
8		$this->syslogParser = new SyslogPostgreSQLParser();
9		$this->logStream = new LogStream();
10	}
11
12	function getLinesFromFile($fileName) {
13		$filePath = 'logs/TestLogFiles/'.$fileName;
14		if(is_readable($filePath)) {
15			$lines = file($filePath);
16		} else {
17			$lines = array();
18		}
19		return $lines;
20	}
21
22	function testDoubleDuration() {
23		$textLines = $this->getLinesFromFile('test_double_duration.log');
24
25		$step = 0;
26
27		foreach($textLines AS $textLine) {
28			$line =& $this->syslogParser->parse($textLine);
29			$logObject =& $this->logStream->append($line);
30
31			switch($step) {
32				case 0:
33					$this->checkLine($line, 'PostgreSQLDurationLine', '1199891794', '30059', '12', '1', '', '3.617465');
34					$this->assertFalse($logObject);
35					break;
36				case 1:
37					$this->checkLine($line, 'PostgreSQLQueryStartWithDurationLine', '1199891794', '30059', '13', '1', 'select', '3.617465');
38					$this->assertFalse($logObject);
39					break;
40				case 2:
41					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891794', '30059', '13', '2', " t.tid,t.title,m.name,gn.name,to_char( t.retail_reldate, 'mm-dd-yy' ) as retail_reldate,coalesce(s0c100r0.units,0) as", false);
42					$this->assertFalse($logObject);
43					break;
44				case 3:
45					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891794', '30059', '13', '3', " w0c100r0units,'NA' as w0c100r0dollars,'NA' as w0c100r0arp,coalesce(s0c1r0.units,0) as w0c1r0units,'NA' as w0c1r0dollars,'NA' as", false);
46					$this->assertFalse($logObject);
47					break;
48				case 4:
49					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891794', '30059', '13', '4', " w0c1r0arp,coalesce(s0c2r0.units,0) as w0c2r0units,coalesce(s0c2r0.dollars,0) as w0c2r0dollars,arp(s0c2r0.dollars, s0c2r0.units)", false);
50					$this->assertFalse($logObject);
51					break;
52				case 5:
53					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891794', '30059', '13', '5', " as w0c2r0arp from title t left outer join sublabel sl on t.sublabel_rel = sl.key left outer join label s on sl.lid = s.id left", false);
54					$this->assertFalse($logObject);
55					break;
56				case 6:
57					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891794', '30059', '13', '6', " outer join label d on s.did = d.id left outer join sale_200601 s0c100r0 on t.tid = s0c100r0.tid and s0c100r0.week = 200601 and", false);
58					$this->assertFalse($logObject);
59					break;
60				case 7:
61					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891794', '30059', '13', '7', " s0c100r0.channel = 100 and s0c100r0.region = 0 left outer join sale_200601 s0c1r0 on t.tid = s0c1r0.tid and s0c1r0.week =", false);
62					$this->assertFalse($logObject);
63					break;
64				case 8:
65					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891794', '30059', '13', '8', " 200601 and s0c1r0.channel = 1 and s0c1r0.region = 0 left outer join sale_200601 s0c2r0 on t.tid = s0c2r0.tid and s0c2r0.week =", false);
66					$this->assertFalse($logObject);
67					break;
68				case 9:
69					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891794', '30059', '13', '9', " 200601 and s0c2r0.channel = 2 and s0c2r0.region = 0 left outer join media m on t.media = m.key left outer join genre_n gn on", false);
70					$this->assertFalse($logObject);
71					break;
72				case 10:
73					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891794', '30059', '13', '10', " t.genre_n = gn.key where ((((upper(t.title) like '%MATRIX%' or upper(t.artist) like '%MATRIX%') ))) and t.blob in ('L', 'M',", false);
74					$this->assertFalse($logObject);
75					break;
76				case 11:
77					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891794', '30059', '13', '11', " 'R') and t.source_dvd != 'IN' order by t.title asc limit 100", false);
78					$this->assertFalse($logObject);
79					break;
80				case 12:
81					$this->checkLine($line, 'PostgreSQLDurationLine', '1199891794', '30059', '14', '1', '', '0.003358');
82					$this->checkQueryLogObject($logObject,
83						'QueryLogObject',
84						1199891794,
85						13,
86						"select t.tid,t.title,m.name,gn.name,to_char( t.retail_reldate, 'mm-dd-yy' ) as retail_reldate,coalesce(s0c100r0.units,0) as w0c100r0units,'NA' as w0c100r0dollars,'NA' as w0c100r0arp,coalesce(s0c1r0.units,0) as w0c1r0units,'NA' as w0c1r0dollars,'NA' as w0c1r0arp,coalesce(s0c2r0.units,0) as w0c2r0units,coalesce(s0c2r0.dollars,0) as w0c2r0dollars,arp(s0c2r0.dollars, s0c2r0.units) as w0c2r0arp from title t left outer join sublabel sl on t.sublabel_rel = sl.key left outer join label s on sl.lid = s.id left outer join label d on s.did = d.id left outer join sale_200601 s0c100r0 on t.tid = s0c100r0.tid and s0c100r0.week = 200601 and s0c100r0.channel = 100 and s0c100r0.region = 0 left outer join sale_200601 s0c1r0 on t.tid = s0c1r0.tid and s0c1r0.week = 200601 and s0c1r0.channel = 1 and s0c1r0.region = 0 left outer join sale_200601 s0c2r0 on t.tid = s0c2r0.tid and s0c2r0.week = 200601 and s0c2r0.channel = 2 and s0c2r0.region = 0 left outer join media m on t.media = m.key left outer join genre_n gn on t.genre_n = gn.key where ((((upper(t.title) like '%MATRIX%' or upper(t.artist) like '%MATRIX%') ))) and t.blob in ('L', 'M', 'R') and t.source_dvd != 'IN' order by t.title asc limit 100",
87						'3.617465');
88					break;
89				case 13:
90					$this->checkLine($line, 'PostgreSQLDurationLine', '1199891796', '30059', '15', '1', '', '1.98246');
91					$this->checkQueryLogObject($logObject,
92						'DurationLogObject',
93						1199891794,
94						14,
95						false,
96						'0.003358');
97					break;
98				case 14:
99					$this->checkLine($line, 'PostgreSQLQueryStartWithDurationLine', '1199891796', '30059', '16', '1', 'select sum(coalesce(s0c100r0.units,0)) as', '1.98246');
100					$this->assertFalse($logObject);
101					break;
102				case 15:
103					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891796', '30059', '16', '2', " w0c100r0units,'' as w0c100r0dollars,'' as w0c100r0arp,sum(coalesce(s0c1r0.units,0)) as w0c1r0units,'' as w0c1r0dollars,'' as", false);
104					$this->assertFalse($logObject);
105					break;
106				case 16:
107					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891796', '30059', '16', '3', " w0c1r0arp,sum(coalesce(s0c2r0.units,0)) as w0c2r0units,sum(coalesce(s0c2r0.dollars,0)) as w0c2r0dollars,NULL as w0c2r0arp from", false);
108					$this->assertFalse($logObject);
109					break;
110				case 17:
111					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891796', '30059', '16', '4', " title t left outer join sublabel sl on t.sublabel_rel = sl.key left outer join label s on sl.lid = s.id left outer join label d", false);
112					$this->assertFalse($logObject);
113					break;
114				case 18:
115					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891796', '30059', '16', '5', " on s.did = d.id left outer join sale_200601 s0c100r0 on t.tid = s0c100r0.tid and s0c100r0.week = 200601 and s0c100r0.channel =", false);
116					$this->assertFalse($logObject);
117					break;
118				case 19:
119					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891796', '30059', '16', '6', " 100 and s0c100r0.region = 0 left outer join sale_200601 s0c1r0 on t.tid = s0c1r0.tid and s0c1r0.week = 200601 and", false);
120					$this->assertFalse($logObject);
121					break;
122				case 20:
123					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891796', '30059', '16', '7', " s0c1r0.channel = 1 and s0c1r0.region = 0 left outer join sale_200601 s0c2r0 on t.tid = s0c2r0.tid and s0c2r0.week = 200601 and", false);
124					$this->assertFalse($logObject);
125					break;
126				case 21:
127					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891796', '30059', '16', '8', " s0c2r0.channel = 2 and s0c2r0.region = 0 where ((((upper(t.title) like '%MATRIX%' or upper(t.artist) like '%MATRIX%') ))) and", false);
128					$this->assertFalse($logObject);
129					break;
130				case 22:
131					$this->checkLine($line, 'PostgreSQLContinuationLine', '1199891796', '30059', '16', '9', " t.blob in ('L', 'M', 'R') and t.source_dvd != 'IN'", false);
132					$this->assertFalse($logObject);
133					break;
134				default:
135					stderrArray($line);
136					break;
137			}
138			unset($line);
139			unset($logObject);
140			$step ++;
141		}
142	}
143
144	function testCarriageReturn() {
145		$textLines = $this->getLinesFromFile('test_carriage_return.log');
146
147		foreach($textLines AS $textLine) {
148			$line =& $this->syslogParser->parse($textLine);
149			$logObject =& $this->logStream->append($line);
150		}
151
152		$this->checkQueryLogObject($logObject,
153			'QueryLogObject',
154			1199891794,
155			13,
156			"select t.tid,t.title,m.name,gn.name,to_char( t.retail_reldate, 'mm-dd-yy' ) as retail_reldate,coalesce(s0c100r0.units,0) as w0c100r0units,'NA' as w0c100r0dollars,'NA' as w0c100r0arp,coalesce(s0c1r0.units,0) as w0c1r0units,'NA' as w0c1r0dollars,'NA' as w0c1r0arp,coalesce(s0c2r0.units,0) as w0c2r0units,coalesce(s0c2r0.dollars,0) as w0c2r0dollars,arp(s0c2r0.dollars, s0c2r0.units) as w0c2r0arp from title t left outer join sublabel sl on t.sublabel_rel = sl.key left outer join label s on sl.lid = s.id left outer join label d on s.did = d.id left outer join sale_200601 s0c100r0 on t.tid = s0c100r0.tid and s0c100r0.week = 200601 and s0c100r0.channel = 100 and s0c100r0.region = 0 left outer join sale_200601 s0c1r0 on t.tid = s0c1r0.tid and s0c1r0.week = 200601 and s0c1r0.channel = 1 and s0c1r0.region = 0 left outer join sale_200601 s0c2r0 on t.tid = s0c2r0.tid and s0c2r0.week = 200601 and s0c2r0.channel = 2 and s0c2r0.region = 0 left outer join media m on t.media = m.key left outer join genre_n gn on t.genre_n = gn.key where ((((upper(t.title) like '%MATRIX%' or upper(t.artist) like '%MATRIX%') ))) and t.blob in ('L', 'M', 'R') and t.source_dvd != 'IN' order by t.title asc limit 100",
157			'3.617465');
158	}
159
160	function checkLine(& $line, $type, $timestamp, $connectionId, $commandNumber, $lineNumber, $text, $duration, $ignore = false, $database = false, $user = false) {
161		$this->assertIsA($line, $type);
162		$this->assertEqual($timestamp, $line->getTimestamp());
163		$this->assertEqual($connectionId, $line->getConnectionId());
164		$this->assertEqual($commandNumber, $line->getCommandNumber());
165		$this->assertEqual($lineNumber, $line->getLineNumber());
166		$this->assertEqual($text, $line->getText());
167		$this->assertEqual(0, bccomp($duration, $line->getDuration()));
168		$this->assertEqual($ignore, $line->isIgnored());
169		$this->assertEqual($database, $line->getDatabase());
170		$this->assertEqual($user, $line->getUser());
171	}
172
173	function checkQueryLogObject(& $logObject, $type, $timestamp, $commandNumber, $text, $duration, $ignored = false, $database = false, $user = false, $context = false, $subQueries = array()) {
174		$this->assertIsA($logObject, $type);
175		$this->assertEqual($timestamp, $logObject->getTimestamp());
176		$this->assertEqual($commandNumber, $logObject->getCommandNumber());
177		$this->assertEqual($text, $logObject->getText());
178		$this->assertEqual(0, bccomp($duration, $logObject->getDuration()));
179		$this->assertEqual($ignored, $logObject->isIgnored());
180		$this->assertEqual($database, $logObject->getDatabase());
181		$this->assertEqual($user, $logObject->getUser());
182		$this->assertEqual($context, $logObject->getContext());
183		if($type == 'QueryLogObject') {
184			$this->assertEqual($subQueries, $logObject->getSubQueries());
185		}
186	}
187}
188
189?>