|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using NUnit.Framework; |
| 6 | +using Simple.Data.Mocking.Ado; |
| 7 | + |
| 8 | +namespace Simple.Data.IntegrationTest |
| 9 | +{ |
| 10 | + [TestFixture] |
| 11 | + public class QueryTest : DatabaseIntegrationContext |
| 12 | + { |
| 13 | + protected override void SetSchema(MockSchemaProvider schemaProvider) |
| 14 | + { |
| 15 | + schemaProvider.SetTables(new[] {"dbo", "Users", "BASE TABLE"}, |
| 16 | + new[] {"dbo", "UserBio", "BASE TABLE"}); |
| 17 | + |
| 18 | + schemaProvider.SetColumns(new object[] {"dbo", "Users", "Id", true}, |
| 19 | + new[] {"dbo", "Users", "Name"}, |
| 20 | + new[] {"dbo", "Users", "Password"}, |
| 21 | + new[] {"dbo", "Users", "Age"}, |
| 22 | + new[] {"dbo", "UserBio", "UserId"}, |
| 23 | + new[] {"dbo", "UserBio", "Text"}); |
| 24 | + |
| 25 | + schemaProvider.SetPrimaryKeys(new object[] { "dbo", "Users", "Id", 0 }); |
| 26 | + schemaProvider.SetForeignKeys(new object[] { "FK_Users_UserBio", "dbo", "UserBio", "UserId", "dbo", "Users", "Id", 0 }); |
| 27 | + } |
| 28 | + |
| 29 | + [Test] |
| 30 | + public void SpecifyingColumnsShouldRestrictSelect() |
| 31 | + { |
| 32 | + _db.Users.All() |
| 33 | + .Select(_db.Users.Name, _db.Users.Password) |
| 34 | + .ToList(); |
| 35 | + GeneratedSqlIs("select [dbo].[users].[name],[dbo].[users].[password] from [dbo].[users]"); |
| 36 | + } |
| 37 | + |
| 38 | + [Test] |
| 39 | + public void SpecifyingColumnsFromOtherTablesShouldAddJoin() |
| 40 | + { |
| 41 | + _db.Users.All() |
| 42 | + .Select(_db.Users.Name, _db.Users.Password, _db.Users.UserBio.Text) |
| 43 | + .ToList(); |
| 44 | + GeneratedSqlIs( |
| 45 | + "select [dbo].[users].[name],[dbo].[users].[password],[dbo].[userbio].[text] from [dbo].[users]" + |
| 46 | + " join [dbo].[userbio] on ([dbo].[users].[id] = [dbo].[userbio].[userid])"); |
| 47 | + } |
| 48 | + |
| 49 | + [Test] |
| 50 | + public void SpecifyingCountShouldSelectCount() |
| 51 | + { |
| 52 | + try |
| 53 | + { |
| 54 | + _db.Users.All().Count(); |
| 55 | + } |
| 56 | + catch (InvalidOperationException) |
| 57 | + { |
| 58 | + // This won't work on Mock provider, but the SQL should be generated OK |
| 59 | + } |
| 60 | + |
| 61 | + GeneratedSqlIs("select count(*) from [dbo].[users]"); |
| 62 | + } |
| 63 | + |
| 64 | + [Test] |
| 65 | + public void SpecifyingExistsShouldSelectCount() |
| 66 | + { |
| 67 | + try |
| 68 | + { |
| 69 | + _db.Users.All().Exists(); |
| 70 | + } |
| 71 | + catch (InvalidOperationException) |
| 72 | + { |
| 73 | + // This won't work on Mock provider, but the SQL should be generated OK |
| 74 | + } |
| 75 | + |
| 76 | + GeneratedSqlIs("select count(*) from [dbo].[users]"); |
| 77 | + } |
| 78 | + |
| 79 | + [Test] |
| 80 | + public void SpecifyingAnyShouldSelectCount() |
| 81 | + { |
| 82 | + try |
| 83 | + { |
| 84 | + _db.Users.All().Any(); |
| 85 | + } |
| 86 | + catch (InvalidOperationException) |
| 87 | + { |
| 88 | + // This won't work on Mock provider, but the SQL should be generated OK |
| 89 | + } |
| 90 | + |
| 91 | + GeneratedSqlIs("select count(*) from [dbo].[users]"); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments