Proyectos de Subversion Moodle

Rev

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