Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
13
//
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/>.
16
 
17
namespace core_analytics;
18
 
19
/**
20
 * Unit tests for the dataset manager.
21
 *
22
 * @package   core_analytics
23
 * @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
24
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
class dataset_manager_test extends \advanced_testcase {
27
 
28
    /** @var array Store dataset top rows. */
29
    protected array $sharedtoprows = [];
30
 
31
    /**
32
     * setUp
33
     *
34
     * @return null
35
     */
36
    public function setUp(): void {
37
        $this->resetAfterTest(true);
38
 
39
        $this->sharedtoprows = array(
40
            array('var1', 'var2'),
41
            array('value1', 'value2'),
42
            array('header1', 'header2')
43
        );
44
    }
45
 
46
    /**
47
     * test_create_dataset
48
     *
49
     * @return null
50
     */
11 efrain 51
    public function test_create_dataset(): void {
1 efrain 52
 
53
        $dataset1 = new \core_analytics\dataset_manager(1, 1, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
54
        $dataset1data = array_merge($this->sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
55
        $f1 = $dataset1->store($dataset1data);
56
 
57
        $f1contents = $f1->get_content();
58
        $this->assertStringContainsString('yeah', $f1contents);
59
        $this->assertStringContainsString('var1', $f1contents);
60
        $this->assertStringContainsString('value1', $f1contents);
61
        $this->assertStringContainsString('header1', $f1contents);
62
    }
63
 
64
    /**
65
     * test_merge_datasets
66
     *
67
     * @return null
68
     */
11 efrain 69
    public function test_merge_datasets(): void {
1 efrain 70
 
71
        $dataset1 = new \core_analytics\dataset_manager(1, 1, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
72
        $dataset1data = array_merge($this->sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
73
        $f1 = $dataset1->store($dataset1data);
74
 
75
        $dataset2 = new \core_analytics\dataset_manager(1, 2, 'whatever', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
76
        $dataset2data = array_merge($this->sharedtoprows, array(array('no', 'no', 'no')));
77
        $f2 = $dataset2->store($dataset2data);
78
 
79
        $files = array($f1, $f2);
80
        $merged = \core_analytics\dataset_manager::merge_datasets($files, 1, 'whatever',
81
            \core_analytics\dataset_manager::LABELLED_FILEAREA);
82
 
83
        $mergedfilecontents = $merged->get_content();
84
        $this->assertStringContainsString('yeah', $mergedfilecontents);
85
        $this->assertStringContainsString('no', $mergedfilecontents);
86
        $this->assertStringContainsString('var1', $mergedfilecontents);
87
        $this->assertStringContainsString('value1', $mergedfilecontents);
88
        $this->assertStringContainsString('header1', $mergedfilecontents);
89
    }
90
 
91
    /**
92
     * test_get_pending_files
93
     *
94
     * @return null
95
     */
11 efrain 96
    public function test_get_pending_files(): void {
1 efrain 97
        global $DB;
98
 
99
        $this->resetAfterTest();
100
 
101
        $fakemodelid = 123;
102
        $timesplittingids = array(
103
            '\core\analytics\time_splitting\quarters',
104
            '\core\analytics\time_splitting\quarters_accum',
105
        );
106
 
107
        // No files.
108
        $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids));
109
        $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));
110
 
111
        // We will reuse this analysable file to create training and prediction datasets (analysable level files are
112
        // merged into training and prediction files).
113
        $analysabledataset = new \core_analytics\dataset_manager($fakemodelid, 1, 'whatever',
114
            \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
115
        $analysabledatasetdata = array_merge($this->sharedtoprows, array(array('yeah', 'yeah', 'yeah')));
116
        $file = $analysabledataset->store($analysabledatasetdata);
117
 
118
        // Evaluation files ignored.
119
        $evaluationdataset = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
120
            '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::LABELLED_FILEAREA, true);
121
 
122
        $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids));
123
        $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));
124
 
125
        // Training and prediction files are not mixed up.
126
        $trainingfile1 = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
127
            '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
128
        $this->waitForSecond();
129
        $trainingfile2 = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
130
            '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::LABELLED_FILEAREA, false);
131
 
132
        $bytimesplitting = \core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids);
133
        $this->assertFalse(isset($bytimesplitting['\core\analytics\time_splitting\quarters_accum']));
134
        $this->assertCount(2, $bytimesplitting['\core\analytics\time_splitting\quarters']);
135
        $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));
136
 
137
        $predictionfile = \core_analytics\dataset_manager::merge_datasets(array($file), $fakemodelid,
138
            '\core\analytics\time_splitting\quarters', \core_analytics\dataset_manager::UNLABELLED_FILEAREA, false);
139
        $bytimesplitting = \core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids);
140
        $this->assertFalse(isset($bytimesplitting['\core\analytics\time_splitting\quarters_accum']));
141
        $this->assertCount(1, $bytimesplitting['\core\analytics\time_splitting\quarters']);
142
 
143
        // Already used for training and prediction are discarded.
144
        $usedfile = (object)['modelid' => $fakemodelid, 'fileid' => $trainingfile1->get_id(), 'action' => 'trained',
145
            'time' => time()];
146
        $DB->insert_record('analytics_used_files', $usedfile);
147
        $bytimesplitting = \core_analytics\dataset_manager::get_pending_files($fakemodelid, true, $timesplittingids);
148
        $this->assertCount(1, $bytimesplitting['\core\analytics\time_splitting\quarters']);
149
 
150
        $usedfile->fileid = $predictionfile->get_id();
151
        $usedfile->action = 'predicted';
152
        $DB->insert_record('analytics_used_files', $usedfile);
153
        $this->assertEmpty(\core_analytics\dataset_manager::get_pending_files($fakemodelid, false, $timesplittingids));
154
    }
155
}