Commit 4ea365ee authored by Tibo's avatar Tibo

Add tests

parent ee689b2f
Pipeline #2137 passed with stage
in 22 seconds
......@@ -11,6 +11,10 @@ class Dataset
private $data;
/**
*
* @param array $data
*/
public function __construct(array $data)
{
$this->data = $data;
......@@ -19,7 +23,7 @@ class Dataset
/**
*
* @param callable $function
* @return \Tests\Unit\MRCollection
* @return \Cylab\Spark\Dataset
*/
public function map(callable $function)
{
......@@ -41,7 +45,7 @@ class Dataset
*
* @param callable $function
* @param type $initial
* @return \App\MRCollection
* @return \Cylab\Spark\Dataset
*/
public function reduceByKey(callable $function, $initial = null)
{
......@@ -61,7 +65,7 @@ class Dataset
/**
*
* @param bool $ascending
* @return \App\MRCollection
* @return \Cylab\Spark\Dataset
*/
public function sortByKey(bool $ascending = true)
{
......@@ -79,6 +83,10 @@ class Dataset
return new Dataset($copy);
}
/**
*
* @return \Cylab\Spark\Dataset
*/
public function groupByKey()
{
$groups = [];
......@@ -88,21 +96,38 @@ class Dataset
return new Dataset($groups);
}
/**
*
* @return array
*/
public function collect() : array
{
return $this->data;
}
/**
*
* @return int
*/
public function count() : int
{
return count($this->data);
}
/**
*
* @return type
*/
public function first()
{
return $this->data[0];
}
/**
* Return the first n elements of the dataset.
* @param type $n
* @return array
*/
public function take($n = 10) : array
{
return array_slice($this->data, 0, $n);
......
......@@ -21,4 +21,29 @@ class DatasetTest extends TestCase
return $agg + $v;
}));
}
public function testCount()
{
$data = new Dataset([1, 2, 3, 4]);
$this->assertEquals(4, $data->count());
}
public function testTake()
{
$data = new Dataset([1, 2, 3, 4]);
$this->assertEquals(2, count($data->take(2)));
$this->assertEquals(1, $data->take(2)[0]);
}
public function testFirst()
{
$data = new Dataset([1, 2, 3, 4]);
$this->assertEquals(1, $data->first());
}
public function testSortByKey()
{
$data = new Dataset([new Tuple(1, 1), new Tuple(0, 0), new Tuple(2, 2)]);
$this->assertEquals(0, $data->sortByKey()->first()->value);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment