-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDataSetServiceTest.php
341 lines (232 loc) · 11.4 KB
/
DataSetServiceTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
use PHPUnit\Framework\TestCase;
use WoganMay\DomoPHP\Client;
use WoganMay\DomoPHP\DomoPHPException;
final class DataSetServiceTest extends TestCase
{
public function testListDatasets()
{
$client = new Client();
$datasets = $client->dataSet()->list();
$this->assertIsArray($datasets);
// Assumes there is at least 1 dataset in Domo
$this->assertGreaterThan(0, count($datasets));
}
public function testCreateDataset()
{
$client = new Client();
$name = "Test DataSet " . uniqid();
$description = "Created by domo-php's test runner";
$schema = [
'Name' => 'STRING',
'Price' => 'DECIMAL',
'Sales' => 'LONG',
'Tax' => 'DOUBLE',
'Recognize Date' => 'DATE',
'Timestamp' => 'DATETIME'
];
$dataset = $client->dataSet()->create($name, $schema, $description);
$this->assertEquals($name, $dataset->name);
$this->assertEquals(0, $dataset->rows);
}
public function testGetDataset()
{
$client = new Client();
$hash = uniqid();
// Create a new dataset, so we have an ID to fetch
$dataset = $client->dataSet()->create("Test GetDataset {$hash}", [ 'Field' => 'STRING' ]);
// Now pull that from scratch
$retrievedDataset = $client->dataSet()->get($dataset->id);
$this->assertEquals($dataset->name, $retrievedDataset->name);
}
public function testUpdateDataset()
{
$client = new Client();
$hash = uniqid();
// Create a new dataset, so we have a target for updating
$dataset = $client->dataSet()->create("Test UpdateDataset {$hash}", [ 'Field' => 'STRING' ]);
// Update the name and description to something random and verify it's being returned by the API
$newName = "Test UpdateDataset1 {$hash}";
$newDescription = "Updated description for Test UpdateDataset {$hash}";
// Do the update
$client->dataSet()->update($dataset->id, $newName, $newDescription);
// Fetch the dataset from scratch to validate
$retrievedDataset = $client->dataSet()->get($dataset->id);
$this->assertEquals($newName, $retrievedDataset->name);
$this->assertEquals($newDescription, $retrievedDataset->description);
}
public function testDeleteDataset()
{
$client = new Client();
$hash = uniqid();
// Create a new dataset, so we have a target for deleting
$dataset = $client->dataSet()->create("Test DeleteDataset {$hash}", [ 'Field' => 'STRING' ]);
$id = $dataset->id;
// Attempt deletion
$deleteResult = $client->dataSet()->delete($id);
$this->assertTrue($deleteResult);
// Verify we can't fetch it anymore
$this->expectException(DomoPHPException::class);
$client->dataSet()->get($id);
}
public function testImportDataset()
{
$client = new Client();
$hash = uniqid();
// Create a new dataset, so we have a target for importing
$dataset = $client->dataSet()->create("Test DeleteDataset {$hash}", [ 'Field' => 'STRING' ]);
// Load some CSV data - 3 rows
$csv = "Field\nRow 1\nRow 2\nRow 3";
$importResult = $client->dataSet()->import($dataset->id, $csv);
$this->assertTrue($importResult);
}
public function testQueryDataset()
{
$client = new Client();
$hash = uniqid();
// Note: There is a delay between importing data, and the data being available to query. If you
// try running a query/execute during that window, you get a `400 Bad Request` response. So, in order
// to test this properly, this test deliberately waits for _10_ seconds before attempting the query.
// Create a new dataset, so we have a target for importing
$dataset = $client->dataSet()->create("Test QueryDataset {$hash}", [ 'Field' => 'STRING' ]);
$client->dataSet()->import($dataset->id, "Field\nRow 1\nRow 2\nRow 3");
// Give Domo a chance to run whatever internal processes are required
sleep(10);
// We should now be able to query this
$queryResult = $client->dataSet()->query($dataset->id, "SELECT * FROM table");
$this->assertEquals($dataset->id, $queryResult->datasource);
$this->assertCount(1, $queryResult->columns);
$this->assertCount(3, $queryResult->rows);
}
public function testExportDataset()
{
$client = new Client();
$hash = uniqid();
$csv = "Field\nRow 1\nRow 2\nRow 3";
// Create a new dataset, so we have a target for exporting
$dataset = $client->dataSet()->create("Test ExportDataset {$hash}", [ 'Field' => 'STRING' ]);
$client->dataSet()->import($dataset->id, $csv);
// Give Domo a chance to run whatever internal processes are required
sleep(10);
// We should now be able to export this
$exportResult = $client->dataSet()->export($dataset->id);
// Domo adds a trailing newline to exported data, so comparing the
// trim() value on both sides should work. In addition, this also
// validates that the header is present in the downloaded file.
$this->assertEquals(trim($csv), trim($exportResult));
}
public function testListDatasetPDP()
{
$client = new Client();
$hash = uniqid();
// Create a new dataset, so we have a target for listing the PDP
$dataset = $client->dataSet()->create("Test ListDatasetPDP {$hash}", [ 'Field' => 'STRING' ]);
// Should now be able to get the PDP on it (an empty array)
$pdp = $client->dataSet()->listPDP($dataset->id);
// A new dataset should have 1x PDP on it, a default of "All Rows" that has no filters or users
// attached to it.
$this->assertIsArray($pdp);
$this->assertEquals("All Rows", $pdp[0]->name);
$this->assertEmpty($pdp[0]->filters);
$this->assertEmpty($pdp[0]->users);
}
public function testCreateDatasetPDP()
{
$client = new Client();
$hash = uniqid();
// Create a new dataset, so we have a target for creating the PDP
$dataset = $client->dataSet()->create("Test CreateDatasetPDP {$hash}", [ 'Field' => 'STRING' ]);
// The PDP system adds filters to fields, but Domo doesn't seem to understand that fields are present
// if the dataset is not populated, so let's add some data to it.
$client->dataSet()->import($dataset->id, "Field\nRow 1\nRow 2\nRow 3");
// Give Domo a chance to run whatever internal processes are required
sleep(10);
// Now, attach a PDP to the first available user
$firstUser = $client->user()->list(1);
$this->assertEquals(865213262, $firstUser[0]->id);
$users = [ $firstUser[0]->id ];
// Allow the user to only see data where 'Field' = 'Row 1'
$filters = [[
'column' => 'Field',
'operator' => 'EQUALS',
'values' => [
'Row 1'
]
]];
$newPDP = $client->dataSet()->createPDP($dataset->id, "Test PDP #1 - {$hash}", $filters, $users);
// If we have a PDP object with at least 1 user in it, we're good
$this->assertEquals("Test PDP #1 - {$hash}", $newPDP->name);
$this->assertNotEmpty($newPDP->users);
}
public function testGetDatasetPDP()
{
$client = new Client();
$hash = uniqid();
// Create a new dataset, so we have a target for getting the PDP
$dataset = $client->dataSet()->create("Test GetDatasetPDP {$hash}", [ 'Field' => 'STRING' ]);
$client->dataSet()->import($dataset->id, "Field\nRow 1\nRow 2\nRow 3");
// Give Domo a chance to run whatever internal processes are required
sleep(10);
// Now, attach a PDP to the first available user
$firstUser = $client->user()->list(1);
$filters = [['column' => 'Field', 'operator' => 'EQUALS', 'values' => [ 'Row 1' ] ]];
$newPDP = $client->dataSet()->createPDP($dataset->id, "Test PDP #1 - {$hash}", $filters, [ $firstUser[0]->id ]);
// Now try the GET endpoint and ensure we get the same value back
$retrievedPDP = $client->dataSet()->getPDP($dataset->id, $newPDP->id);
$this->assertEquals($newPDP->id, $retrievedPDP->id);
}
public function testUpdateDatasetPDP()
{
$client = new Client();
$hash = uniqid();
// Create a new dataset, so we have a target for getting the PDP
$dataset = $client->dataSet()->create("Test UpdateDatasetPDP {$hash}", [ 'Field' => 'STRING' ]);
$client->dataSet()->import($dataset->id, "Field\nRow 1\nRow 2\nRow 3");
// Give Domo a chance to run whatever internal processes are required
sleep(10);
// Now, attach a PDP to the first available user
$firstUser = $client->user()->list(1);
$secondUser = $client->user()->list(1, 1);
$filters = [['column' => 'Field', 'operator' => 'EQUALS', 'values' => [ 'Row 1' ] ]];
$newPDP = $client->dataSet()->createPDP($dataset->id, "Test PDP #1 - {$hash}", $filters, [ $firstUser[0]->id ]);
// Update the existing PDP to modify everything about it:
$updates = [
// Rename the PDP
'name' => "Test PDP #1-renamed - {$hash}",
// Change the only filter on it to check for Field=Row 2 instead
'filters' => [['column' => 'Field', 'operator' => 'EQUALS', 'values' => [ 'Row 2' ] ]],
// Update the user to someone totally different
'users' => [ $secondUser[0]->id ]
];
// Do the actual update
$client->dataSet()->updatePDP($dataset->id, $newPDP->id, $updates);
// Retrieve the PDP from scratch for comparison
$retrievedPDP = $client->dataSet()->getPDP($dataset->id, $newPDP->id);
// Ensure the PDP has updated
$this->assertEquals("Test PDP #1-renamed - {$hash}", $retrievedPDP->name);
$this->assertEquals("Row 2", $retrievedPDP->filters[0]->values[0]);
$this->assertEquals($secondUser[0]->id, $retrievedPDP->users[0]);
}
public function testDeleteDatasetPDP()
{
$client = new Client();
$hash = uniqid();
// Create a new dataset, so we have a target for getting the PDP
$dataset = $client->dataSet()->create("Test DeleteDatasetPDP {$hash}", [ 'Field' => 'STRING' ]);
$client->dataSet()->import($dataset->id, "Field\nRow 1\nRow 2\nRow 3");
// Give Domo a chance to run whatever internal processes are required
sleep(10);
// Now, attach a PDP to the first available user
$firstUser = $client->user()->list(1);
$filters = [['column' => 'Field', 'operator' => 'EQUALS', 'values' => [ 'Row 1' ] ]];
$newPDP = $client->dataSet()->createPDP($dataset->id, "Test PDP #1 - {$hash}", $filters, [ $firstUser[0]->id ]);
// Verify it's been created by listing out the policies on the DataSet - there should be 2:
$pdpCheck1 = $client->dataSet()->listPDP($dataset->id);
$this->assertEquals(2, count($pdpCheck1));
// Now let's delete ours
$client->dataSet()->deletePDP($dataset->id, $newPDP->id);
// And confirm it's gone, by re-listing and only seeing 1 PDP on the dataset
$pdpCheck2 = $client->dataSet()->listPDP($dataset->id);
$this->assertEquals(1, count($pdpCheck2));
}
}