Loading...
Searching...
No Matches
TestGAMSJob.cs
1using System;
2using System.IO;
3using GAMS;
4using NLog;
5using NUnit.Framework;
6
7namespace GamsApiTests
8{
9 [TestFixture]
10 public class TestGAMSJob
11 {
12 static readonly String PROPERTIES_FILE = "test.properties";
13 static GAMSWorkspace globalWS;
14 static GAMSDatabase globalDB;
15 static GAMSWorkspace ws;
16 static GAMSDatabase db;
17 static Logger logger = LogManager.GetCurrentClassLogger();
18
19 [OneTimeSetUp]
20 public void OneTimeSetup() {
21 logger.Info("entering TestGAMSJob", "setUpBeforeClass");
22 HouseKeeper.initializeTestFrom(PROPERTIES_FILE, "TestGAMSJob");
23 logger.Trace("HouseKeeper task initialized");
25 wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
26 wsInfo.SystemDirectory = HouseKeeper.gamsSystemDir;
27 wsInfo.Debug = DebugLevel.Off;
28 globalWS = new GAMSWorkspace(wsInfo);
29 logger.Trace("Workspace created");
30 globalDB = globalWS.AddDatabase("globalDB");
31 logger.Trace("database created");
32 TestGAMSDatabase.initializeDatabase(globalDB);
33 logger.Info("exiting TestGAMSJob","Setup");
34 }
35
36 [OneTimeTearDown]
37 public void OneTimeTearDown()
38 {
39 logger.Info("entering TestGAMSJob", "Setup");
40 if (globalDB != null)
41 globalDB.Dispose();
42
43 logger.Info("exiting TestGAMSJob", "Setup");
44 }
45
46 [SetUp]
47 public void Setup()
48 {
49 ws = null;
50 db = null;
51 }
52
53 [TearDown]
54 public void TearDown()
55 {
56 if(db != null)
57 db.Dispose();
58 }
59
60 [Test]
61 public void testAddJobFromNonExistFileString()
62 {
63 logger.Debug("entering testAddJobFromNonExistingFileString");
64 ws = new GAMSWorkspace("testAddJobFromFileString");
65 db = ws.AddDatabase("testAddJobFromFileString");
66 Assert.Throws<GAMSException>(() => ws.AddJobFromFile("ThisIsProbablyANonExistFileName.gms"));
67 Assert.That(true, "Test skipped", null);
68 }
69
70 [Test]
71 public void testAddJobFromFileString() {
72 logger.Debug("entering TestGAMSJob", "testAddJobFromFileString");
73
74 String testDir = HouseKeeper.gamsWorkingDir +
75 Path.DirectorySeparatorChar +
76 "testAddJobFromFileString";
77 HouseKeeper.prepare(testDir);
78
79 ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir, DebugLevel.Off);
80 db = ws.AddDatabase("TtestAddJobFromFileString");
81
82 GAMSJob job = ws.AddJobFromGamsLib("diet");
83 Assert.NotNull(job, "does not expect a NULL GAMSJob", null);
84
85 job = ws.AddJobFromFile("diet.gms");
86 Assert.NotNull(job.Name, "does not expect GAMSJob instance with a NULL file name", null);
87
88 logger.Debug("job initialized");
89 job.Run();
90 logger.Debug("job executed");
91
92 // cleanup
93 if (job != null)
94 {
95 if (job.OutDB != null)
96 job.OutDB.Dispose();
97 }
98 logger.Info("exiting TestGAMSJob", "testAddJobFromFileString");
99 }
100
101 [Test]
102 public void testAddJobFromFileGAMSWorkspaceString()
103 {
104 logger.Debug("entering TestGAMSJob", "testAddJobFromFileGAMSWorkspaceString");
105
106 String testDir = HouseKeeper.gamsWorkingDir +
107 Path.DirectorySeparatorChar +
108 "testAddJobFromFileGAMSWorkspaceString";
109 HouseKeeper.prepare(testDir);
110
111 ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir);
112 db = ws.AddDatabase("testAddJobFromFileStringGAMSWorkspaceString");
113
114 GAMSJob job = ws.AddJobFromGamsLib("diet");
115 String jobName = "testAddJobFromFileGAMSWorkspaceString";
116
117 job = ws.AddJobFromFile("diet.gms", null, jobName);
118 Assert.NotNull("does not expect GAMSJob instance with a NULL file name", job.Name);
119 Assert.AreEqual(job.Name, jobName, "expect GAMSJob instance whose name is [" +
120 jobName + "]");
121 logger.Trace("job initialized");
122
123 job.Run();
124
125 logger.Trace("job executed");
126 // cleanup
127 if (job != null)
128 {
129 if (job.OutDB != null)
130 job.OutDB.Dispose();
131 }
132 logger.Debug("exiting TestGAMSJob", "testAddJobFromFileGAMSWorkspaceString");
133 }
134
135 [Test]
136 public void testRunWithMultipleTimesDefinesOption()
137 {
138 logger.Debug("Entering TestGAMSJob | testRunWithMultipleTimesDefinesOption");
139 // given
140 int numberOfTimes = 5000;
141 String testDir = HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testRunWithMultipleTimesDefinesOption";
142 HouseKeeper.prepare(testDir);
143
144 ws = new GAMSWorkspace(HouseKeeper.gamsWorkingDir, HouseKeeper.gamsSystemDir);
145
146 String data = "Sets i canning plants / seattle, san-diego / ; ";
147 String model = "Sets i canning plants \n" +
148 "$if not set gdxincname $abort 'no include file name for data file provided'\n" +
149 "$gdxin %gdxincname% \n" +
150 "$load i \n" +
151 "$gdxin \n" +
152 "display i; ";
153 GAMSJob job = ws.AddJobFromString(data);
154 job.Run();
155 job.OutDB.Export(ws.WorkingDirectory + Path.DirectorySeparatorChar + "pdata.gdx");
156
157 job = ws.AddJobFromString(model);
158 // creating an instance of GAMSOptions that defines the data include file multiple times
159 GAMSOptions opt = ws.AddOptions();
160 for (int i = 0; i < numberOfTimes; i++)
161 opt.Defines["dummyname" + i] = "dummyvalue" + i;
162 opt.Defines["gdxincname"] = "pdata";
163
164 // when
165 job.Run(opt);
166
167 // then
168 Assert.AreEqual(2, job.OutDB.GetSet("i").NumberRecords, "expect 2 set records but found [" + job.OutDB.GetSet("i").NumberRecords + "]");
169
170 // cleanup
171 if (job != null)
172 if (job.OutDB != null)
173 job.OutDB.Dispose();
174 logger.Debug("Exiting TestGAMSJob | testRunWithMultipleTimesDefinesOption");
175 }
176
177 [Test]
178 public void testOutDB()
179 {
180 logger.Debug("Entering TestGAMSJob | testOutDB");
181 // given
182 ws = new GAMSWorkspace(HouseKeeper.gamsWorkingDir, HouseKeeper.gamsSystemDir);
183 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
184 // when, then
185 Assert.True(job.OutDB == null, "expect null OutDB before job.run");
186
187 //when
188 job.Run();
189 // when, then
190 Assert.AreEqual(job.OutDB.NrSymbols, 12, "expect null OutDB before job.run");
191
192 logger.Debug("Exiting TestGAMSJob | testOutDB");
193 }
194
195 }
196}
GAMSSet GetSet(string setIdentifier)
void Export(string filePath=null)
GAMSDatabase OutDB
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
Dictionary< string, string > Defines
GAMSJob AddJobFromString(string gamsSource, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSJob AddJobFromGamsLib(string model, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)
GAMSJob AddJobFromFile(string fileName, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSOptions AddOptions(GAMSOptions optFrom=null)
static void prepare(String dir)
Prepare directory by checking its existence. If exists, (non - recursively) delete all its contents,...
Definition: HouseKeeper.cs:30
static void initializeTestFrom(String filename, String subdir)
initialize class properties from fileName and prepare directory subdir
Definition: HouseKeeper.cs:114