MCPcopy Create free account
hub / github.com/Rello/analytics / DataloadServiceTest

Class DataloadServiceTest

tests/Service/DataloadServiceTest.php:20–605  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18use Psr\Log\NullLogger;
19
20class DataloadServiceTest extends TestCase {
21 /** @dataProvider invalidDeletionOptions */
22 public function testInvalidStoredDeletionJobCannotCountOrDeleteRows(string $options): void {
23 $loads = $this->createMock(DataloadMapper::class);
24 $metadata = ['datasource' => '0', 'dataset' => 11, 'option' => $options];
25 $loads->method('readOwnById')->willReturn($metadata);
26 $loads->method('getDataloadById')->willReturn($metadata);
27 $datasets = $this->createMock(DatasetService::class);
28 $datasets->method('read')->willReturn(['storageMode' => 'legacy']);
29 $storage = $this->createMock(StorageService::class);
30 $storage->expects($this->never())->method('deleteWithFilterSimulate');
31 $storage->expects($this->never())->method('deleteWithFilter');
32 $service = $this->createService($this->createMock(DatasourceController::class), $loads, $datasets, storageService: $storage);
33
34 $this->assertNotEmpty($service->getDataFromDatasource(42)['error']);
35 foreach ([true, false] as $enforceOwnership) {
36 $result = $service->execute(42, null, $enforceOwnership);
37 $this->assertSame(1, $result['error']);
38 $this->assertSame(0, $result['delete']);
39 }
40 }
41
42 public static function invalidDeletionOptions(): array {
43 return [
44 'incomplete saved job' => ['{}'],
45 'invalid JSON' => ['{'],
46 'unknown column' => ['{"filterDimension":"obsolete","filterOption":"EQ","filterValue":"0"}'],
47 'unknown operator' => ['{"filterDimension":"dimension2","filterOption":"obsolete","filterValue":"%currentday%"}'],
48 'missing value' => ['{"filterDimension":"dimension1","filterOption":"LIKE"}'],
49 'empty contains' => ['{"filterDimension":"dimension1","filterOption":"LIKE","filterValue":" "}'],
50 'empty exclusion list' => ['{"filterDimension":"dimension1","filterOption":"NOTIN","filterValue":";,"}'],
51 ];
52 }
53
54 public function testDeletionExecutionUsesTheSameLiteralFilterAsSimulation(): void {
55 $value = 'Sales "North"';
56 $options = ['filterDimension' => 'c_2', 'filterOption' => 'EQ', 'filterValue' => $value];
57 $loads = $this->createMock(DataloadMapper::class);
58 $loads->method('readOwnById')->willReturn(['datasource' => 0, 'dataset' => 11, 'option' => json_encode($options)]);
59 $datasets = $this->createMock(DatasetService::class);
60 $datasets->method('read')->willReturn(['storageMode' => 'flexible_shared', 'columns' => [['role' => 'dimension', 'ref' => 'c_2']]]);
61 $variables = $this->createMock(VariableService::class);
62 $variables->method('replaceFilterVariables')->willReturnArgument(0);
63 $storage = $this->createMock(StorageService::class);
64 $expected = ['filter' => ['c_2' => ['option' => 'EQ', 'value' => $value]]];
65 $storage->expects($this->once())->method('deleteWithFilterSimulate')->with(11, $expected)->willReturn(['count' => 1]);
66 $storage->expects($this->once())->method('deleteWithFilter')->with(11, $expected)->willReturn(1);
67 $service = $this->createService($this->createMock(DatasourceController::class), $loads, $datasets, storageService: $storage, variableService: $variables);
68
69 $this->assertSame(1, $service->execute(42)['delete']);
70 }
71
72 public function testDataloadUpdateValidatesOptionsForTheStoredDatasourceBeforeWriting(): void {
73 $options = '{"mode":"unavailable"}';
74 $dataloadMapper = $this->createMock(DataloadMapper::class);
75 $dataloadMapper->method('readOwnById')->with(42)->willReturn(['datasource' => DatasourceController::DATASET_TYPE_LOCAL_CSV]);
76 $dataloadMapper->expects($this->never())->method('update');
77 $datasourceController = $this->createMock(DatasourceController::class);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected