add test of facets using a "n-aria" tree

This commit is contained in:
vnmedeiros 2019-04-24 18:59:44 -03:00
parent 11b9ffcdf7
commit 0dbd8f09fa
2 changed files with 70 additions and 35 deletions

View File

@ -1072,40 +1072,7 @@ class Metadata extends Repository {
}
// $results = [];
//
// function children_has_items($term_id, $hierarchy) {
//
// $has_items = false;
//
// foreach ( $hierarchy as $h ) {
//
// if ( $term_in_hierarchy->parent != $term_id ) {
// continue;
// }
//
// if ( $h->have_items > 0 ) {
// return true;
// } else {
// return children_has_items($h->term_id, $hierarchy);
// }
//
// }
//
// return false;
//
// }
// var_dump($all_hierarchy);
// foreach ($all_hierarchy as $term_in_hierarchy) {
// if ( $term_in_hierarchy->parent != $args['parent_id'] ) {
// continue;
// }
//
// if ( $term_in_hierarchy->have_items > 0 || children_has_items($term_in_hierarchy->term_id, $all_hierarchy) ) {
// $results[] = $term_in_hierarchy;
// }
//
// }
}

View File

@ -969,6 +969,74 @@ class Facets extends TAINACAN_UnitApiTestCase {
}
/**
* @group term_tree
*/
public function test_process_term_tree_naria() {
$MetaRepo = \Tainacan\Repositories\Metadata::get_instance();
$nchildrens = 3;
$h = 6;
$data = $this->generate_narias_tree_test($nchildrens, $h);
$this->set_items_in_tree($data, $nchildrens, $h, [1,3], 1);
//var_dump($data);
$start = microtime(true);
$results = $MetaRepo->_process_terms_tree($data, 0);
$time = microtime(true) - $start;
$this->assertEquals(2, count($results));
$ids = array_map(function($el) {return $el->term_id; }, $results);
$this->assertContains(1, $ids);
$this->assertContains(3, $ids);
}
private function set_items_in_tree($data, $nchildrens, $h, $parents=[], $items_repeat=1) {
if (empty($parents) || $nchildrens < 2 || $h < 1)
return $data;
foreach ($parents as $parent) {
for($i=0; $i < $items_repeat; $i++) {
$rando_h = rand (1, $h);
$rando_c = rand (1, $nchildrens) - 1;
$id = $parent;
for ($count=0; $count < $rando_h; $count++ ) {
$idx = ($id * $nchildrens) + $rando_c;
if($idx > count($data)-1) {
$idx = ($parent * $nchildrens) + $rando_c;
}
$id = $data[$idx]->term_id;
}
$data[$idx]->have_items = 1;
}
}
}
/*
* generate n-árias trees
*
* $nchildrens || $h=2 | $h=4 | $h=8 | $h=9
* 2 || 3 | 15 | 255 | 511
* 3 || 4 | 40 | 3280 | 9841
* 4 || 5 | 85 | 21845| 87381
* 5 || 6 | 156 | 97656| 488281
*/
private function generate_narias_tree_test($nchildrens, $h) {
if ($nchildrens < 2 || $h < 2)
return [];
$n = (pow($nchildrens, $h) - 1) / ($nchildrens - 1);
$data = [];
for ($i = 0; $i < $n-1; $i++) {
$id = $i+1;
$parent = floor($i/$nchildrens);
$data[] = (object) [
'term_id' => $id,
'name' => "i-$id",
'parent' => $parent,
'have_items' => 0
];
}
return $data;
}
}