1
- using System . CommandLine . Builder ;
1
+ // Licensed to the .NET Foundation under one or more agreements.
2
+ // The .NET Foundation licenses this file to you under the MIT license.
3
+ // See the LICENSE file in the project root for more information.
4
+
5
+ using System . CommandLine . Builder ;
2
6
using System . CommandLine . Invocation ;
3
7
using System . IO ;
4
- using Microsoft . ML . Auto ;
5
8
using Microsoft . ML . CLI . Commands ;
9
+ using Microsoft . ML . CLI . Data ;
6
10
using Microsoft . VisualStudio . TestTools . UnitTesting ;
7
11
8
12
namespace mlnet . Test
@@ -16,8 +20,8 @@ public void TestCommandLineArgs()
16
20
bool parsingSuccessful = false ;
17
21
18
22
// Create handler outside so that commandline and the handler is decoupled and testable.
19
- var handler = CommandHandler . Create < FileInfo , FileInfo , FileInfo , TaskKind , string , uint , uint > (
20
- ( trainDataset , validationDataset , testDataset , mlTask , labelColumnName , maxExplorationTime , labelColumnIndex ) =>
23
+ var handler = CommandHandler . Create < NewCommandOptions > (
24
+ ( opt ) =>
21
25
{
22
26
parsingSuccessful = true ;
23
27
} ) ;
@@ -28,10 +32,12 @@ public void TestCommandLineArgs()
28
32
. UseDefaults ( )
29
33
. Build ( ) ;
30
34
31
- var file = Path . GetTempFileName ( ) ;
32
- string [ ] args = new [ ] { "new" , "--ml-task" , "BinaryClassification" , "--train-dataset" , file , "--label-column-name" , "Label" } ;
35
+ var trainDataset = Path . GetTempFileName ( ) ;
36
+ var testDataset = Path . GetTempFileName ( ) ;
37
+ string [ ] args = new [ ] { "new" , "--ml-task" , "binary-classification" , "--train-dataset" , trainDataset , "--test-dataset" , testDataset , "--label-column-name" , "Label" } ;
33
38
parser . InvokeAsync ( args ) . Wait ( ) ;
34
- File . Delete ( file ) ;
39
+ File . Delete ( trainDataset ) ;
40
+ File . Delete ( testDataset ) ;
35
41
Assert . IsTrue ( parsingSuccessful ) ;
36
42
}
37
43
@@ -42,8 +48,8 @@ public void TestCommandLineArgsFailTest()
42
48
bool parsingSuccessful = false ;
43
49
44
50
// Create handler outside so that commandline and the handler is decoupled and testable.
45
- var handler = CommandHandler . Create < FileInfo , FileInfo , FileInfo , TaskKind , string , uint , uint > (
46
- ( trainDataset , validationDataset , testDataset , mlTask , labelColumnName , maxExplorationTime , labelColumnIndex ) =>
51
+ var handler = CommandHandler . Create < NewCommandOptions > (
52
+ ( opt ) =>
47
53
{
48
54
parsingSuccessful = true ;
49
55
} ) ;
@@ -55,46 +61,56 @@ public void TestCommandLineArgsFailTest()
55
61
. Build ( ) ;
56
62
57
63
// Incorrect mltask test
58
- var file = Path . GetTempFileName ( ) ;
59
- string [ ] args = new [ ] { "new" , "--ml-task" , "BinaryClass" , "--train-dataset" , file , "--label-column-name" , "Label" } ;
64
+ var trainDataset = Path . GetTempFileName ( ) ;
65
+ var testDataset = Path . GetTempFileName ( ) ;
66
+
67
+ //wrong value to ml-task
68
+ string [ ] args = new [ ] { "new" , "--ml-task" , "bad-value" , "--train-dataset" , trainDataset , "--label-column-name" , "Label" } ;
60
69
parser . InvokeAsync ( args ) . Wait ( ) ;
61
70
Assert . IsFalse ( parsingSuccessful ) ;
62
71
63
72
// Incorrect invocation
64
- args = new [ ] { "new" , "BinaryClassification " , "--train-dataset" , file , "--label-column-name" , "Label" } ;
73
+ args = new [ ] { "new" , "binary-classification " , "--train-dataset" , trainDataset , "--label-column-name" , "Label" } ;
65
74
parser . InvokeAsync ( args ) . Wait ( ) ;
66
75
Assert . IsFalse ( parsingSuccessful ) ;
67
76
68
77
// Non-existent file test
69
- args = new [ ] { "new" , "--ml-task" , "BinaryClassification " , "--train-dataset" , "blah .csv" , "--label-column-name" , "Label" } ;
78
+ args = new [ ] { "new" , "--ml-task" , "binary-classification " , "--train-dataset" , "nonexistentfile .csv" , "--label-column-name" , "Label" } ;
70
79
parser . InvokeAsync ( args ) . Wait ( ) ;
71
80
Assert . IsFalse ( parsingSuccessful ) ;
72
81
73
82
// No label column or index test
74
- args = new [ ] { "new" , "--ml-task" , "BinaryClassification " , "--train-dataset" , "blah.csv" } ;
83
+ args = new [ ] { "new" , "--ml-task" , "binary-classification " , "--train-dataset" , trainDataset , "--test-dataset" , testDataset } ;
75
84
parser . InvokeAsync ( args ) . Wait ( ) ;
85
+ File . Delete ( trainDataset ) ;
86
+ File . Delete ( testDataset ) ;
76
87
Assert . IsFalse ( parsingSuccessful ) ;
77
-
78
88
}
79
89
80
90
[ TestMethod ]
81
91
public void TestCommandLineArgsValuesTest ( )
82
92
{
83
93
bool parsingSuccessful = false ;
84
- var file1 = Path . GetTempFileName ( ) ;
85
- var file2 = Path . GetTempFileName ( ) ;
94
+ var trainDataset = Path . GetTempFileName ( ) ;
95
+ var testDataset = Path . GetTempFileName ( ) ;
96
+ var validDataset = Path . GetTempFileName ( ) ;
86
97
var labelName = "Label" ;
98
+ var name = "testname" ;
99
+ var outputPath = "." ;
87
100
88
101
// Create handler outside so that commandline and the handler is decoupled and testable.
89
- var handler = CommandHandler . Create < FileInfo , FileInfo , FileInfo , TaskKind , string , uint , uint > (
90
- ( trainDataset , validationDataset , testDataset , mlTask , labelColumnName , maxExplorationTime , labelColumnIndex ) =>
102
+ var handler = CommandHandler . Create < NewCommandOptions > (
103
+ ( opt ) =>
91
104
{
92
105
parsingSuccessful = true ;
93
- Assert . AreEqual ( mlTask , TaskKind . BinaryClassification ) ;
94
- Assert . AreEqual ( trainDataset , file1 ) ;
95
- Assert . AreEqual ( testDataset , file2 ) ;
96
- Assert . AreEqual ( labelColumnName , labelName ) ;
97
- Assert . AreEqual ( maxExplorationTime , 5 ) ;
106
+ Assert . AreEqual ( opt . MlTask , "binary-classification" ) ;
107
+ Assert . AreEqual ( opt . TrainDataset , trainDataset ) ;
108
+ Assert . AreEqual ( opt . TestDataset , testDataset ) ;
109
+ Assert . AreEqual ( opt . ValidationDataset , validDataset ) ;
110
+ Assert . AreEqual ( opt . LabelColumnName , labelName ) ;
111
+ Assert . AreEqual ( opt . MaxExplorationTime , 5 ) ;
112
+ Assert . AreEqual ( opt . Name , name ) ;
113
+ Assert . AreEqual ( opt . OutputPath , outputPath ) ;
98
114
} ) ;
99
115
100
116
var parser = new CommandLineBuilder ( )
@@ -104,12 +120,55 @@ public void TestCommandLineArgsValuesTest()
104
120
. Build ( ) ;
105
121
106
122
// Incorrect mltask test
107
- string [ ] args = new [ ] { "new" , "--ml-task" , "BinaryClassification " , "--train-dataset" , file1 , "--label-column-name" , labelName , "--test-dataset" , file2 , "--max-exploration-time" , "5" } ;
123
+ string [ ] args = new [ ] { "new" , "--ml-task" , "binary-classification " , "--train-dataset" , trainDataset , "--label-column-name" , labelName , "--validation-dataset" , validDataset , "-- test-dataset", testDataset , "--max-exploration-time" , "5" , "--name" , name , "--output-path" , outputPath } ;
108
124
parser . InvokeAsync ( args ) . Wait ( ) ;
109
- File . Delete ( file1 ) ;
110
- File . Delete ( file2 ) ;
125
+ File . Delete ( trainDataset ) ;
126
+ File . Delete ( testDataset ) ;
127
+ File . Delete ( validDataset ) ;
111
128
Assert . IsTrue ( parsingSuccessful ) ;
112
129
113
130
}
131
+
132
+ [ TestMethod ]
133
+ public void TestCommandLineArgsMutuallyExclusiveArgsTest ( )
134
+ {
135
+ bool parsingSuccessful = false ;
136
+ var dataset = Path . GetTempFileName ( ) ;
137
+ var trainDataset = Path . GetTempFileName ( ) ;
138
+ var testDataset = Path . GetTempFileName ( ) ;
139
+ var labelName = "Label" ;
140
+
141
+ // Create handler outside so that commandline and the handler is decoupled and testable.
142
+ var handler = CommandHandler . Create < NewCommandOptions > (
143
+ ( opt ) =>
144
+ {
145
+ parsingSuccessful = true ;
146
+ } ) ;
147
+
148
+ var parser = new CommandLineBuilder ( )
149
+ // Parser
150
+ . AddCommand ( CommandDefinitions . New ( handler ) )
151
+ . UseDefaults ( )
152
+ . Build ( ) ;
153
+
154
+ // Incorrect arguments : specifying dataset and train-dataset
155
+ string [ ] args = new [ ] { "new" , "--ml-task" , "BinaryClassification" , "--dataset" , dataset , "--train-dataset" , trainDataset , "--label-column-name" , labelName , "--test-dataset" , testDataset , "--max-exploration-time" , "5" } ;
156
+ parser . InvokeAsync ( args ) . Wait ( ) ;
157
+ Assert . IsFalse ( parsingSuccessful ) ;
158
+
159
+ // Incorrect arguments : specifying train-dataset and not specifying test-dataset
160
+ args = new [ ] { "new" , "--ml-task" , "BinaryClassification" , "--train-dataset" , trainDataset , "--label-column-name" , labelName , "--max-exploration-time" , "5" } ;
161
+ parser . InvokeAsync ( args ) . Wait ( ) ;
162
+ Assert . IsFalse ( parsingSuccessful ) ;
163
+
164
+ // Incorrect arguments : specifying label column name and index
165
+ args = new [ ] { "new" , "--ml-task" , "BinaryClassification" , "--train-dataset" , trainDataset , "--label-column-name" , labelName , "--label-column-index" , "0" , "--test-dataset" , testDataset , "--max-exploration-time" , "5" } ;
166
+ parser . InvokeAsync ( args ) . Wait ( ) ;
167
+ File . Delete ( trainDataset ) ;
168
+ File . Delete ( testDataset ) ;
169
+ File . Delete ( dataset ) ;
170
+ Assert . IsFalse ( parsingSuccessful ) ;
171
+
172
+ }
114
173
}
115
174
}
0 commit comments