Merge branch 'install_master' of https://git.in.moodle.com/amosbot/moodle-install
[moodle.git] / analytics / tests / dataset_manager_test.php
blob760e667f13886ff9d639243d3a539e076423cbed
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
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();
27 /**
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 {
36 /**
37 * setUp
39 * @return null
41 public function setUp() {
42 $this->resetAfterTest(true);
44 $this->sharedtoprows = array(
45 array('var1', 'var2'),
46 array('value1', 'value2'),
47 array('header1', 'header2')
51 /**
52 * test_create_dataset
54 * @return null
56 public function test_create_dataset() {
58 $dataset1 = new \core_analytics\dataset_manager(1, 1, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
59 $dataset1->init_process();
60 $dataset1data = array_merge($this->sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
61 $f1 = $dataset1->store($dataset1data);
62 $dataset1->close_process();
64 $f1contents = $f1->get_content();
65 $this->assertContains('yeah', $f1contents);
66 $this->assertContains('var1', $f1contents);
67 $this->assertContains('value1', $f1contents);
68 $this->assertContains('header1', $f1contents);
71 /**
72 * test_merge_datasets
74 * @return null
76 public function test_merge_datasets() {
78 $dataset1 = new \core_analytics\dataset_manager(1, 1, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
79 $dataset1->init_process();
80 $dataset1data = array_merge($this->sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
81 $f1 = $dataset1->store($dataset1data);
82 $dataset1->close_process();
84 $dataset2 = new \core_analytics\dataset_manager(1, 2, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
85 $dataset2->init_process();
86 $dataset2data = array_merge($this->sharedtoprows, array(array('no', 'no', 'no')));
87 $f2 = $dataset2->store($dataset2data);
88 $dataset2->close_process();
90 $files = array($f1, $f2);
91 $merged = \core_analytics\dataset_manager::merge_datasets($files, 1, 'whatever',
92 \core_analytics\dataset_manager::LABELLED_FILEAREA);
94 $mergedfilecontents = $merged->get_content();
95 $this->assertContains('yeah', $mergedfilecontents);
96 $this->assertContains('no', $mergedfilecontents);
97 $this->assertContains('var1', $mergedfilecontents);
98 $this->assertContains('value1', $mergedfilecontents);
99 $this->assertContains('header1', $mergedfilecontents);
103 * test_get_pending_files
105 * @return null
107 public function test_get_pending_files() {
108 global $DB;
110 $this->resetAfterTest();
112 $fakemodelid = 123;
113 $timesplittingids = array(
114 '\core\analytics\time_splitting\quarters',
115 '\core\analytics\time_splitting\quarters_accum',
118 // No files.
119 $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids));
120 $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));
122 // We will reuse this analysable file to create training and prediction datasets (analysable level files are
123 // merged into training and prediction files).
124 $analysabledataset = new \core_analytics\dataset_manager($fakemodelid, 1, 'whatever',
125 \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
126 $analysabledataset->init_process();
127 $analysabledatasetdata = array_merge($this->sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
128 $file = $analysabledataset->store($analysabledatasetdata);
129 $analysabledataset->close_process();
131 // Evaluation files ignored.
132 $evaluationdataset = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
133 '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::LABELLED_FILEAREA, true);
135 $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids));
136 $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));
138 // Training and prediction files are not mixed up.
139 $trainingfile1 = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
140 '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
141 $this->waitForSecond();
142 $trainingfile2 = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
143 '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
145 $bytimesplitting = \core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids);
146 $this->assertFalse(isset($bytimesplitting['\core\analytics\time_splitting\quarters_accum']));
147 $this->assertCount(2, $bytimesplitting['\core\analytics\time_splitting\quarters']);
148 $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));
150 $predictionfile = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
151 '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::UNLABELLED_FILEAREA, false);
152 $bytimesplitting = \core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids);
153 $this->assertFalse(isset($bytimesplitting['\core\analytics\time_splitting\quarters_accum']));
154 $this->assertCount(1, $bytimesplitting['\core\analytics\time_splitting\quarters']);
156 // Already used for training and prediction are discarded.
157 $usedfile = (object)['modelid' => $fakemodelid, 'fileid' => $trainingfile1->get_id(), 'action' => 'trained',
158 'time' => time()];
159 $DB->insert_record('analytics_used_files', $usedfile);
160 $bytimesplitting = \core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids);
161 $this->assertCount(1, $bytimesplitting['\core\analytics\time_splitting\quarters']);
163 $usedfile->fileid = $predictionfile->get_id();
164 $usedfile->action = 'predicted';
165 $DB->insert_record('analytics_used_files', $usedfile);
166 $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));