Skip to content

Commit e1585f9

Browse files
committed
PgSqlTracker tests
1 parent ccb94b0 commit e1585f9

File tree

3 files changed

+124
-15
lines changed

3 files changed

+124
-15
lines changed

src/Instrumentation/PostgreSql/src/PgSqlTracker.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ final class PgSqlTracker
2020

2121
private WeakMap $connectionAttributes;
2222

23-
private WeakMap $connectionStatements;
23+
/**
24+
* @var WeakMap<Connection, string>
25+
*/
26+
private WeakMap $connectionStatements;
2427

2528
/**
2629
* @var WeakMap<Connection, SplQueue<WeakReference<?SpanContextInterface>>
2730
*/
2831
private WeakMap $connectionAsyncLink;
2932

30-
/**
33+
/**
3134
* @var WeakMap<Lob, WeakReference<Connection>>
3235
*/
3336
private WeakMap $connectionLargeObjects;
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Tests\Instrumentation\PostgreSql\Integration;
6+
7+
use OpenTelemetry\Contrib\Instrumentation\PostgreSql\PgSqlTracker;
8+
use PHPUnit\Framework\TestCase;
9+
class PgSqlTrackerTest extends TestCase
10+
{
11+
public function test_query_split(): void
12+
{
13+
$sql = "SELECT 1; SELECT 2;";
14+
$queries = PgSqlTracker::splitQueries($sql);
15+
16+
$this->assertSame(['SELECT 1;', 'SELECT 2;'], $queries);
17+
}
18+
19+
public function test_parse_standard_connection_string(): void
20+
{
21+
$result = PgSqlTracker::parsePgConnString("host=localhost port=5432 dbname=mydb user=otel password=secret");
22+
23+
$this->assertSame('localhost', $result['host']);
24+
$this->assertSame('5432', $result['port']);
25+
$this->assertSame('mydb', $result['dbname']);
26+
$this->assertSame('otel', $result['user']);
27+
$this->assertSame('secret', $result['password']);
28+
}
29+
30+
public function test_parse_quoted_values(): void
31+
{
32+
$result = PgSqlTracker::parsePgConnString("host='localhost' dbname=\"my db\" user='user name'");
33+
34+
$this->assertSame('localhost', $result['host']);
35+
$this->assertSame('my db', $result['dbname']);
36+
$this->assertSame('user name', $result['user']);
37+
}
38+
public function test_parse_socket_only(): void
39+
{
40+
$result = PgSqlTracker::parsePgConnString("dbname=mydb user=postgres");
41+
42+
$this->assertArrayNotHasKey('host', $result);
43+
$this->assertSame('mydb', $result['dbname']);
44+
$this->assertSame('postgres', $result['user']);
45+
}
46+
47+
public function test_parse_empty_string(): void
48+
{
49+
$result = PgSqlTracker::parsePgConnString("");
50+
$this->assertSame([], $result);
51+
}
52+
53+
public function test_parse_attributes_from_connstring_with_host(): void
54+
{
55+
$connString = "host=localhost port=5432 dbname=testdb user=otel";
56+
$attrs = PgSqlTracker::parseAttributesFromConnectionString($connString);
57+
58+
$this->assertSame('localhost', $attrs['server.address']);
59+
$this->assertSame('5432', $attrs['server.port']);
60+
$this->assertSame('testdb', $attrs['db.namespace']);
61+
$this->assertSame('postgresql', $attrs['db.system.name']);
62+
}
63+
64+
public function test_parse_attributes_from_connstring_socket(): void
65+
{
66+
$connString = "dbname=testdb user=otel";
67+
$attrs = PgSqlTracker::parseAttributesFromConnectionString($connString);
68+
69+
$this->assertSame(null, $attrs['server.address']);
70+
$this->assertSame('testdb', $attrs['db.namespace']);
71+
$this->assertSame('postgresql', $attrs['db.system.name']);
72+
}
73+
74+
public function test_basic_split(): void
75+
{
76+
$sql = "SELECT * FROM users; INSERT INTO logs (message) VALUES ('test');";
77+
$expected = [
78+
'SELECT * FROM users;',
79+
"INSERT INTO logs (message) VALUES ('test');",
80+
];
81+
82+
$result = PgSqlTracker::splitQueries($sql);
83+
$this->assertEquals($expected, $result);
84+
}
85+
86+
public function test_whitespace_variants(): void
87+
{
88+
$sql = " SELECT 1;\n\nINSERT INTO test VALUES (2); SELECT 3";
89+
$expected = [
90+
'SELECT 1;',
91+
'INSERT INTO test VALUES (2);',
92+
'SELECT 3',
93+
];
94+
95+
$result = PgSqlTracker::splitQueries($sql);
96+
$this->assertEquals($expected, $result);
97+
}
98+
99+
public function test_semicolon_in_quotes(): void
100+
{
101+
$sql = "INSERT INTO x (text) VALUES ('abc;def'); SELECT 1;";
102+
$expected = [
103+
"INSERT INTO x (text) VALUES ('abc;def');",
104+
'SELECT 1;',
105+
];
106+
107+
$result = PgSqlTracker::splitQueries($sql);
108+
$this->assertEquals($expected, $result);
109+
}
110+
public function test_empty_input(): void
111+
{
112+
$sql = "\n\n\t ";
113+
$expected = [];
114+
$result = PgSqlTracker::splitQueries($sql);
115+
$this->assertEquals($expected, $result);
116+
}
117+
118+
}
119+

src/Instrumentation/PostgreSql/tests/Integration/PostgreSqlInstrumentationTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
namespace OpenTelemetry\Tests\Instrumentation\PostgreSql\Integration;
66

77
use ArrayObject;
8-
use mysqli;
9-
use mysqli_result;
10-
use mysqli_sql_exception;
11-
use mysqli_stmt;
128
use OpenTelemetry\API\Instrumentation\Configurator;
139
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator;
1410
use OpenTelemetry\API\Trace\StatusCode;
@@ -36,15 +32,6 @@ class PostgreSqlInstrumentationTest extends TestCase
3632
private string $passwd;
3733
private string $database;
3834

39-
40-
// pg_lo_open
41-
// pg_lo_write
42-
// pg_lo_read
43-
// pg_lo_read_all
44-
// pg_lo_unlink
45-
// pg_lo_import
46-
// pg_lo_export
47-
4835
public function setUp(): void
4936
{
5037
$this->storage = new ArrayObject();

0 commit comments

Comments
 (0)