Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Services.OKB.DataAccess/3.3/Tests/UnitTest.cs @ 5698

Last change on this file since 5698 was 5295, checked in by swagner, 14 years ago

Worked on OKB (#1174)

File size: 20.2 KB
RevLine 
[4384]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
[5295]22using System;
23using System.Data.Linq;
24using System.Linq;
[4558]25using HeuristicLab.Services.OKB.DataAccess;
[4384]26using Microsoft.VisualStudio.TestTools.UnitTesting;
27
28namespace HeuristicLab.Services.OKB.DataAccess_33.Tests {
29  /// <summary>
30  /// Summary description for UnitTest1
31  /// </summary>
32  [TestClass]
33  public class UnitTest {
[5295]34    private const int PLATFORMS = 1;
35    private const int DATATYPES_PER_PLATFORM = 500;
36    private const int ALGORITHMCLASSES_PER_PLATFORM = 10;
37    private const int ALGORITHMS_PER_CLASS = 10;
38    private const int PROBLEMCLASSES_PER_PLATFORM = 10;
39    private const int PROBLEMS_PER_CLASS = 10;
40    private const int PARAMETERS_PER_ALGORITHM = 10;
41    private const int RESULTS_PER_ALGORITHM = 10;
42    private const int PARAMETERS_PER_PROBLEM = 10;
43    private const int EXPERIMENTS_PER_PLATFORM = 1000;
44    private const int RUNS_PER_EXPERIMENT = 10;
45    private const int ALGORITHMDATA_SIZE = 1024 * 1024;
46    private const int PROBLEMDATA_SIZE = 1024 * 1024;
47    private const int BLOBVALUE_SIZE = 1024;
48
49    private string[] sqlNames = new string[] { "varbinary", "nvarchar", "float", "bit", "bigint" };
50    private Random random = new Random();
51    private DataType[] varbinaryTypes;
52    private DataType[] nvarcharTypes;
53    private DataType[] floatTypes;
54    private DataType[] bitTypes;
55    private DataType[] bigintTypes;
56
[4384]57    public UnitTest() {
58      //
59      // TODO: Add constructor logic here
60      //
61    }
62
63    private TestContext testContextInstance;
64
65    /// <summary>
66    ///Gets or sets the test context which provides
67    ///information about and functionality for the current test run.
68    ///</summary>
69    public TestContext TestContext {
70      get {
71        return testContextInstance;
72      }
73      set {
74        testContextInstance = value;
75      }
76    }
77
78    #region Additional test attributes
79    //
80    // You can use the following additional attributes as you write your tests:
81    //
82    // Use ClassInitialize to run code before running the first test in the class
83    // [ClassInitialize()]
84    // public static void MyClassInitialize(TestContext testContext) { }
85    //
86    // Use ClassCleanup to run code after all tests in a class have run
87    // [ClassCleanup()]
88    // public static void MyClassCleanup() { }
89    //
90    // Use TestInitialize to run code before running each test
91    // [TestInitialize()]
92    // public void MyTestInitialize() { }
93    //
94    // Use TestCleanup to run code after each test has run
95    // [TestCleanup()]
96    // public void MyTestCleanup() { }
97    //
98    #endregion
99
100    [TestMethod]
[5295]101    public void ClearDB() {
[4384]102      using (OKBDataContext okb = new OKBDataContext()) {
[5295]103        okb.ExecuteCommand("DELETE FROM dbo.ResultBlobValue");
104        okb.ExecuteCommand("DELETE FROM dbo.ResultBoolValue");
105        okb.ExecuteCommand("DELETE FROM dbo.ResultFloatValue");
106        okb.ExecuteCommand("DELETE FROM dbo.ResultIntValue");
107        okb.ExecuteCommand("DELETE FROM dbo.ResultStringValue");
108
109        okb.ExecuteCommand("DELETE FROM dbo.AlgorithmParameterBlobValue");
110        okb.ExecuteCommand("DELETE FROM dbo.AlgorithmParameterBoolValue");
111        okb.ExecuteCommand("DELETE FROM dbo.AlgorithmParameterFloatValue");
112        okb.ExecuteCommand("DELETE FROM dbo.AlgorithmParameterIntValue");
113        okb.ExecuteCommand("DELETE FROM dbo.AlgorithmParameterStringValue");
114
115        okb.ExecuteCommand("DELETE FROM dbo.ProblemParameterBlobValue");
116        okb.ExecuteCommand("DELETE FROM dbo.ProblemParameterBoolValue");
117        okb.ExecuteCommand("DELETE FROM dbo.ProblemParameterFloatValue");
118        okb.ExecuteCommand("DELETE FROM dbo.ProblemParameterIntValue");
119        okb.ExecuteCommand("DELETE FROM dbo.ProblemParameterStringValue");
120
121        okb.ExecuteCommand("DELETE FROM dbo.Run");
122        okb.ExecuteCommand("DELETE FROM dbo.Experiment");
123        okb.ExecuteCommand("DELETE FROM dbo.AlgorithmData");
124        okb.ExecuteCommand("DELETE FROM dbo.AlgorithmUser");
125        okb.ExecuteCommand("DELETE FROM dbo.AlgorithmParameter");
126        okb.ExecuteCommand("DELETE FROM dbo.Result");
127        okb.ExecuteCommand("DELETE FROM dbo.Algorithm");
128        okb.ExecuteCommand("DELETE FROM dbo.AlgorithmClass");
129        okb.ExecuteCommand("DELETE FROM dbo.ProblemData");
130        okb.ExecuteCommand("DELETE FROM dbo.ProblemUser");
131        okb.ExecuteCommand("DELETE FROM dbo.ProblemParameter");
132        okb.ExecuteCommand("DELETE FROM dbo.Problem");
133        okb.ExecuteCommand("DELETE FROM dbo.ProblemClass");
134        okb.ExecuteCommand("DELETE FROM dbo.DataType");
135        okb.ExecuteCommand("DELETE FROM dbo.Platform");
136      }
137    }
138
139    [TestMethod]
140    public void InitDummyDB() {
141      using (OKBDataContext okb = new OKBDataContext()) {
142        for (int i = 0; i < PLATFORMS; i++) {
143          CreatePlatform(okb);
144        }
[4384]145        okb.SubmitChanges();
146      }
147    }
[5295]148
149    [TestMethod]
150    public void CreateDummyExperiments() {
151      using (OKBDataContext okb = new OKBDataContext()) {
152        foreach (Platform platform in okb.Platforms) {
153          for (int i = 0; i < EXPERIMENTS_PER_PLATFORM; i++) {
154            Algorithm[] algorithms = okb.Algorithms.Where(x => x.PlatformId == platform.Id).ToArray();
155            Problem[] problems = okb.Problems.Where(x => x.PlatformId == platform.Id).ToArray();
156            varbinaryTypes = null;
157            nvarcharTypes = null;
158            floatTypes = null;
159            bitTypes = null;
160            bigintTypes = null;
161
162            CreateExperiment(okb, algorithms[random.Next(algorithms.Length)], problems[random.Next(problems.Length)], platform);
163          }
164          okb.SubmitChanges();
165        }
166      }
167    }
168
169    private void CreatePlatform(OKBDataContext okb) {
170      Platform entity = new Platform();
171      entity.Name = Guid.NewGuid().ToString();
172      entity.Description = Guid.NewGuid().ToString();
173      okb.Platforms.InsertOnSubmit(entity);
174
175      for (int i = 0; i < DATATYPES_PER_PLATFORM; i++) {
176        CreateDataType(okb, entity);
177      }
178      okb.SubmitChanges();
179
180      varbinaryTypes = null;
181      nvarcharTypes = null;
182      floatTypes = null;
183      bitTypes = null;
184      bigintTypes = null;
185
186      for (int i = 0; i < ALGORITHMCLASSES_PER_PLATFORM; i++) {
187        CreateAlgorithmClass(okb, entity);
188      }
189      for (int i = 0; i < PROBLEMCLASSES_PER_PLATFORM; i++) {
190        CreateProblemClass(okb, entity);
191      }
192    }
193    private void CreateDataType(OKBDataContext okb, Platform platform) {
194      DataType entity = new DataType();
195      entity.Name = Guid.NewGuid().ToString();
196      entity.TypeName = Guid.NewGuid().ToString();
197      entity.SqlName = sqlNames[random.Next(sqlNames.Length)];
198      entity.Platform = platform;
199      okb.DataTypes.InsertOnSubmit(entity);
200    }
201    private DataType GetDataType(OKBDataContext okb, Platform platform, string sqlName) {
202      if (varbinaryTypes == null) varbinaryTypes = okb.DataTypes.Where(x => (x.PlatformId == platform.Id) && (x.SqlName == "varbinary")).ToArray();
203      if (nvarcharTypes == null) nvarcharTypes = okb.DataTypes.Where(x => (x.PlatformId == platform.Id) && (x.SqlName == "nvarchar")).ToArray();
204      if (floatTypes == null) floatTypes = okb.DataTypes.Where(x => (x.PlatformId == platform.Id) && (x.SqlName == "float")).ToArray();
205      if (bitTypes == null) bitTypes = okb.DataTypes.Where(x => (x.PlatformId == platform.Id) && (x.SqlName == "bit")).ToArray();
206      if (bigintTypes == null) bigintTypes = okb.DataTypes.Where(x => (x.PlatformId == platform.Id) && (x.SqlName == "bigint")).ToArray();
207
208      switch (sqlName) {
209        case "varbinary": return varbinaryTypes[random.Next(varbinaryTypes.Length)];
210        case "nvarchar": return nvarcharTypes[random.Next(nvarcharTypes.Length)];
211        case "float": return floatTypes[random.Next(floatTypes.Length)];
212        case "bit": return bitTypes[random.Next(bitTypes.Length)];
213        case "bigint": return bigintTypes[random.Next(bigintTypes.Length)];
214        default: return null;
215      }
216    }
217    private void CreateAlgorithmClass(OKBDataContext okb, Platform platform) {
218      AlgorithmClass entity = new AlgorithmClass();
219      entity.Name = Guid.NewGuid().ToString();
220      entity.Description = Guid.NewGuid().ToString();
221      for (int i = 0; i < ALGORITHMS_PER_CLASS; i++) {
222        CreateAlgorithm(okb, entity, platform);
223      }
224      okb.AlgorithmClasses.InsertOnSubmit(entity);
225    }
226    private void CreateProblemClass(OKBDataContext okb, Platform platform) {
227      ProblemClass entity = new ProblemClass();
228      entity.Name = Guid.NewGuid().ToString();
229      entity.Description = Guid.NewGuid().ToString();
230      for (int i = 0; i < PROBLEMS_PER_CLASS; i++) {
231        CreateProblem(okb, entity, platform);
232      }
233      okb.ProblemClasses.InsertOnSubmit(entity);
234    }
235    private void CreateAlgorithm(OKBDataContext okb, AlgorithmClass algorithmClass, Platform platform) {
236      Algorithm entity = new Algorithm();
237      entity.Name = Guid.NewGuid().ToString();
238      entity.Description = Guid.NewGuid().ToString();
239      entity.AlgorithmClass = algorithmClass;
240      entity.Platform = platform;
241      CreateAlgorithmData(okb, entity, platform);
242      for (int i = 0; i < PARAMETERS_PER_ALGORITHM; i++) {
243        CreateAlgorithmParameter(okb, entity, platform);
244      }
245      for (int i = 0; i < RESULTS_PER_ALGORITHM; i++) {
246        CreateResult(okb, entity, platform);
247      }
248      okb.Algorithms.InsertOnSubmit(entity);
249    }
250    private void CreateAlgorithmData(OKBDataContext okb, Algorithm algorithm, Platform platform) {
251      AlgorithmData entity = new AlgorithmData();
252      entity.Algorithm = algorithm;
253      entity.Data = new System.Data.Linq.Binary(new byte[ALGORITHMDATA_SIZE]);
254      entity.DataType = GetDataType(okb, platform, "varbinary");
255      okb.AlgorithmDatas.InsertOnSubmit(entity);
256    }
257    private void CreateAlgorithmParameter(OKBDataContext okb, Algorithm algorithm, Platform platform) {
258      AlgorithmParameter entity = new AlgorithmParameter();
259      entity.Name = Guid.NewGuid().ToString();
260      entity.Alias = Guid.NewGuid().ToString();
261      entity.Description = Guid.NewGuid().ToString();
262      entity.Algorithm = algorithm;
263      entity.DataType = GetDataType(okb, platform, sqlNames[random.Next(sqlNames.Length)]);
264      okb.AlgorithmParameters.InsertOnSubmit(entity);
265    }
266    private void CreateResult(OKBDataContext okb, Algorithm algorithm, Platform platform) {
267      Result entity = new Result();
268      entity.Name = Guid.NewGuid().ToString();
269      entity.Alias = Guid.NewGuid().ToString();
270      entity.Description = Guid.NewGuid().ToString();
271      entity.Algorithm = algorithm;
272      entity.DataType = GetDataType(okb, platform, sqlNames[random.Next(sqlNames.Length)]);
273      okb.Results.InsertOnSubmit(entity);
274    }
275    private void CreateProblem(OKBDataContext okb, ProblemClass problemClass, Platform platform) {
276      Problem entity = new Problem();
277      entity.Name = Guid.NewGuid().ToString();
278      entity.Description = Guid.NewGuid().ToString();
279      entity.ProblemClass = problemClass;
280      entity.Platform = platform;
281      CreateProblemData(okb, entity, platform);
282      for (int i = 0; i < PARAMETERS_PER_PROBLEM; i++) {
283        CreateProblemParameter(okb, entity, platform);
284      }
285      okb.Problems.InsertOnSubmit(entity);
286    }
287    private void CreateProblemData(OKBDataContext okb, Problem problem, Platform platform) {
288      ProblemData entity = new ProblemData();
289      entity.Problem = problem;
290      entity.Data = new System.Data.Linq.Binary(new byte[PROBLEMDATA_SIZE]);
291      entity.DataType = GetDataType(okb, platform, "varbinary");
292      okb.ProblemDatas.InsertOnSubmit(entity);
293    }
294    private void CreateProblemParameter(OKBDataContext okb, Problem problem, Platform platform) {
295      ProblemParameter entity = new ProblemParameter();
296      entity.Name = Guid.NewGuid().ToString();
297      entity.Alias = Guid.NewGuid().ToString();
298      entity.Description = Guid.NewGuid().ToString();
299      entity.Problem = problem;
300      entity.DataType = GetDataType(okb, platform, sqlNames[random.Next(sqlNames.Length)]);
301      okb.ProblemParameters.InsertOnSubmit(entity);
302    }
303    private void CreateExperiment(OKBDataContext okb, Algorithm algorithm, Problem problem, Platform platform) {
304      Experiment entity = new Experiment();
305      entity.Algorithm = algorithm;
306      entity.Problem = problem;
307
308      foreach (AlgorithmParameter algorithmParameter in okb.AlgorithmParameters.Where(x => x.AlgorithmId == algorithm.Id)) {
309        CreateAlgorithmParameterValue(okb, algorithmParameter, entity, platform);
310      }
311      foreach (ProblemParameter problemParameter in okb.ProblemParameters.Where(x => x.ProblemId == problem.Id)) {
312        CreateProblemParameterValue(okb, problemParameter, entity, platform);
313      }
314
315      for (int i = 0; i < RUNS_PER_EXPERIMENT; i++) {
316        CreateRun(okb, entity, platform);
317      }
318      okb.Experiments.InsertOnSubmit(entity);
319    }
320    private void CreateAlgorithmParameterValue(OKBDataContext okb, AlgorithmParameter algorithmParameter, Experiment experiment, Platform platform) {
321      switch (algorithmParameter.DataType.SqlName) {
322        case "varbinary":
323          AlgorithmParameterBlobValue v1 = new AlgorithmParameterBlobValue();
324          v1.AlgorithmParameter = algorithmParameter;
325          v1.Experiment = experiment;
326          v1.Value = new Binary(new byte[BLOBVALUE_SIZE]);
327          v1.DataType = GetDataType(okb, platform, "varbinary");
328          okb.AlgorithmParameterBlobValues.InsertOnSubmit(v1);
329          return;
330        case "bit":
331          AlgorithmParameterBoolValue v2 = new AlgorithmParameterBoolValue();
332          v2.AlgorithmParameter = algorithmParameter;
333          v2.Experiment = experiment;
334          v2.Value = random.Next(2) == 0;
335          v2.DataType = GetDataType(okb, platform, "bit");
336          okb.AlgorithmParameterBoolValues.InsertOnSubmit(v2);
337          return;
338        case "float":
339          AlgorithmParameterFloatValue v3 = new AlgorithmParameterFloatValue();
340          v3.AlgorithmParameter = algorithmParameter;
341          v3.Experiment = experiment;
342          v3.Value = random.NextDouble();
343          v3.DataType = GetDataType(okb, platform, "float");
344          okb.AlgorithmParameterFloatValues.InsertOnSubmit(v3);
345          return;
346        case "bigint":
347          AlgorithmParameterIntValue v4 = new AlgorithmParameterIntValue();
348          v4.AlgorithmParameter = algorithmParameter;
349          v4.Experiment = experiment;
350          v4.Value = random.Next();
351          v4.DataType = GetDataType(okb, platform, "bigint");
352          okb.AlgorithmParameterIntValues.InsertOnSubmit(v4);
353          return;
354        case "nvarchar":
355          AlgorithmParameterStringValue v5 = new AlgorithmParameterStringValue();
356          v5.AlgorithmParameter = algorithmParameter;
357          v5.Experiment = experiment;
358          v5.Value = Guid.NewGuid().ToString();
359          v5.DataType = GetDataType(okb, platform, "nvarchar");
360          okb.AlgorithmParameterStringValues.InsertOnSubmit(v5);
361          return;
362      }
363    }
364    private void CreateProblemParameterValue(OKBDataContext okb, ProblemParameter problemParameter, Experiment experiment, Platform platform) {
365      switch (problemParameter.DataType.SqlName) {
366        case "varbinary":
367          ProblemParameterBlobValue v1 = new ProblemParameterBlobValue();
368          v1.ProblemParameter = problemParameter;
369          v1.Experiment = experiment;
370          v1.Value = new Binary(new byte[BLOBVALUE_SIZE]);
371          v1.DataType = GetDataType(okb, platform, "varbinary");
372          okb.ProblemParameterBlobValues.InsertOnSubmit(v1);
373          return;
374        case "bit":
375          ProblemParameterBoolValue v2 = new ProblemParameterBoolValue();
376          v2.ProblemParameter = problemParameter;
377          v2.Experiment = experiment;
378          v2.Value = random.Next(2) == 0;
379          v2.DataType = GetDataType(okb, platform, "bit");
380          okb.ProblemParameterBoolValues.InsertOnSubmit(v2);
381          return;
382        case "float":
383          ProblemParameterFloatValue v3 = new ProblemParameterFloatValue();
384          v3.ProblemParameter = problemParameter;
385          v3.Experiment = experiment;
386          v3.Value = random.NextDouble();
387          v3.DataType = GetDataType(okb, platform, "float");
388          okb.ProblemParameterFloatValues.InsertOnSubmit(v3);
389          return;
390        case "bigint":
391          ProblemParameterIntValue v4 = new ProblemParameterIntValue();
392          v4.ProblemParameter = problemParameter;
393          v4.Experiment = experiment;
394          v4.Value = random.Next();
395          v4.DataType = GetDataType(okb, platform, "bigint");
396          okb.ProblemParameterIntValues.InsertOnSubmit(v4);
397          return;
398        case "nvarchar":
399          ProblemParameterStringValue v5 = new ProblemParameterStringValue();
400          v5.ProblemParameter = problemParameter;
401          v5.Experiment = experiment;
402          v5.Value = Guid.NewGuid().ToString();
403          v5.DataType = GetDataType(okb, platform, "nvarchar");
404          okb.ProblemParameterStringValues.InsertOnSubmit(v5);
405          return;
406      }
407    }
408    private void CreateRun(OKBDataContext okb, Experiment experiment, Platform platform) {
409      Run entity = new Run();
410      entity.RandomSeed = random.Next();
411      entity.CreatedDate = DateTime.Now;
412      entity.UserId = Guid.NewGuid();
413      entity.ClientId = Guid.NewGuid();
414      entity.Experiment = experiment;
415
416      foreach (Result result in okb.Results.Where(x => x.AlgorithmId == experiment.AlgorithmId)) {
417        CreateResultValue(okb, result, entity, platform);
418      }
419      okb.Runs.InsertOnSubmit(entity);
420    }
421    private void CreateResultValue(OKBDataContext okb, Result result, Run run, Platform platform) {
422      switch (result.DataType.SqlName) {
423        case "varbinary":
424          ResultBlobValue v1 = new ResultBlobValue();
425          v1.Result = result;
426          v1.Run = run;
427          v1.Value = new Binary(new byte[BLOBVALUE_SIZE]);
428          v1.DataType = GetDataType(okb, platform, "varbinary");
429          okb.ResultBlobValues.InsertOnSubmit(v1);
430          return;
431        case "bit":
432          ResultBoolValue v2 = new ResultBoolValue();
433          v2.Result = result;
434          v2.Run = run;
435          v2.Value = random.Next(2) == 0;
436          v2.DataType = GetDataType(okb, platform, "bit");
437          okb.ResultBoolValues.InsertOnSubmit(v2);
438          return;
439        case "float":
440          ResultFloatValue v3 = new ResultFloatValue();
441          v3.Result = result;
442          v3.Run = run;
443          v3.Value = random.NextDouble();
444          v3.DataType = GetDataType(okb, platform, "float");
445          okb.ResultFloatValues.InsertOnSubmit(v3);
446          return;
447        case "bigint":
448          ResultIntValue v4 = new ResultIntValue();
449          v4.Result = result;
450          v4.Run = run;
451          v4.Value = random.Next();
452          v4.DataType = GetDataType(okb, platform, "bigint");
453          okb.ResultIntValues.InsertOnSubmit(v4);
454          return;
455        case "nvarchar":
456          ResultStringValue v5 = new ResultStringValue();
457          v5.Result = result;
458          v5.Run = run;
459          v5.Value = Guid.NewGuid().ToString();
460          v5.DataType = GetDataType(okb, platform, "nvarchar");
461          okb.ResultStringValues.InsertOnSubmit(v5);
462          return;
463      }
464    }
[4384]465  }
466}
Note: See TracBrowser for help on using the repository browser.