Skip to content

Commit

Permalink
Creating datacontexts functional tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wagner Andrade committed Apr 7, 2013
1 parent 964c9f3 commit 2d3faef
Show file tree
Hide file tree
Showing 10 changed files with 388 additions and 1 deletion.
64 changes: 64 additions & 0 deletions Thunderstruck.Test/Functional/SqlEnvironment.cs
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Thunderstruck.Test.Functional
{
public class SqlEnvironment : IDisposable
{
public SqlEnvironment()
{
CreateDatabase();
CreateTables();
}

private void CreateDatabase()
{
using (var data = new DataContext("Master", Transaction.No))
{
data.Execute("CREATE DATABASE ThunderTest");
}
}

private void CreateTables()
{
var createManufacturerTable = new StringBuilder("CREATE TABLE Le_Manufacturer (")
.Append("[TheId] NUMERIC(8) NOT NULL IDENTITY PRIMARY KEY,")
.Append("[Name] VARCHAR(32) NOT NULL,")
.Append("[BuildYear] NUMERIC(4) NOT NULL)")
.ToString();

var createCarTable = new StringBuilder("CREATE TABLE Car (")
.Append("[Id] NUMERIC(8) NOT NULL IDENTITY PRIMARY KEY,")
.Append("[Name] VARCHAR(32),")
.Append("[ModelYear] NUMERIC(4) NOT NULL,")
.Append("[CreatedAt] DATETIME NOT NULL,")
.Append("[Chassis] VARCHAR(32) NOT NULL,")
.Append("[Mileage] FLOAT,")
.Append("[Category] NUMERIC(1) NOT NULL,")
.Append("[ManufacturerId] NUMERIC(8) REFERENCES Le_Manufacturer(TheId))")
.ToString();

using (var data = new DataContext())
{
data.Execute(createManufacturerTable);
data.Execute(createCarTable);
data.Commit();
}
}

private void DropDatabase()
{
using (var data = new DataContext("Master", Transaction.No))
{
data.Execute("DROP DATABASE ThunderTest");
}
}

public void Dispose()
{
DropDatabase();
}
}
}
214 changes: 214 additions & 0 deletions Thunderstruck.Test/Functional/SqlTest.cs
@@ -0,0 +1,214 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FluentAssertions;
using Thunderstruck.Test.Models;
using System.Linq;
using System.Data.SqlClient;

namespace Thunderstruck.Test.Functional
{
[TestClass]
public class SqlTest
{
private static SqlEnvironment environment;

[ClassInitialize]
public static void ClassInitialize(TestContext tests)
{
environment = new SqlEnvironment();
}

[TestMethod]
public void DataContext_Transitional_Should_Execute_A_Insert_SQL_Command_With_Anonymous_Object_Parameters_And_Read_Created_Data_In_Same_And_Count_Data_In_Another_Context()
{
using (var context = new DataContext())
{
var insertParameters = new { Name = "Lotus", Year = 1952 };
context.Execute("INSERT INTO Le_Manufacturer VALUES (@Name, @Year)", insertParameters);
var manufacturer = context.First<Manufacturer>("SELECT TOP 1 * FROM Le_Manufacturer");
context.Commit();

manufacturer.TheId.Should().BeGreaterThan(0);
manufacturer.Name.Should().Be("Lotus");
manufacturer.BuildYear.Should().Be(1952);
}

using (var context = new DataContext())
{
var manufacturerCount = context.GetValue<int>("SELECT COUNT(TheId) FROM Le_Manufacturer");

manufacturerCount.Should().Be(1);
}
}

[TestMethod]
public void DataContext_Non_Transitional_Should_Execute_A_Insert_SQL_Command_With_Object_Parameters_And_Read_Created_Data_In_Same_And_Count_Data()
{
using (var context = new DataContext(Transaction.No))
{
var insertParameters = new Manufacturer { Name = "General Motors", BuildYear = 1908 };
context.Execute("INSERT INTO Le_Manufacturer VALUES (@Name, @BuildYear)", insertParameters);
var manufacturer = context.First<Manufacturer>("SELECT * FROM Le_Manufacturer WHERE BuildYear = 1908");
var manufacturerCount = context.GetValue<int>("SELECT COUNT(TheId) FROM Le_Manufacturer");

manufacturer.TheId.Should().BeGreaterThan(0);
manufacturer.Name.Should().Be("General Motors");
manufacturer.BuildYear.Should().Be(1908);
manufacturerCount.Should().Be(2);
}
}

[TestMethod]
public void DataContext_Transitional_Should_Not_Commit_Data_If_Not_Explicit_Commit_Command_Call()
{
using (var context = new DataContext())
{
var insertParameters = new { Name = "Bentley", Year = 1919 };
context.Execute("INSERT INTO Le_Manufacturer VALUES (@Name, @Year)", insertParameters);
}

using (var context = new DataContext())
{
var manufacturers = context.All<Manufacturer>("SELECT * FROM Le_Manufacturer");

manufacturers.Count.Should().Be(2);
manufacturers.Should().Contain(m => m.Name == "Lotus");
manufacturers.Should().Contain(m => m.Name == "General Motors");
}
}

[TestMethod]
public void DataContext_Transitional_Should_Execute_A_SQL_And_Share_Transaction_With_A_Data_Reader()
{
using (var context = new DataContext())
{
var insertParameters = new { Name = "BMW", Year = 1916 };
context.Execute("INSERT INTO Le_Manufacturer VALUES (@Name, @Year)", insertParameters);
var selectParameters = new { Name = "BMW" };
var manufacturer = context.First<Manufacturer>("SELECT * FROM Le_Manufacturer WHERE Name = @Name", selectParameters);

manufacturer.Should().NotBeNull();
}

using (var context = new DataContext())
{
var selectParameters = new { Name = "BMW" };
var manufacturer = context.First<Manufacturer>("SELECT * FROM Le_Manufacturer WHERE Name = @Name", selectParameters);

manufacturer.Should().BeNull();
}
}

[TestMethod]
public void DataContext_Should_Read_Data_With_One_Field_And_Insert_A_Data_With_Some_Parameters_Types()
{
using (var context = new DataContext())
{
var selectParameters = new { Name = "Lotus" };
var manufacturer = context.First<Manufacturer>("SELECT TheId FROM Le_Manufacturer WHERE Name = @Name", selectParameters);

var insertParameters = new Car
{
Name = "Esprit Turbo",
ModelYear = 1981,
Mileage = 318.19850801,
ManufacturerId = manufacturer.TheId,
Category = CarCategory.Sport
};

context.Execute("INSERT INTO Car VALUES (@Name, @ModelYear, @CreatedAt, @Chassis, @Mileage, @Category, @ManufacturerId)", insertParameters);
var car = context.First<Car>("SELECT TOP 1 * FROM Car");
context.Commit();

car.Id.Should().BeGreaterThan(0);
car.Name.Should().Be("Esprit Turbo");
car.ModelYear.Should().Be(1981);
car.Mileage.Should().Be(318.19850801);
car.ManufacturerId.Should().Be(manufacturer.TheId);
car.CreatedAt.Date.Should().Be(DateTime.Today.Date);
car.Category.Should().Be(CarCategory.Sport);
}
}

[TestMethod]
public void DataContext_Should_Execute_A_Procedure()
{
using (var context = new DataContext())
{
var procedureParams = new { Status = "active" };
var whoResults = context.All<WhoResult>("EXEC sp_who @Status", procedureParams);

whoResults.Should().Contain(r => r.dbname == "ThunderTest" && r.cmd == "SELECT");
}
}

[TestMethod]
public void DataContext_Reader_Should_Translate_DBNull_To_Null_Value()
{
using (var context = new DataContext())
{
context.Execute("UPDATE Car SET Name = NULL WHERE Name = 'Esprit Turbo'");
var car = context.First<Car>("SELECT * FROM Car WHERE Name IS NULL");

car.Name.Should().BeNull();
}
}

[TestMethod]
[ExpectedException(typeof(SqlException))]
public void DataContext_Should_Throws_An_Exception_When_Execute_An_Incorrect_Command()
{
using (var context = new DataContext())
{
context.Execute("DELETE FROM Blargh");
}
}

[TestMethod]
public void DataContext_Reader_First_Should_Return_A_Null_Object_When_Query_Do_Not_Found_Items()
{
using (var context = new DataContext())
{
var car = context.First<Car>("SELECT * FROM Car WHERE Name = 'Fusca'");

car.Should().BeNull();
}
}

[TestMethod]
public void DataContext_Reader_All_Should_Return_An_Empty_Object_List_When_Query_Do_Not_Found_Items()
{
using (var context = new DataContext())
{
var cars = context.All<Car>("SELECT * FROM Car WHERE Name = 'Fusca'");

cars.Should().BeEmpty();
}
}

[TestMethod]
[ExpectedException(typeof(SqlException))]
public void DataContext_Should_Throws_An_Exeption_When_A_Constraint_Was_Broken()
{
using (var context = new DataContext())
{
var insertParameters = new Car
{
Name = "AGL 1113",
ModelYear = 1964,
Mileage = 1567922,
ManufacturerId = 98765, // Invalid ManufacturerId
Category = CarCategory.Transport
};

context.Execute("INSERT INTO Car VALUES (@Name, @ModelYear, @CreatedAt, @Chassis, @Mileage, @Category, @ManufacturerId)", insertParameters);
}
}

[ClassCleanup]
public static void ClassCleanup()
{
environment.Dispose();
}
}
}
37 changes: 37 additions & 0 deletions Thunderstruck.Test/Models/Car.cs
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Thunderstruck.Test.Models
{
public class Car
{
public Car()
{
CreatedAt = DateTime.Today;
}

public int Id { get; set; }

public string Name { get; set; }

public int ModelYear { get; set; }

public DateTime CreatedAt { get; set; }

public double? Mileage { get; set; }

public CarCategory Category { get; set; }

public string Chassis
{
get { return String.Concat(ManufacturerId, "_", Name).ToUpper(); }
}

public int ManufacturerId { get; set; }

[Ignore]
public HttpStyleUriParser MadIgnoredProperty { get; set; }
}
}
12 changes: 12 additions & 0 deletions Thunderstruck.Test/Models/CarCategory.cs
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Thunderstruck.Test.Models
{
public enum CarCategory
{
Normal, Sport, Transport
}
}
16 changes: 16 additions & 0 deletions Thunderstruck.Test/Models/Manufacturer.cs
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Thunderstruck.Test.Models
{
public class Manufacturer
{
public string Name { get; set; }

public int TheId { get; set; }

public int BuildYear { get; set; }
}
}
29 changes: 29 additions & 0 deletions Thunderstruck.Test/Models/WhoResult.cs
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Thunderstruck.Test.Models
{
public class WhoResult
{
public int spid { get; set; }

public int ecid { get; set; }

public string status { get; set; }

public string loginame { get; set; }

public string hostname { get; set; }

public int blk { get; set; }

public string dbname { get; set; }

public string cmd { get; set; }

public int request_id { get; set; }
}
}
6 changes: 6 additions & 0 deletions Thunderstruck.Test/Thunderstruck.Test.csproj
Expand Up @@ -62,6 +62,12 @@
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="Functional\SqlEnvironment.cs" />
<Compile Include="Functional\SqlTest.cs" />
<Compile Include="Models\Car.cs" />
<Compile Include="Models\CarCategory.cs" />
<Compile Include="Models\Manufacturer.cs" />
<Compile Include="Models\WhoResult.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Units\DataContextTest.cs" />
<Compile Include="Units\ProviderFactoryTest.cs" />
Expand Down

0 comments on commit 2d3faef

Please sign in to comment.