TestGAMSJob.cs
1using System;
2 using System.IO;
3 using GAMS;
4 using NLog;
5 using NUnit.Framework;
6 
7 namespace 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");
24  GAMSWorkspaceInfo wsInfo = new GAMSWorkspaceInfo();
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 }
GAMSJob AddJobFromFile(string fileName, GAMSCheckpoint checkpoint=null, string jobName=null)
static void prepare(String dir)
Prepare directory by checking its existence. If exists, (non - recursively) delete all its contents,...
Definition: HouseKeeper.cs:30
GAMSDatabase OutDB
static void initializeTestFrom(String filename, String subdir)
initialize class properties from fileName and prepare directory subdir
Definition: HouseKeeper.cs:114
GAMSJob AddJobFromGamsLib(string model, GAMSCheckpoint checkpoint=null, string jobName=null)
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)