Skip to content

Commit

Permalink
Use functions instead of static calls for tests requiring randomness
Browse files Browse the repository at this point in the history
  • Loading branch information
mleczakm committed Jul 30, 2024
1 parent 5100213 commit d4c0085
Show file tree
Hide file tree
Showing 34 changed files with 216 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Flow\ETL\Adapter\Elasticsearch\Tests\Integration\ElasticsearchPHP;

use function Flow\ETL\Adapter\Elasticsearch\{es_hits_to_rows, from_es, to_es_bulk_index};
use function Flow\ETL\DSL\df;
use function Flow\ETL\DSL\{df, generate_random_int};
use Flow\ETL\Adapter\Elasticsearch\ElasticsearchPHP\DocumentDataSource;
use Flow\ETL\Adapter\Elasticsearch\EntryIdFactory\EntryIdFactory;
use Flow\ETL\Adapter\Elasticsearch\Tests\Integration\TestCase;
Expand Down Expand Up @@ -40,7 +40,7 @@ public function test_empty_extraction() : void
new Row\Entry\StringEntry('id', \sha1((string) $i)),
new Row\Entry\IntegerEntry('position', $i),
new Row\Entry\StringEntry('name', 'id_' . $i),
new Row\Entry\BooleanEntry('active', (bool) \Flow\ETL\NativePHPRandomValueGenerator::int(0, 1))
new Row\Entry\BooleanEntry('active', (bool) generate_random_int(0, 1))
),
\range(1, 100)
),
Expand Down Expand Up @@ -80,7 +80,7 @@ public function test_extraction_index_with_from_and_size() : void
new Row\Entry\StringEntry('id', \sha1((string) $i)),
new Row\Entry\IntegerEntry('position', $i),
new Row\Entry\StringEntry('name', 'id_' . $i),
new Row\Entry\BooleanEntry('active', (bool) \Flow\ETL\NativePHPRandomValueGenerator::int(0, 1))
new Row\Entry\BooleanEntry('active', (bool) generate_random_int(0, 1))
),
\range(1, 2000)
),
Expand Down Expand Up @@ -123,7 +123,7 @@ public function test_extraction_index_with_search_after() : void
new Row\Entry\StringEntry('id', \sha1((string) $i)),
new Row\Entry\IntegerEntry('position', $i),
new Row\Entry\StringEntry('name', 'id_' . $i),
new Row\Entry\BooleanEntry('active', (bool) \Flow\ETL\NativePHPRandomValueGenerator::int(0, 1))
new Row\Entry\BooleanEntry('active', (bool) generate_random_int(0, 1))
),
\range(1, 2005)
),
Expand Down Expand Up @@ -159,7 +159,7 @@ public function test_extraction_index_with_search_after_with_point_in_time() : v
new Row\Entry\StringEntry('id', \sha1((string) $i)),
new Row\Entry\IntegerEntry('position', $i),
new Row\Entry\StringEntry('name', 'id_' . $i),
new Row\Entry\BooleanEntry('active', (bool) \Flow\ETL\NativePHPRandomValueGenerator::int(0, 1))
new Row\Entry\BooleanEntry('active', (bool) generate_random_int(0, 1))
),
\range(1, 2005)
),
Expand Down Expand Up @@ -200,7 +200,7 @@ public function test_extraction_whole_index_with_point_in_time() : void
new Row\Entry\StringEntry('id', \sha1((string) $i)),
new Row\Entry\IntegerEntry('position', $i),
new Row\Entry\StringEntry('name', 'id_' . $i),
new Row\Entry\BooleanEntry('active', (bool) \Flow\ETL\NativePHPRandomValueGenerator::int(0, 1))
new Row\Entry\BooleanEntry('active', (bool) generate_random_int(0, 1))
),
\range(1, 2005)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Flow\ETL\Adapter\Elasticsearch\Tests\Integration\ElasticsearchPHP;

