2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Unit tests for the dataset manager.
20 * @package core_analytics
21 * @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 * Unit tests for the dataset manager.
30 * @package core_analytics
31 * @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class dataset_manager_testcase extends advanced_testcase {
41 public function test_create_dataset() {
42 $this->resetAfterTest(true);
44 $sharedtoprows = array(
45 array('var1', 'var2'),
46 array('value1', 'value2'),
47 array('header1', 'header2')
50 $dataset1 = new \core_analytics\dataset_manager(1, 1, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
51 $dataset1->init_process();
52 $dataset1data = array_merge($sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
53 $f1 = $dataset1->store($dataset1data);
54 $dataset1->close_process();
56 $f1contents = $f1->get_content();
57 $this->assertContains('yeah', $f1contents);
58 $this->assertContains('var1', $f1contents);
59 $this->assertContains('value1', $f1contents);
60 $this->assertContains('header1', $f1contents);
68 public function test_merge_datasets() {
69 $this->resetAfterTest(true);
71 $sharedtoprows = array(
72 array('var1', 'var2'),
73 array('value1', 'value2'),
74 array('header1', 'header2')
77 $dataset1 = new \core_analytics\dataset_manager(1, 1, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
78 $dataset1->init_process();
79 $dataset1data = array_merge($sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
80 $f1 = $dataset1->store($dataset1data);
81 $dataset1->close_process();
83 $dataset2 = new \core_analytics\dataset_manager(1, 2, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
84 $dataset2->init_process();
85 $dataset2data = array_merge($sharedtoprows, array(array('no', 'no', 'no')));
86 $f2 = $dataset2->store($dataset2data);
87 $dataset2->close_process();
89 $files = array($f1, $f2);
90 $merged = \core_analytics\dataset_manager::merge_datasets($files, 1, 'whatever',
91 \core_analytics\dataset_manager::LABELLED_FILEAREA);
93 $mergedfilecontents = $merged->get_content();
94 $this->assertContains('yeah', $mergedfilecontents);
95 $this->assertContains('no', $mergedfilecontents);
96 $this->assertContains('var1', $mergedfilecontents);
97 $this->assertContains('value1', $mergedfilecontents);
98 $this->assertContains('header1', $mergedfilecontents);