Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 5da52b9

Browse files
committed
Add SqlServer Join test
1 parent c24a661 commit 5da52b9

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

src/ServiceStack.OrmLite.SqlServerTests/ServiceStack.OrmLite.SqlServerTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<Compile Include="TypeWithByteArrayFieldTests.cs" />
104104
<Compile Include="UnicodeTests.cs" />
105105
<Compile Include="UpdateTests.cs" />
106+
<Compile Include="UseCase\ComplexJoinWithLimitAndNoOrderByTests.cs" />
106107
<Compile Include="UseCase\TestEntityWithAliases.cs" />
107108
<Compile Include="UseCase\SimpleUseCase.cs" />
108109
<Compile Include="UseCase\TestEntity.cs" />
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.Data;
3+
using System.Linq;
4+
using NUnit.Framework;
5+
using ServiceStack.DataAnnotations;
6+
using ServiceStack.Model;
7+
using ServiceStack.Text;
8+
9+
namespace ServiceStack.OrmLite.SqlServerTests.UseCase
10+
{
11+
public class Bar : IHasGuidId
12+
{
13+
[PrimaryKey]
14+
[Alias("BarId")]
15+
public Guid Id { get; set; }
16+
17+
[Required]
18+
public string Name { get; set; }
19+
}
20+
21+
public class Foo : IHasIntId
22+
{
23+
[AutoIncrement]
24+
[PrimaryKey]
25+
public int Id { get; set; }
26+
27+
[Alias("FKBarId")]
28+
[ForeignKey(typeof(Bar), ForeignKeyName = "fk_Foo_Bar")]
29+
public Guid BarId { get; set; }
30+
}
31+
32+
internal class FooBarJoin
33+
{
34+
[BelongTo(typeof(Foo))]
35+
public int Id { get; set; }
36+
37+
[BelongTo(typeof(Bar))]
38+
public Guid BarId { get; set; }
39+
40+
[BelongTo(typeof(Bar))]
41+
public string Name { get; set; }
42+
}
43+
44+
[TestFixture]
45+
public class ComplexJoinWithLimitAndNoOrderByTests : OrmLiteTestBase
46+
{
47+
private static int _foo1Id;
48+
private static int _foo2Id;
49+
private static int _foo3Id;
50+
private static Guid _bar1Id;
51+
private static Guid _bar2Id;
52+
private static Guid _bar3Id;
53+
54+
private static void InitTables(IDbConnection db)
55+
{
56+
db.DropTable<Foo>();
57+
db.DropTable<Bar>();
58+
59+
db.CreateTable<Bar>();
60+
db.CreateTable<Foo>();
61+
62+
_bar1Id = new Guid("5bd67b84-bfdb-4057-9799-5e7a72a6eaa9");
63+
_bar2Id = new Guid("a8061d08-6816-4e1e-b3d7-1178abcefa0d");
64+
_bar3Id = new Guid("84BF769D-5BA9-4506-A7D2-5030E5595EDC");
65+
66+
db.Insert(new Bar { Id = _bar1Id, Name = "Banana", });
67+
db.Insert(new Bar { Id = _bar2Id, Name = "Orange", });
68+
db.Insert(new Bar { Id = _bar3Id, Name = "Apple", });
69+
70+
_foo1Id = (int)db.Insert(new Foo { BarId = _bar1Id, }, true);
71+
_foo2Id = (int)db.Insert(new Foo { BarId = _bar2Id, }, true);
72+
_foo3Id = (int)db.Insert(new Foo { BarId = _bar3Id, }, true);
73+
}
74+
75+
[Test]
76+
public void ComplexJoin_with_JoinSqlBuilder_and_limit_and_no_orderby()
77+
{
78+
using (var db = OpenDbConnection())
79+
{
80+
InitTables(db);
81+
82+
//JoinSqlBuilder is obsolete
83+
//var jn = new JoinSqlBuilder<FooBarJoin, Foo>()
84+
// .Join<Foo, Bar>(dp => dp.BarId, p => p.Id)
85+
// //.OrderBy<Foo>(f => f.Id) // Test fails without an explicity OrderBy because auto-generated OrderBy uses join table (FooBarJoin) name
86+
// .Limit(1, 2);
87+
88+
var jn = db.From<Foo>()
89+
.Join<Foo, Bar>((f,b) => f.BarId == b.Id)
90+
.Limit(1, 2);
91+
92+
var results = db.Select<FooBarJoin>(jn);
93+
db.GetLastSql().Print();
94+
95+
results.PrintDump();
96+
97+
var fooBarJoin = results.FirstOrDefault(x => x.BarId == _bar1Id);
98+
Assert.IsNull(fooBarJoin);
99+
fooBarJoin = results.First(x => x.BarId == _bar2Id);
100+
Assert.That(fooBarJoin.Id, Is.EqualTo(_foo2Id));
101+
fooBarJoin = results.First(x => x.BarId == _bar3Id);
102+
Assert.That(fooBarJoin.Id, Is.EqualTo(_foo3Id));
103+
}
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)