Skip to content

Commit 54efae9

Browse files
committed
- tests passing
1 parent 451ceb7 commit 54efae9

15 files changed

Lines changed: 434 additions & 67 deletions
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Data;
3+
using System.Data.SqlClient;
4+
using Moq;
5+
using NUnit.Framework;
6+
7+
namespace Shuttle.Core.Data.Tests
8+
{
9+
[TestFixture]
10+
public class ProcedureQueryFixture : Fixture
11+
{
12+
[Test]
13+
public void Should_be_able_to_create_a_query()
14+
{
15+
const string sql = "uspDoSomething";
16+
17+
var query1 = new ProcedureQuery(sql);
18+
var query2 = ProcedureQuery.Create(sql);
19+
}
20+
21+
[Test]
22+
public void Should_be_able_prepare_a_query()
23+
{
24+
const string sql = "uspDoSomething";
25+
26+
var guid = Guid.NewGuid();
27+
var mc = new MappedColumn<Guid>("Id", DbType.Guid);
28+
var query = new ProcedureQuery(sql).AddParameterValue(mc, guid);
29+
var dataParameterCollection = new Mock<IDataParameterCollection>();
30+
31+
var command = new Mock<IDbCommand>();
32+
33+
dataParameterCollection.Setup(m => m.Add(It.IsAny<IDbDataParameter>())).Verifiable();
34+
35+
command.SetupGet(m => m.Parameters).Returns(dataParameterCollection.Object);
36+
command.SetupSet(m => m.CommandText = sql).Verifiable();
37+
command.SetupSet(m => m.CommandType = CommandType.StoredProcedure).Verifiable();
38+
command.Setup(m => m.CreateParameter()).Returns(new SqlParameter());
39+
40+
query.Prepare(command.Object);
41+
42+
command.VerifyAll();
43+
}
44+
}
45+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace Shuttle.Core.Data.Tests
4+
{
5+
public class BasicMapping
6+
{
7+
public Guid Id { get; set; }
8+
public string Name { get; set; }
9+
public int Age { get; set; }
10+
}
11+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
using System;
2+
using System.Linq;
3+
using NUnit.Framework;
4+
5+
namespace Shuttle.Core.Data.Tests
6+
{
7+
[TestFixture]
8+
public class QueryMapperFixture : Fixture
9+
{
10+
[SetUp]
11+
public void SetUp()
12+
{
13+
using (GetDatabaseContext())
14+
{
15+
new DatabaseGateway().ExecuteUsing(RawQuery.Create(@"
16+
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BasicMapping]') AND type in (N'U'))
17+
BEGIN
18+
CREATE TABLE [dbo].[BasicMapping](
19+
[Id] [uniqueidentifier] NOT NULL,
20+
[Name] [varchar](65) NOT NULL,
21+
[Age] [int] NOT NULL,
22+
CONSTRAINT [PK_BasicMapping] PRIMARY KEY CLUSTERED
23+
(
24+
[Id] ASC
25+
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
26+
) ON [PRIMARY]
27+
END
28+
29+
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[DF_BasicMapping_Id]') AND type = 'D')
30+
BEGIN
31+
ALTER TABLE [dbo].[BasicMapping] ADD CONSTRAINT [DF_BasicMapping_Id] DEFAULT (newid()) FOR [Id]
32+
END
33+
34+
delete from BasicMapping;
35+
36+
insert into BasicMapping
37+
(
38+
Id,
39+
Name,
40+
Age
41+
)
42+
values
43+
(
44+
'E09D96E1-5401-4CB6-A871-092DA1C7248D',
45+
'Name-1',
46+
25
47+
)
48+
49+
insert into BasicMapping
50+
(
51+
Id,
52+
Name,
53+
Age
54+
)
55+
values
56+
(
57+
'B5E0088E-4873-4244-9B91-1059E0383C3E',
58+
'Name-2',
59+
50
60+
)
61+
"));
62+
}
63+
}
64+
65+
[Test]
66+
public void Should_be_able_to_do_basic_mapping()
67+
{
68+
var mapper = new QueryMapper(new DatabaseGateway());
69+
70+
var queryRow = RawQuery.Create(@"
71+
select top 1
72+
Id,
73+
Name,
74+
Age
75+
from
76+
BasicMapping
77+
");
78+
79+
var queryRows = RawQuery.Create(@"
80+
select
81+
Id,
82+
Name,
83+
Age
84+
from
85+
BasicMapping
86+
");
87+
88+
using (GetDatabaseContext())
89+
{
90+
var item = mapper.MapObject<BasicMapping>(queryRow);
91+
var items = mapper.MapObjects<BasicMapping>(queryRows);
92+
93+
Assert.IsNotNull(item);
94+
Assert.AreEqual(2, items.Count());
95+
96+
var mappedRow = mapper.MapRow<BasicMapping>(queryRow);
97+
var mappedRows = mapper.MapRows<BasicMapping>(queryRows);
98+
99+
Assert.IsNotNull(mappedRow);
100+
Assert.AreEqual(2, mappedRows.Count());
101+
}
102+
}
103+
104+
[Test]
105+
public void Should_be_able_to_do_basic_mapping_even_though_columns_are_missing()
106+
{
107+
var mapper = new QueryMapper(new DatabaseGateway());
108+
109+
var queryRow = RawQuery.Create(@"
110+
select top 1
111+
Id,
112+
Name as NotMapped,
113+
Age as TheAge
114+
from
115+
BasicMapping
116+
");
117+
118+
var queryRows = RawQuery.Create(@"
119+
select
120+
Id,
121+
Name,
122+
Age
123+
from
124+
BasicMapping
125+
");
126+
127+
using (GetDatabaseContext())
128+
{
129+
var item = mapper.MapObject<BasicMapping>(queryRow);
130+
var items = mapper.MapObjects<BasicMapping>(queryRows);
131+
132+
Assert.IsNotNull(item);
133+
Assert.AreEqual(2, items.Count());
134+
135+
var mappedRow = mapper.MapRow<BasicMapping>(queryRow);
136+
var mappedRows = mapper.MapRows<BasicMapping>(queryRows);
137+
138+
Assert.IsNotNull(mappedRow);
139+
Assert.AreEqual(2, mappedRows.Count());
140+
}
141+
}
142+
143+
[Test]
144+
public void Should_be_able_to_do_value_mapping()
145+
{
146+
var mapper = new QueryMapper(new DatabaseGateway());
147+
148+
var queryRow = RawQuery.Create(@"
149+
select top 1
150+
Id
151+
from
152+
BasicMapping
153+
");
154+
155+
var queryRows = RawQuery.Create(@"
156+
select
157+
Id
158+
from
159+
BasicMapping
160+
");
161+
162+
using (GetDatabaseContext())
163+
{
164+
var value = mapper.MapValue<Guid>(queryRow);
165+
var values = mapper.MapValues<Guid>(queryRows);
166+
167+
Assert.IsNotNull(value);
168+
Assert.AreEqual(2, values.Count());
169+
}
170+
}
171+
}
172+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Data;
3+
using System.Data.SqlClient;
4+
using Moq;
5+
using NUnit.Framework;
6+
7+
namespace Shuttle.Core.Data.Tests
8+
{
9+
[TestFixture]
10+
public class RawQueryFixture : Fixture
11+
{
12+
[Test]
13+
public void Should_be_able_prepare_a_query()
14+
{
15+
const string sql = "select @Id";
16+
17+
var guid = Guid.NewGuid();
18+
var mc = new MappedColumn<Guid>("Id", DbType.Guid);
19+
var query = new RawQuery(sql).AddParameterValue(mc, guid);
20+
var dataParameterCollection = new Mock<IDataParameterCollection>();
21+
22+
var command = new Mock<IDbCommand>();
23+
24+
dataParameterCollection.Setup(m => m.Add(It.IsAny<IDbDataParameter>())).Verifiable();
25+
26+
command.SetupGet(m => m.Parameters).Returns(dataParameterCollection.Object);
27+
command.SetupSet(m => m.CommandText = sql).Verifiable();
28+
command.SetupSet(m => m.CommandType = CommandType.Text).Verifiable();
29+
command.Setup(m => m.CreateParameter()).Returns(new SqlParameter());
30+
31+
query.Prepare(command.Object);
32+
33+
command.VerifyAll();
34+
}
35+
36+
[Test]
37+
public void Should_be_able_to_create_a_query()
38+
{
39+
const string sql = "select 1";
40+
41+
var query1 = new RawQuery(sql);
42+
var query2 = RawQuery.Create(sql);
43+
var query3 = RawQuery.Create("select {0}", 1);
44+
}
45+
}
46+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Data;
3+
using System.IO;
4+
using Moq;
5+
using NUnit.Framework;
6+
7+
namespace Shuttle.Core.Data.Tests
8+
{
9+
public class ScriptProviderFixture : Fixture
10+
{
11+
[SetUp]
12+
public void SetupContext()
13+
{
14+
var cache = new Mock<IDatabaseContextCache>();
15+
16+
cache.Setup(m => m.Current).Returns(new Mock<IDatabaseContext>().Object);
17+
18+
DatabaseContext.Assign(cache.Object);
19+
}
20+
21+
[Test]
22+
public void Should_fail_when_there_is_no_ambient_database_context()
23+
{
24+
Assert.Throws<InvalidOperationException>(
25+
() => new ScriptProvider(new Mock<IScriptProviderConfiguration>().Object).Get("throw"));
26+
}
27+
28+
[Test]
29+
public void Should_eb_able_to_retrieve_script_from_file()
30+
{
31+
var provider = new ScriptProvider(new ScriptProviderConfiguration
32+
{
33+
FileNameFormat = "{ScriptName}.sql",
34+
ScriptFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @".\.scripts\")
35+
});
36+
37+
var script = provider.Get("file-script");
38+
39+
Assert.IsFalse(string.IsNullOrEmpty(script));
40+
Assert.AreEqual("select 'file-script'", script);
41+
}
42+
43+
[Test]
44+
public void Should_eb_able_to_retrieve_script_from_resource()
45+
{
46+
var provider = new ScriptProvider(new ScriptProviderConfiguration
47+
{
48+
ResourceAssembly = GetType().Assembly,
49+
ResourceNameFormat = "Shuttle.Core.Data.Tests..scripts.System.Data.SqlClient.{ScriptName}.sql"
50+
});
51+
52+
var script = provider.Get("embedded-script");
53+
54+
Assert.IsFalse(string.IsNullOrEmpty(script));
55+
Assert.AreEqual("select 'embedded-script'", script);
56+
}
57+
58+
[Test]
59+
public void Should_throw_exception_when_no_resource_or_file_found()
60+
{
61+
var provider = new ScriptProvider(new ScriptProviderConfiguration
62+
{
63+
ResourceAssembly = GetType().Assembly
64+
});
65+
66+
Assert.Throws<InvalidOperationException>(() => provider.Get("System.Data.SqlClient", "missing-script"));
67+
}
68+
}
69+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using Moq;
3+
using NUnit.Framework;
4+
5+
namespace Shuttle.Core.Data.Tests
6+
{
7+
public class ScriptProviderNegativeFixture : Fixture
8+
{
9+
[Test]
10+
public void Should_fail_when_there_is_no_ambient_database_context()
11+
{
12+
DatabaseContext.Assign(new Mock<IDatabaseContextCache>().Object);
13+
14+
Assert.Throws<InvalidOperationException>(
15+
() => new ScriptProvider(new Mock<IScriptProviderConfiguration>().Object).Get("throw"));
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)