#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Data.Linq;
using System.Linq;
using HeuristicLab.Services.OKB.DataAccess;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace HeuristicLab.Services.OKB.DataAccess_33.Tests {
///
/// Summary description for UnitTest1
///
[TestClass]
public class UnitTest {
private const int PLATFORMS = 1;
private const int DATATYPES_PER_PLATFORM = 500;
private const int ALGORITHMCLASSES_PER_PLATFORM = 10;
private const int ALGORITHMS_PER_CLASS = 10;
private const int PROBLEMCLASSES_PER_PLATFORM = 10;
private const int PROBLEMS_PER_CLASS = 10;
private const int PARAMETERS_PER_ALGORITHM = 10;
private const int RESULTS_PER_ALGORITHM = 10;
private const int PARAMETERS_PER_PROBLEM = 10;
private const int EXPERIMENTS_PER_PLATFORM = 1000;
private const int RUNS_PER_EXPERIMENT = 10;
private const int ALGORITHMDATA_SIZE = 1024 * 1024;
private const int PROBLEMDATA_SIZE = 1024 * 1024;
private const int BLOBVALUE_SIZE = 1024;
private string[] sqlNames = new string[] { "varbinary", "nvarchar", "float", "bit", "bigint" };
private Random random = new Random();
private DataType[] varbinaryTypes;
private DataType[] nvarcharTypes;
private DataType[] floatTypes;
private DataType[] bitTypes;
private DataType[] bigintTypes;
public UnitTest() {
//
// TODO: Add constructor logic here
//
}
private TestContext testContextInstance;
///
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///
public TestContext TestContext {
get {
return testContextInstance;
}
set {
testContextInstance = value;
}
}
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
[TestMethod]
public void ClearDB() {
using (OKBDataContext okb = new OKBDataContext()) {
okb.ExecuteCommand("DELETE FROM dbo.ResultBlobValue");
okb.ExecuteCommand("DELETE FROM dbo.ResultBoolValue");
okb.ExecuteCommand("DELETE FROM dbo.ResultFloatValue");
okb.ExecuteCommand("DELETE FROM dbo.ResultIntValue");
okb.ExecuteCommand("DELETE FROM dbo.ResultStringValue");
okb.ExecuteCommand("DELETE FROM dbo.AlgorithmParameterBlobValue");
okb.ExecuteCommand("DELETE FROM dbo.AlgorithmParameterBoolValue");
okb.ExecuteCommand("DELETE FROM dbo.AlgorithmParameterFloatValue");
okb.ExecuteCommand("DELETE FROM dbo.AlgorithmParameterIntValue");
okb.ExecuteCommand("DELETE FROM dbo.AlgorithmParameterStringValue");
okb.ExecuteCommand("DELETE FROM dbo.ProblemParameterBlobValue");
okb.ExecuteCommand("DELETE FROM dbo.ProblemParameterBoolValue");
okb.ExecuteCommand("DELETE FROM dbo.ProblemParameterFloatValue");
okb.ExecuteCommand("DELETE FROM dbo.ProblemParameterIntValue");
okb.ExecuteCommand("DELETE FROM dbo.ProblemParameterStringValue");
okb.ExecuteCommand("DELETE FROM dbo.Run");
okb.ExecuteCommand("DELETE FROM dbo.Experiment");
okb.ExecuteCommand("DELETE FROM dbo.AlgorithmData");
okb.ExecuteCommand("DELETE FROM dbo.AlgorithmUser");
okb.ExecuteCommand("DELETE FROM dbo.AlgorithmParameter");
okb.ExecuteCommand("DELETE FROM dbo.Result");
okb.ExecuteCommand("DELETE FROM dbo.Algorithm");
okb.ExecuteCommand("DELETE FROM dbo.AlgorithmClass");
okb.ExecuteCommand("DELETE FROM dbo.ProblemData");
okb.ExecuteCommand("DELETE FROM dbo.ProblemUser");
okb.ExecuteCommand("DELETE FROM dbo.ProblemParameter");
okb.ExecuteCommand("DELETE FROM dbo.Problem");
okb.ExecuteCommand("DELETE FROM dbo.ProblemClass");
okb.ExecuteCommand("DELETE FROM dbo.DataType");
okb.ExecuteCommand("DELETE FROM dbo.Platform");
}
}
[TestMethod]
public void InitDummyDB() {
using (OKBDataContext okb = new OKBDataContext()) {
for (int i = 0; i < PLATFORMS; i++) {
CreatePlatform(okb);
}
okb.SubmitChanges();
}
}
[TestMethod]
public void CreateDummyExperiments() {
using (OKBDataContext okb = new OKBDataContext()) {
foreach (Platform platform in okb.Platforms) {
for (int i = 0; i < EXPERIMENTS_PER_PLATFORM; i++) {
Algorithm[] algorithms = okb.Algorithms.Where(x => x.PlatformId == platform.Id).ToArray();
Problem[] problems = okb.Problems.Where(x => x.PlatformId == platform.Id).ToArray();
varbinaryTypes = null;
nvarcharTypes = null;
floatTypes = null;
bitTypes = null;
bigintTypes = null;
CreateExperiment(okb, algorithms[random.Next(algorithms.Length)], problems[random.Next(problems.Length)], platform);
}
okb.SubmitChanges();
}
}
}
private void CreatePlatform(OKBDataContext okb) {
Platform entity = new Platform();
entity.Name = Guid.NewGuid().ToString();
entity.Description = Guid.NewGuid().ToString();
okb.Platforms.InsertOnSubmit(entity);
for (int i = 0; i < DATATYPES_PER_PLATFORM; i++) {
CreateDataType(okb, entity);
}
okb.SubmitChanges();
varbinaryTypes = null;
nvarcharTypes = null;
floatTypes = null;
bitTypes = null;
bigintTypes = null;
for (int i = 0; i < ALGORITHMCLASSES_PER_PLATFORM; i++) {
CreateAlgorithmClass(okb, entity);
}
for (int i = 0; i < PROBLEMCLASSES_PER_PLATFORM; i++) {
CreateProblemClass(okb, entity);
}
}
private void CreateDataType(OKBDataContext okb, Platform platform) {
DataType entity = new DataType();
entity.Name = Guid.NewGuid().ToString();
entity.TypeName = Guid.NewGuid().ToString();
entity.SqlName = sqlNames[random.Next(sqlNames.Length)];
entity.Platform = platform;
okb.DataTypes.InsertOnSubmit(entity);
}
private DataType GetDataType(OKBDataContext okb, Platform platform, string sqlName) {
if (varbinaryTypes == null) varbinaryTypes = okb.DataTypes.Where(x => (x.PlatformId == platform.Id) && (x.SqlName == "varbinary")).ToArray();
if (nvarcharTypes == null) nvarcharTypes = okb.DataTypes.Where(x => (x.PlatformId == platform.Id) && (x.SqlName == "nvarchar")).ToArray();
if (floatTypes == null) floatTypes = okb.DataTypes.Where(x => (x.PlatformId == platform.Id) && (x.SqlName == "float")).ToArray();
if (bitTypes == null) bitTypes = okb.DataTypes.Where(x => (x.PlatformId == platform.Id) && (x.SqlName == "bit")).ToArray();
if (bigintTypes == null) bigintTypes = okb.DataTypes.Where(x => (x.PlatformId == platform.Id) && (x.SqlName == "bigint")).ToArray();
switch (sqlName) {
case "varbinary": return varbinaryTypes[random.Next(varbinaryTypes.Length)];
case "nvarchar": return nvarcharTypes[random.Next(nvarcharTypes.Length)];
case "float": return floatTypes[random.Next(floatTypes.Length)];
case "bit": return bitTypes[random.Next(bitTypes.Length)];
case "bigint": return bigintTypes[random.Next(bigintTypes.Length)];
default: return null;
}
}
private void CreateAlgorithmClass(OKBDataContext okb, Platform platform) {
AlgorithmClass entity = new AlgorithmClass();
entity.Name = Guid.NewGuid().ToString();
entity.Description = Guid.NewGuid().ToString();
for (int i = 0; i < ALGORITHMS_PER_CLASS; i++) {
CreateAlgorithm(okb, entity, platform);
}
okb.AlgorithmClasses.InsertOnSubmit(entity);
}
private void CreateProblemClass(OKBDataContext okb, Platform platform) {
ProblemClass entity = new ProblemClass();
entity.Name = Guid.NewGuid().ToString();
entity.Description = Guid.NewGuid().ToString();
for (int i = 0; i < PROBLEMS_PER_CLASS; i++) {
CreateProblem(okb, entity, platform);
}
okb.ProblemClasses.InsertOnSubmit(entity);
}
private void CreateAlgorithm(OKBDataContext okb, AlgorithmClass algorithmClass, Platform platform) {
Algorithm entity = new Algorithm();
entity.Name = Guid.NewGuid().ToString();
entity.Description = Guid.NewGuid().ToString();
entity.AlgorithmClass = algorithmClass;
entity.Platform = platform;
CreateAlgorithmData(okb, entity, platform);
for (int i = 0; i < PARAMETERS_PER_ALGORITHM; i++) {
CreateAlgorithmParameter(okb, entity, platform);
}
for (int i = 0; i < RESULTS_PER_ALGORITHM; i++) {
CreateResult(okb, entity, platform);
}
okb.Algorithms.InsertOnSubmit(entity);
}
private void CreateAlgorithmData(OKBDataContext okb, Algorithm algorithm, Platform platform) {
AlgorithmData entity = new AlgorithmData();
entity.Algorithm = algorithm;
entity.Data = new System.Data.Linq.Binary(new byte[ALGORITHMDATA_SIZE]);
entity.DataType = GetDataType(okb, platform, "varbinary");
okb.AlgorithmDatas.InsertOnSubmit(entity);
}
private void CreateAlgorithmParameter(OKBDataContext okb, Algorithm algorithm, Platform platform) {
AlgorithmParameter entity = new AlgorithmParameter();
entity.Name = Guid.NewGuid().ToString();
entity.Alias = Guid.NewGuid().ToString();
entity.Description = Guid.NewGuid().ToString();
entity.Algorithm = algorithm;
entity.DataType = GetDataType(okb, platform, sqlNames[random.Next(sqlNames.Length)]);
okb.AlgorithmParameters.InsertOnSubmit(entity);
}
private void CreateResult(OKBDataContext okb, Algorithm algorithm, Platform platform) {
Result entity = new Result();
entity.Name = Guid.NewGuid().ToString();
entity.Alias = Guid.NewGuid().ToString();
entity.Description = Guid.NewGuid().ToString();
entity.Algorithm = algorithm;
entity.DataType = GetDataType(okb, platform, sqlNames[random.Next(sqlNames.Length)]);
okb.Results.InsertOnSubmit(entity);
}
private void CreateProblem(OKBDataContext okb, ProblemClass problemClass, Platform platform) {
Problem entity = new Problem();
entity.Name = Guid.NewGuid().ToString();
entity.Description = Guid.NewGuid().ToString();
entity.ProblemClass = problemClass;
entity.Platform = platform;
CreateProblemData(okb, entity, platform);
for (int i = 0; i < PARAMETERS_PER_PROBLEM; i++) {
CreateProblemParameter(okb, entity, platform);
}
okb.Problems.InsertOnSubmit(entity);
}
private void CreateProblemData(OKBDataContext okb, Problem problem, Platform platform) {
ProblemData entity = new ProblemData();
entity.Problem = problem;
entity.Data = new System.Data.Linq.Binary(new byte[PROBLEMDATA_SIZE]);
entity.DataType = GetDataType(okb, platform, "varbinary");
okb.ProblemDatas.InsertOnSubmit(entity);
}
private void CreateProblemParameter(OKBDataContext okb, Problem problem, Platform platform) {
ProblemParameter entity = new ProblemParameter();
entity.Name = Guid.NewGuid().ToString();
entity.Alias = Guid.NewGuid().ToString();
entity.Description = Guid.NewGuid().ToString();
entity.Problem = problem;
entity.DataType = GetDataType(okb, platform, sqlNames[random.Next(sqlNames.Length)]);
okb.ProblemParameters.InsertOnSubmit(entity);
}
private void CreateExperiment(OKBDataContext okb, Algorithm algorithm, Problem problem, Platform platform) {
Experiment entity = new Experiment();
entity.Algorithm = algorithm;
entity.Problem = problem;
foreach (AlgorithmParameter algorithmParameter in okb.AlgorithmParameters.Where(x => x.AlgorithmId == algorithm.Id)) {
CreateAlgorithmParameterValue(okb, algorithmParameter, entity, platform);
}
foreach (ProblemParameter problemParameter in okb.ProblemParameters.Where(x => x.ProblemId == problem.Id)) {
CreateProblemParameterValue(okb, problemParameter, entity, platform);
}
for (int i = 0; i < RUNS_PER_EXPERIMENT; i++) {
CreateRun(okb, entity, platform);
}
okb.Experiments.InsertOnSubmit(entity);
}
private void CreateAlgorithmParameterValue(OKBDataContext okb, AlgorithmParameter algorithmParameter, Experiment experiment, Platform platform) {
switch (algorithmParameter.DataType.SqlName) {
case "varbinary":
AlgorithmParameterBlobValue v1 = new AlgorithmParameterBlobValue();
v1.AlgorithmParameter = algorithmParameter;
v1.Experiment = experiment;
v1.Value = new Binary(new byte[BLOBVALUE_SIZE]);
v1.DataType = GetDataType(okb, platform, "varbinary");
okb.AlgorithmParameterBlobValues.InsertOnSubmit(v1);
return;
case "bit":
AlgorithmParameterBoolValue v2 = new AlgorithmParameterBoolValue();
v2.AlgorithmParameter = algorithmParameter;
v2.Experiment = experiment;
v2.Value = random.Next(2) == 0;
v2.DataType = GetDataType(okb, platform, "bit");
okb.AlgorithmParameterBoolValues.InsertOnSubmit(v2);
return;
case "float":
AlgorithmParameterFloatValue v3 = new AlgorithmParameterFloatValue();
v3.AlgorithmParameter = algorithmParameter;
v3.Experiment = experiment;
v3.Value = random.NextDouble();
v3.DataType = GetDataType(okb, platform, "float");
okb.AlgorithmParameterFloatValues.InsertOnSubmit(v3);
return;
case "bigint":
AlgorithmParameterIntValue v4 = new AlgorithmParameterIntValue();
v4.AlgorithmParameter = algorithmParameter;
v4.Experiment = experiment;
v4.Value = random.Next();
v4.DataType = GetDataType(okb, platform, "bigint");
okb.AlgorithmParameterIntValues.InsertOnSubmit(v4);
return;
case "nvarchar":
AlgorithmParameterStringValue v5 = new AlgorithmParameterStringValue();
v5.AlgorithmParameter = algorithmParameter;
v5.Experiment = experiment;
v5.Value = Guid.NewGuid().ToString();
v5.DataType = GetDataType(okb, platform, "nvarchar");
okb.AlgorithmParameterStringValues.InsertOnSubmit(v5);
return;
}
}
private void CreateProblemParameterValue(OKBDataContext okb, ProblemParameter problemParameter, Experiment experiment, Platform platform) {
switch (problemParameter.DataType.SqlName) {
case "varbinary":
ProblemParameterBlobValue v1 = new ProblemParameterBlobValue();
v1.ProblemParameter = problemParameter;
v1.Experiment = experiment;
v1.Value = new Binary(new byte[BLOBVALUE_SIZE]);
v1.DataType = GetDataType(okb, platform, "varbinary");
okb.ProblemParameterBlobValues.InsertOnSubmit(v1);
return;
case "bit":
ProblemParameterBoolValue v2 = new ProblemParameterBoolValue();
v2.ProblemParameter = problemParameter;
v2.Experiment = experiment;
v2.Value = random.Next(2) == 0;
v2.DataType = GetDataType(okb, platform, "bit");
okb.ProblemParameterBoolValues.InsertOnSubmit(v2);
return;
case "float":
ProblemParameterFloatValue v3 = new ProblemParameterFloatValue();
v3.ProblemParameter = problemParameter;
v3.Experiment = experiment;
v3.Value = random.NextDouble();
v3.DataType = GetDataType(okb, platform, "float");
okb.ProblemParameterFloatValues.InsertOnSubmit(v3);
return;
case "bigint":
ProblemParameterIntValue v4 = new ProblemParameterIntValue();
v4.ProblemParameter = problemParameter;
v4.Experiment = experiment;
v4.Value = random.Next();
v4.DataType = GetDataType(okb, platform, "bigint");
okb.ProblemParameterIntValues.InsertOnSubmit(v4);
return;
case "nvarchar":
ProblemParameterStringValue v5 = new ProblemParameterStringValue();
v5.ProblemParameter = problemParameter;
v5.Experiment = experiment;
v5.Value = Guid.NewGuid().ToString();
v5.DataType = GetDataType(okb, platform, "nvarchar");
okb.ProblemParameterStringValues.InsertOnSubmit(v5);
return;
}
}
private void CreateRun(OKBDataContext okb, Experiment experiment, Platform platform) {
Run entity = new Run();
entity.RandomSeed = random.Next();
entity.CreatedDate = DateTime.Now;
entity.UserId = Guid.NewGuid();
entity.ClientId = Guid.NewGuid();
entity.Experiment = experiment;
foreach (Result result in okb.Results.Where(x => x.AlgorithmId == experiment.AlgorithmId)) {
CreateResultValue(okb, result, entity, platform);
}
okb.Runs.InsertOnSubmit(entity);
}
private void CreateResultValue(OKBDataContext okb, Result result, Run run, Platform platform) {
switch (result.DataType.SqlName) {
case "varbinary":
ResultBlobValue v1 = new ResultBlobValue();
v1.Result = result;
v1.Run = run;
v1.Value = new Binary(new byte[BLOBVALUE_SIZE]);
v1.DataType = GetDataType(okb, platform, "varbinary");
okb.ResultBlobValues.InsertOnSubmit(v1);
return;
case "bit":
ResultBoolValue v2 = new ResultBoolValue();
v2.Result = result;
v2.Run = run;
v2.Value = random.Next(2) == 0;
v2.DataType = GetDataType(okb, platform, "bit");
okb.ResultBoolValues.InsertOnSubmit(v2);
return;
case "float":
ResultFloatValue v3 = new ResultFloatValue();
v3.Result = result;
v3.Run = run;
v3.Value = random.NextDouble();
v3.DataType = GetDataType(okb, platform, "float");
okb.ResultFloatValues.InsertOnSubmit(v3);
return;
case "bigint":
ResultIntValue v4 = new ResultIntValue();
v4.Result = result;
v4.Run = run;
v4.Value = random.Next();
v4.DataType = GetDataType(okb, platform, "bigint");
okb.ResultIntValues.InsertOnSubmit(v4);
return;
case "nvarchar":
ResultStringValue v5 = new ResultStringValue();
v5.Result = result;
v5.Run = run;
v5.Value = Guid.NewGuid().ToString();
v5.DataType = GetDataType(okb, platform, "nvarchar");
okb.ResultStringValues.InsertOnSubmit(v5);
return;
}
}
}
}