Skip to content

Commit 47c08f9

Browse files
Michael Ferompenick
authored andcommitted
test: Adding new types to collection tests and simplifying scalar tests
1 parent e44a8ac commit 47c08f9

File tree

3 files changed

+231
-247
lines changed

3 files changed

+231
-247
lines changed

tests/integration/Cassandra/CollectionsIntegrationTest.php

Lines changed: 1 addition & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* A base class for collections integration tests
2323
*/
24-
abstract class CollectionsIntegrationTest extends BasicIntegrationTest {
24+
abstract class CollectionsIntegrationTest extends DatatypeIntegrationTests {
2525
/**
2626
* Create user types after initializing cluster and session
2727
*/
@@ -41,30 +41,6 @@ protected function setUp() {
4141
}
4242
}
4343

44-
/**
45-
* Scalar Cassandra types to be used by data providers
46-
*/
47-
public function scalarCassandraTypes() {
48-
return array(
49-
array(Type::ascii(), array("a", "b", "c")),
50-
array(Type::bigint(), array(new Bigint("1"), new Bigint("2"), new Bigint("3"))),
51-
array(Type::blob(), array(new Blob("x"), new Blob("y"), new Blob("z"))),
52-
array(Type::boolean(), array(true, false, true, false)),
53-
array(Type::decimal(), array(new Decimal(1.1), new Decimal(2.2), new Decimal(3.3))),
54-
array(Type::double(), array(1.1, 2.2, 3.3, 4.4)),
55-
array(Type::float(), array(new Float(1.0), new Float(2.2), new Float(2.2))),
56-
array(Type::inet(), array(new Inet("127.0.0.1"), new Inet("127.0.0.2"), new Inet("127.0.0.3"))),
57-
array(Type::text(), array("a", "b", "c", "x", "y", "z")),
58-
array(Type::timestamp(), array(new Timestamp(123), new Timestamp(456), new Timestamp(789))),
59-
array(Type::timeuuid(), array(new Timeuuid(0), new Timeuuid(1), new Timeuuid(2))),
60-
array(Type::uuid(), array(new Uuid("03398c99-c635-4fad-b30a-3b2c49f785c2"),
61-
new Uuid("03398c99-c635-4fad-b30a-3b2c49f785c3"),
62-
new Uuid("03398c99-c635-4fad-b30a-3b2c49f785c4"))),
63-
array(Type::varchar(), array("a", "b", "c", "x", "y", "z")),
64-
array(Type::varint(), array(new Varint(1), new Varint(2), new Varint(3))),
65-
);
66-
}
67-
6844
/**
6945
* Composite Cassandra types (list, map, set, tuple, and UDT) to be used by
7046
* data providers
@@ -131,148 +107,4 @@ public function nestedCassandraTypes() {
131107

132108
return $nestedCassandraTypes;
133109
}
134-
135-
/**
136-
* Create a table using $type for the value's type and insert $value using
137-
* positional parameters.
138-
*
139-
* @param $type Cassandra\Type
140-
* @param $value mixed
141-
*/
142-
public function createTableInsertAndVerifyValueByIndex($type, $value) {
143-
$key = "key";
144-
$options = new ExecutionOptions(array('arguments' => array($key, $value)));
145-
$this->createTableInsertAndVerifyValue($type, $options, $key, $value);
146-
}
147-
148-
/**
149-
* Create a table using $type for the value's type and insert $value using
150-
* named parameters.
151-
*
152-
* @param $type Cassandra\Type
153-
* @param $value mixed
154-
*/
155-
public function createTableInsertAndVerifyValueByName($type, $value) {
156-
$key = "key";
157-
$options = new ExecutionOptions(array('arguments' => array("key" => $key, "value" => $value)));
158-
$this->createTableInsertAndVerifyValue($type, $options, $key, $value);
159-
}
160-
161-
/**
162-
* Create a user type in the current keyspace
163-
*
164-
* @param $userType Cassandra\Type\UserType
165-
*/
166-
public function createUserType($userType) {
167-
$query = "CREATE TYPE IF NOT EXISTS %s (%s)";
168-
$fieldsString = implode(", ", array_map(function ($name, $type) {
169-
return "$name " . self::typeString($type);
170-
}, array_keys($userType->types()), $userType->types()));
171-
$query = sprintf($query, $this->userTypeString($userType), $fieldsString);
172-
$this->session->execute(new SimpleStatement($query));
173-
}
174-
175-
/**
176-
* Create a table named for the CQL $type parameter
177-
*
178-
* @param $type Cassandra\Type
179-
* @return string Table name generated from $type
180-
*/
181-
public function createTable($type) {
182-
$query = "CREATE TABLE IF NOT EXISTS %s (key text PRIMARY KEY, value %s)";
183-
184-
$cqlType = $this->typeString($type);
185-
$tableName = "table_" . str_replace(array("-"), "", (string)(new Uuid()));
186-
187-
$query = sprintf($query, $tableName, $cqlType);
188-
189-
$this->session->execute(new SimpleStatement($query));
190-
191-
return $tableName;
192-
}
193-
194-
/**
195-
* Create a new table with specified type and insert and verify value
196-
*
197-
* @param $type Cassandra\Type
198-
* @param $options Cassandra\ExecutionOptions
199-
* @param $key string
200-
* @param $value mixed
201-
*/
202-
protected function createTableInsertAndVerifyValue($type, $options, $key, $value) {
203-
$tableName = $this->createTable($type);
204-
205-
$this->insertValue($tableName, $options);
206-
207-
$this->verifyValue($tableName, $type, $key, $value);
208-
}
209-
210-
/**
211-
* Insert a value into table
212-
*
213-
* @param $tableName string
214-
* @param $options Cassandra\ExecutionOptions
215-
*/
216-
protected function insertValue($tableName, $options) {
217-
$insertQuery = "INSERT INTO $tableName (key, value) VALUES (?, ?)";
218-
219-
$this->session->execute(new SimpleStatement($insertQuery), $options);
220-
}
221-
222-
/**
223-
* Verify value
224-
*
225-
* @param $tableName string
226-
* @param $type Cassandra\Type
227-
* @param $key string
228-
* @param $value mixed
229-
*/
230-
protected function verifyValue($tableName, $type, $key, $value) {
231-
$selectQuery = "SELECT * FROM $tableName WHERE key = ?";
232-
233-
$options = new ExecutionOptions(array('arguments' => array($key)));
234-
235-
$result = $this->session->execute(new SimpleStatement($selectQuery), $options);
236-
237-
$this->assertEquals(count($result), 1);
238-
239-
$row = $result->first();
240-
241-
$this->assertEquals($row['value'], $value);
242-
$this->assertTrue($row['value'] == $value);
243-
if ($row['value']) {
244-
$this->assertEquals(count($row['value']), count($value));
245-
$this->assertEquals($row['value']->type(), $type);
246-
}
247-
}
248-
249-
/**
250-
* Generate a type string suitable for creating a new table or user type
251-
* using CQL
252-
*
253-
* @param $type Cassandra\Type
254-
* @return string String representation of type
255-
*/
256-
public static function typeString($type) {
257-
if ($type instanceof Type\Tuple || $type instanceof Type\Collection ||
258-
$type instanceof Type\Map || $type instanceof Type\Set ||
259-
$type instanceof Type\UserType) {
260-
return sprintf("frozen<%s>", $type);
261-
} else {
262-
return (string)$type;
263-
}
264-
}
265-
266-
/**
267-
* Generate a user type name string suitable for creating a new table or
268-
* user type using CQL
269-
*
270-
* @param $userType Cassandra\Type
271-
* @return string String representation of the UserType
272-
*/
273-
public static function userTypeString($userType) {
274-
return sprintf("%s", implode("_", array_map(function ($name, $type) {
275-
return $name . str_replace(array("frozen", "<", " ", ",", ">"), "", $type);
276-
}, array_keys($userType->types()), $userType->types())));
277-
}
278110
}