use function Flow\ETL\Adapter\Elasticsearch\{to_es_bulk_index, to_es_bulk_update};
use function Flow\ETL\DSL\generate_random_string;
use Flow\ETL\Adapter\Elasticsearch\EntryIdFactory\{EntryIdFactory, HashIdFactory};
use Flow\ETL\Adapter\Elasticsearch\Tests\Integration\TestCase;
use Flow\ETL\{Config, FlowContext, Row, Rows};
Expand Down Expand Up @@ -53,19 +54,19 @@ public function test_integration_with_entry_factory() : void

$loader->load(new Rows(
Row::create(
new Row\Entry\StringEntry('id', \sha1('id' . \Flow\ETL\NativePHPRandomValueGenerator::string(32))),
new Row\Entry\StringEntry('id', \sha1('id' . generate_random_string())),
new Row\Entry\StringEntry('name', 'Łukasz')
),
Row::create(
new Row\Entry\StringEntry('id', \sha1('id' . \Flow\ETL\NativePHPRandomValueGenerator::string(32))),
new Row\Entry\StringEntry('id', \sha1('id' . \Flow\ETL\DSL\generate_random_string())),
new Row\Entry\StringEntry('name', 'Norbert')
),
Row::create(
new Row\Entry\StringEntry('id', \sha1('id' . \Flow\ETL\NativePHPRandomValueGenerator::string(32))),
new Row\Entry\StringEntry('id', \sha1('id' . \Flow\ETL\DSL\generate_random_string())),
new Row\Entry\StringEntry('name', 'Dawid')
),
Row::create(
new Row\Entry\StringEntry('id', \sha1('id' . \Flow\ETL\NativePHPRandomValueGenerator::string(32))),
new Row\Entry\StringEntry('id', \sha1('id' . \Flow\ETL\DSL\generate_random_string())),
new Row\Entry\StringEntry('name', 'Tomek')
),
), new FlowContext(Config::default()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function test_empty_extraction() : void
new Row\Entry\StringEntry('id', \sha1((string) $i)),
new Row\Entry\IntegerEntry('position', $i),
new Row\Entry\StringEntry('name', 'id_' . $i),
new Row\Entry\BooleanEntry('active', (bool) \Flow\ETL\NativePHPRandomValueGenerator::int(0, 1))
new Row\Entry\BooleanEntry('active', (bool) generate_random_int(0, 1))
),
\range(1, 100)
),
Expand All @@ -63,7 +63,7 @@ public function test_extraction_index_with_from_and_size() : void
new Row\Entry\StringEntry('id', \sha1((string) $i)),
new Row\Entry\IntegerEntry('position', $i),
new Row\Entry\StringEntry('name', 'id_' . $i),
new Row\Entry\BooleanEntry('active', (bool) \Flow\ETL\NativePHPRandomValueGenerator::int(0, 1))
new Row\Entry\BooleanEntry('active', (bool) generate_random_int(0, 1))
),
// Default limit for Meilisearch is 1000 documents: https://www.meilisearch.com/docs/reference/api/settings#pagination
\range(1, 999)
Expand Down Expand Up @@ -102,7 +102,7 @@ public function test_extraction_index_with_sort() : void
new Row\Entry\StringEntry('id', \sha1((string) $i)),
new Row\Entry\IntegerEntry('position', $i),
new Row\Entry\StringEntry('name', 'id_' . $i),
new Row\Entry\BooleanEntry('active', (bool) \Flow\ETL\NativePHPRandomValueGenerator::int(0, 1))
new Row\Entry\BooleanEntry('active', (bool) generate_random_int(0, 1))
),
// Default limit for Meilisearch is 1000 documents: https://www.meilisearch.com/docs/reference/api/settings#pagination
\range(1, 999)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ public function test_integration_with_entry_factory() : void
$loader = to_meilisearch_bulk_index($this->meilisearchContext->clientConfig(), self::INDEX_NAME);
$loader->load(new Rows(
Row::create(
new Row\Entry\StringEntry('id', \sha1('id' . \Flow\ETL\NativePHPRandomValueGenerator::string(32))),
new Row\Entry\StringEntry('id', \sha1('id' . \Flow\ETL\DSL\generate_random_string())),
new Row\Entry\StringEntry('name', 'Łukasz')
),
Row::create(
new Row\Entry\StringEntry('id', \sha1('id' . \Flow\ETL\NativePHPRandomValueGenerator::string(32))),
new Row\Entry\StringEntry('id', \sha1('id' . \Flow\ETL\DSL\generate_random_string())),
new Row\Entry\StringEntry('name', 'Norbert')
),
Row::create(
new Row\Entry\StringEntry('id', \sha1('id' . \Flow\ETL\NativePHPRandomValueGenerator::string(32))),
new Row\Entry\StringEntry('id', \sha1('id' . \Flow\ETL\DSL\generate_random_string())),
new Row\Entry\StringEntry('name', 'Dawid')
),
Row::create(
new Row\Entry\StringEntry('id', \sha1('id' . \Flow\ETL\NativePHPRandomValueGenerator::string(32))),
new Row\Entry\StringEntry('id', \sha1('id' . \Flow\ETL\DSL\generate_random_string())),
new Row\Entry\StringEntry('name', 'Tomek')
),
), new FlowContext(Config::default()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class TextTest extends TestCase
{
public function test_loading_text_files() : void
{
$path = __DIR__ . '/var/flow_php_etl_csv_loader' . \Flow\ETL\NativePHPRandomValueGenerator::string(32) . '.csv';
$path = __DIR__ . '/var/flow_php_etl_csv_loader' . \Flow\ETL\DSL\generate_random_string() . '.csv';

(new Flow())
->process(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function test_using_put_blob_with_content_when_data_is_larger_than_block_
$blockFactory->method('create')
->willReturnCallback(
function () use ($blockSize) {
return new Block($id = \Flow\ETL\NativePHPRandomValueGenerator::string(32), $blockSize, new Path(sys_get_temp_dir() . '/' . $id . '_block_01.txt'));
return new Block($id = \Flow\ETL\DSL\generate_random_string(), $blockSize, new Path(sys_get_temp_dir() . '/' . $id . '_block_01.txt'));
}
);

Expand Down Expand Up @@ -74,7 +74,7 @@ public function test_using_put_blob_with_content_when_data_is_smaller_than_block
$blockFactory->method('create')
->willReturnCallback(
function () use ($blockSize) {
return new Block($id = \Flow\ETL\NativePHPRandomValueGenerator::string(32), $blockSize, new Path(sys_get_temp_dir() . '/' . $id . '_block_01.txt'));
return new Block($id = \Flow\ETL\DSL\generate_random_string(), $blockSize, new Path(sys_get_temp_dir() . '/' . $id . '_block_01.txt'));
}
);
$stream = AzureBlobDestinationStream::openBlank(
Expand Down
7 changes: 5 additions & 2 deletions src/core/etl/src/Flow/ETL/Config/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Flow\ETL\PHP\Type\Caster;
use Flow\ETL\Pipeline\Optimizer;
use Flow\ETL\Row\Factory\NativeEntryFactory;
use Flow\ETL\{Cache, Config, NativePHPRandomValueGenerator};
use Flow\ETL\{Cache, Config, NativePHPRandomValueGenerator, RandomValueGenerator};
use Flow\Filesystem\{Filesystem, FilesystemTable};
use Flow\Serializer\{Base64Serializer, NativePHPSerializer, Serializer};

Expand All @@ -33,6 +33,8 @@ final class ConfigBuilder

private bool $putInputIntoRows;

private RandomValueGenerator $randomValueGenerator;

private ?Serializer $serializer;

public function __construct()
Expand All @@ -45,11 +47,12 @@ public function __construct()
$this->caster = null;
$this->cache = new CacheConfigBuilder();
$this->sort = new SortConfigBuilder();
$this->randomValueGenerator = new NativePHPRandomValueGenerator();
}

public function build() : Config
{
$this->id ??= 'flow_php' . NativePHPRandomValueGenerator::string(32);
$this->id ??= 'flow_php' . $this->randomValueGenerator->string(32);
$entryFactory = new NativeEntryFactory();
$this->serializer ??= new Base64Serializer(new NativePHPSerializer());

Expand Down
11 changes: 11 additions & 0 deletions src/core/etl/src/Flow/ETL/DSL/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
Join\Comparison\Identical,
Join\Expression,
Loader,
NativePHPRandomValueGenerator,
Pipeline,
Row,
Rows,
Expand Down Expand Up @@ -1209,3 +1210,13 @@ function is_type(array $types, mixed $value) : bool

return false;
}

function generate_random_string(int $length = 32, NativePHPRandomValueGenerator $generator = new NativePHPRandomValueGenerator()) : string
{
return $generator->string($length);

Check failure on line 1216 in src/core/etl/src/Flow/ETL/DSL/functions.php

View workflow job for this annotation

GitHub Actions / Static Analyze (locked, 8.1, ubuntu-latest)

Parameter #1 $int of method Flow\ETL\NativePHPRandomValueGenerator::string() expects int<1, max>, int given.
}

function generate_random_int(int $start = PHP_INT_MIN, int $end = PHP_INT_MAX, NativePHPRandomValueGenerator $generator = new NativePHPRandomValueGenerator()) : int
{
return $generator->int($start, $end);
}
8 changes: 2 additions & 6 deletions src/core/etl/src/Flow/ETL/NativePHPRandomValueGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@

final class NativePHPRandomValueGenerator implements RandomValueGenerator
{
private function __construct()
{
}

public static function int(int $min, int $max) : int
public function int(int $min, int $max) : int
{
return \random_int($min, $max);
}

/** @param int<1, max> $int */
public static function string(int $int) : string
public function string(int $int) : string
{
$bytes = (int) \ceil($int / 2);
$bytes >= 1 ?: $bytes = 1;
Expand Down
4 changes: 2 additions & 2 deletions src/core/etl/src/Flow/ETL/RandomValueGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

interface RandomValueGenerator
{
public static function int(int $min, int $max) : int;
public function int(int $min, int $max) : int;

/** @param int<1, max> $int */
public static function string(int $int) : string;
public function string(int $int) : string;
}
3 changes: 2 additions & 1 deletion src/core/etl/tests/Flow/ETL/Tests/Double/FakeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
datetime_entry,
enum_entry,
float_entry,
generate_random_int,
int_entry,
json_entry,
list_entry,
Expand Down Expand Up @@ -50,7 +51,7 @@ public function extract(FlowContext $context) : \Generator
yield rows(
row(
int_entry('int', $id),
float_entry('float', \Flow\ETL\NativePHPRandomValueGenerator::int(100, 100000) / 100),
float_entry('float', generate_random_int(100, 100000) / 100),
bool_entry('bool', false),
datetime_entry('datetime', new \DateTimeImmutable('now')),
str_entry('null', null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from_array,
from_path_partitions,
from_rows,
generate_random_int,
int_entry,
lit,
overwrite,
Expand Down Expand Up @@ -163,13 +164,13 @@ public function test_partition_by_partitions_order() : void
function (int $i) : array {
$data = [];

$maxItems = \Flow\ETL\NativePHPRandomValueGenerator::int(2, 10);
$maxItems = generate_random_int(2, 10);

for ($d = 0; $d < $maxItems; $d++) {
$data[] = [
'id' => \Flow\ETL\NativePHPRandomValueGenerator::string(32),
'created_at' => (new \DateTimeImmutable('2020-01-01'))->add(new \DateInterval('P' . $i . 'D'))->setTime(\Flow\ETL\NativePHPRandomValueGenerator::int(0, 23), \Flow\ETL\NativePHPRandomValueGenerator::int(0, 59), \Flow\ETL\NativePHPRandomValueGenerator::int(0, 59)),
'value' => \Flow\ETL\NativePHPRandomValueGenerator::int(1, 1000),
'id' => \Flow\ETL\DSL\generate_random_string(),
'created_at' => (new \DateTimeImmutable('2020-01-01'))->add(new \DateInterval('P' . $i . 'D'))->setTime(generate_random_int(0, 23), generate_random_int(0, 59), generate_random_int(0, 59)),
'value' => generate_random_int(1, 1000),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public static function valid_range_provider() : array

public function test_can_create_random_int_from_given_range() : void
{
self::assertSame(1, (NativePHPRandomValueGenerator::int(1, 1)));
self::assertSame(1, ((new NativePHPRandomValueGenerator())->int(1, 1)));
self::assertThat(
NativePHPRandomValueGenerator::int(1, 2),
(new NativePHPRandomValueGenerator())->int(1, 2),
self::logicalOr(
self::equalTo(1),
self::equalTo(2)
Expand All @@ -48,33 +48,33 @@ public function test_can_create_random_int_from_given_range() : void
/** @dataProvider integers_provider */
public function test_can_create_random_string_with_given_length(int $expectedLength) : void
{
self::assertSame($expectedLength, mb_strlen(NativePHPRandomValueGenerator::string($expectedLength)));
self::assertSame($expectedLength, mb_strlen((new NativePHPRandomValueGenerator())->string($expectedLength)));
}

public function test_empty_string_on_length_below_1() : void
{
self::assertSame(
'',
NativePHPRandomValueGenerator::string(0)
(new NativePHPRandomValueGenerator())->string(0)
);
self::assertSame(
'',
NativePHPRandomValueGenerator::string(-1)
(new NativePHPRandomValueGenerator())->string(-1)
);
}

/** @dataProvider invalid_range_provider */
public function test_fail_on_invalid_range(int $min, int $max) : void
{
self::expectException(\ValueError::class);
NativePHPRandomValueGenerator::int($min, $max);
(new NativePHPRandomValueGenerator())->int($min, $max);
}

/** @dataProvider valid_range_provider */
public function test_return_random_int_on_valid_range(int $min, int $max) : void
{
self::assertThat(
NativePHPRandomValueGenerator::int($min, $max),
(new NativePHPRandomValueGenerator())->int($min, $max),
self::logicalOr(
self::greaterThanOrEqual($min),
self::lessThanOrEqual($max)
Expand Down
24 changes: 21 additions & 3 deletions src/core/etl/tests/Flow/ETL/Tests/Unit/RowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,25 @@

namespace Flow\ETL\Tests\Unit;

use function Flow\ETL\DSL\{array_entry, bool_entry, datetime_entry, float_entry, int_entry, list_entry, map_entry, object_entry, row, str_entry, struct_element, struct_entry, struct_type, type_int, type_list, type_map, type_object, type_string};
use function Flow\ETL\DSL\{array_entry,
bool_entry,
datetime_entry,
float_entry,
generate_random_int,
int_entry,
list_entry,
map_entry,
object_entry,
row,
str_entry,
struct_element,
struct_entry,
struct_type,
type_int,
type_list,
type_map,
type_object,
type_string};
use Flow\ETL\PHP\Type\Logical\List\ListElement;
use Flow\ETL\PHP\Type\Logical\Map\{MapKey, MapValue};
use Flow\ETL\PHP\Type\Logical\Structure\StructureElement;
Expand Down Expand Up @@ -78,8 +96,8 @@ public static function is_equal_data_provider() : \Generator
public function test_getting_schema_from_row() : void
{
$row = row(
int_entry('id', \Flow\ETL\NativePHPRandomValueGenerator::int(100, 100000)),
float_entry('price', \Flow\ETL\NativePHPRandomValueGenerator::int(100, 100000) / 100),
int_entry('id', generate_random_int(100, 100000)),
float_entry('price', generate_random_int(100, 100000) / 100),
bool_entry('deleted', false),
datetime_entry('created-at', new \DateTimeImmutable('now')),
str_entry('phase', null),
Expand Down
Loading

0 comments on commit d4c0085

Please sign in to comment.