tests/integration/Cassandra/DatatypeIntegrationTest.php

Lines changed: 31 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,37 @@
2121
/**
2222
* Datatype integration tests.
2323
*/
24-
class DatatypeIntegrationTest extends BasicIntegrationTest {
24+
class DatatypeIntegrationTest extends DatatypeIntegrationTests {
25+
/**
26+
* Scalar data types
27+
*
28+
* This test ensures that data types work with all Cassandra's scalar
29+
* types.
30+
*
31+
* @test
32+
* @dataProvider dataTypes
33+
*/
34+
public function testDataTypes($type, $values) {
35+
foreach ($values as $value) {
36+
$this->createTableInsertAndVerifyValueByIndex($type, $value);
37+
$this->createTableInsertAndVerifyValueByName($type, $value);
38+
}
39+
}
40+
41+
/**
42+
* Data provider scalar data types
43+
*/
44+
public function dataTypes() {
45+
return array_map(function ($cassandraType) {
46+
$type = $cassandraType[0];
47+
$values = array();
48+
foreach ($cassandraType[1] as $value) {
49+
$values[] = $value;
50+
}
51+
return array($type, $values);
52+
}, $this->scalarCassandraTypes());
53+
}
54+
2555
/**
2656
* Ensure Decimal/Varint encoding on byte boundaries
2757
*
@@ -71,81 +101,4 @@ public function testByteBoundaryDecimalVarint() {
71101
$this->assertEquals($values[2], $row["value_varint"]);
72102
}
73103
}
74-
75-
/**
76-
* @test
77-
* @ticket PHP-63
78-
*/
79-
public function testSupportsSmallint() {
80-
// Create the table
81-
$query = "CREATE TABLE {$this->tableNamePrefix} (value_smallint smallint PRIMARY KEY)";
82-
$this->session->execute(new SimpleStatement($query));
83-
$statement = $this->session->prepare("INSERT INTO {$this->tableNamePrefix} (value_smallint) VALUES (?)");
84-
85-
$value = rand(Smallint::min()->toInt(), Smallint::max()->toInt());
86-
$this->session->execute($statement, new ExecutionOptions(array("arguments" => array(new Smallint($value)))));
87-
$rows = $this->session->execute(new SimpleStatement("SELECT * FROM {$this->tableNamePrefix}"));
88-
$this->assertCount(1, $rows);
89-
$row = $rows->first();
90-
$this->assertNotNull($row);
91-
$this->assertEquals(new Smallint($value), $row["value_smallint"]);
92-
}
93-
94-
/**
95-
* @test
96-
* @ticket PHP-63
97-
*/
98-
public function testSupportsTinyint() {
99-
// Create the table
100-
$query = "CREATE TABLE {$this->tableNamePrefix} (value_tinyint tinyint PRIMARY KEY)";
101-
$this->session->execute(new SimpleStatement($query));
102-
$statement = $this->session->prepare("INSERT INTO {$this->tableNamePrefix} (value_tinyint) VALUES (?)");
103-
104-
$value = rand(Tinyint::min()->toInt(), Tinyint::max()->toInt());
105-
fprintf(STDERR, "value is %d\n", $value);
106-
$this->session->execute($statement, new ExecutionOptions(array("arguments" => array(new Tinyint($value)))));
107-
$rows = $this->session->execute(new SimpleStatement("SELECT * FROM {$this->tableNamePrefix}"));
108-
$this->assertCount(1, $rows);
109-
$row = $rows->first();
110-
$this->assertNotNull($row);
111-
$this->assertEquals(new Tinyint($value), $row["value_tinyint"]);
112-
}
113-
114-
/**
115-
* @test
116-
* @ticket PHP-64
117-
*/
118-
public function testSupportsDate() {
119-
// Create the table
120-
$query = "CREATE TABLE {$this->tableNamePrefix} (value_date date PRIMARY KEY)";
121-
$this->session->execute(new SimpleStatement($query));
122-
$statement = $this->session->prepare("INSERT INTO {$this->tableNamePrefix} (value_date) VALUES (?)");
123-
124-
$date = new Date();
125-
$this->session->execute($statement, new ExecutionOptions(array("arguments" => array($date))));
126-
$rows = $this->session->execute(new SimpleStatement("SELECT * FROM {$this->tableNamePrefix}"));
127-
$this->assertCount(1, $rows);
128-
$row = $rows->first();
129-
$this->assertNotNull($row);
130-
$this->assertEquals($date, $row["value_date"]);
131-
}
132-
133-
/**
134-
* @test
135-
* @ticket PHP-64
136-
*/
137-
public function testSupportsTime() {
138-
// Create the table
139-
$query = "CREATE TABLE {$this->tableNamePrefix} (value_time time PRIMARY KEY)";
140-
$this->session->execute(new SimpleStatement($query));
141-
$statement = $this->session->prepare("INSERT INTO {$this->tableNamePrefix} (value_time) VALUES (?)");
142-
143-
$time = new Time();
144-
$this->session->execute($statement, new ExecutionOptions(array("arguments" => array($time))));
145-
$rows = $this->session->execute(new SimpleStatement("SELECT * FROM {$this->tableNamePrefix}"));
146-
$this->assertCount(1, $rows);
147-
$row = $rows->first();
148-
$this->assertNotNull($row);
149-
$this->assertEquals($time, $row["value_time"]);
150-
}
151104
}

0 commit comments

Comments
 (0)