Created
January 3, 2012 19:10
-
-
Save marcusoftnet/1556391 to your computer and use it in GitHub Desktop.
I don't understand InMemoryAdapter.ConfigureJoin or Join doesn't work for InMemoryAdapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| using Simple.Data; | |
| using Xunit; | |
| using System.Collections.Generic; | |
| namespace AbbeSays.Tests | |
| { | |
| public class DemoTestsForJoins | |
| { | |
| /* MY DATABASE SCHEMA FOR Parents and Kids | |
| CREATE TABLE [dbo].[Parents]( | |
| [Id] [int] IDENTITY(1,1) NOT NULL, | |
| [Name] [varchar](200) NOT NULL, | |
| [Email] [varchar](200) NULL, | |
| [UserName] [varchar](200) NOT NULL, | |
| [Password] [varchar](200) NULL, | |
| CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED | |
| ( | |
| [Id] ASC | |
| )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | |
| ) ON [PRIMARY] | |
| GO | |
| SET ANSI_PADDING OFF | |
| GO | |
| * CREATE TABLE [dbo].[Kids]( | |
| [Id] [int] IDENTITY(1,1) NOT NULL, | |
| [ParentId] [int] NOT NULL, | |
| [Name] [varchar](200) NOT NULL, | |
| [BirthDate] [date] NOT NULL, | |
| [Bio] [text] NULL, | |
| [PictureURL] [varchar](300) NULL, | |
| CONSTRAINT [PK_Kids] PRIMARY KEY CLUSTERED | |
| ( | |
| [Id] ASC | |
| )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | |
| ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] | |
| GO | |
| SET ANSI_PADDING OFF | |
| GO | |
| * ALTER TABLE [dbo].[Kids] WITH CHECK ADD CONSTRAINT [FK_Kids_Users] FOREIGN KEY([ParentId]) | |
| REFERENCES [dbo].[Parents] ([Id]) | |
| GO | |
| ALTER TABLE [dbo].[Kids] CHECK CONSTRAINT [FK_Kids_Users] | |
| G0 | |
| */ | |
| [Fact] | |
| public void joining_against_sql_server_works_fine() | |
| { | |
| // Act | |
| var kids = GetKids(); | |
| // Assert | |
| Assert.Equal(3, kids.Count()); | |
| Assert.Equal(3, kids.Count(x => x.ParentName == "Marcus")); | |
| } | |
| [Fact] | |
| public void joining_against_memory_should_work() | |
| { | |
| // Arrange - setup inmemory database | |
| var inMemoryAdapter = new InMemoryAdapter(); | |
| inMemoryAdapter.ConfigureJoin("Parents", "Id", "Parents", "Kids", "ParentId", "Kids"); | |
| Database.UseMockAdapter(inMemoryAdapter); | |
| dynamic db = Database.Open(); | |
| db.Parents.Insert(Id: 1, Name: "Marcus"); | |
| /*// Sanity check | |
| Assert.Equal(1, db.Parents.FindByName("Marcus").Id); | |
| */ | |
| db.Kids.Insert(Id: 1, Name: "Albert", ParentId: 1); | |
| db.Kids.Insert(Id: 2, Name: "Arvid", ParentId: 1); | |
| db.Kids.Insert(Id: 3, Name: "Gustav", ParentId: 1); | |
| /*// Sanity check | |
| IEnumerable<dynamic> all = db.Kids.All().ToList(); | |
| Assert.Equal(3, all.Count(x => x.ParentId == 1)); | |
| */ | |
| /// Act | |
| var kids = GetKids(); | |
| // Assert | |
| Assert.Equal(3, kids.Count()); // It goes boom here with Count being 0 | |
| Assert.Equal(3, kids.Count(x => x.ParentName == "Marcus")); | |
| } | |
| private static IEnumerable<dynamic> GetKids() | |
| { | |
| var db = Database.Open(); | |
| return db.Kids.Query() | |
| .Join(db.Parents) | |
| .On(db.Kids.ParentId == db.Parents.Id) | |
| .Select(db.Kids.Name.As("KidName"), | |
| db.Parents.Name.As("ParentName")) | |
| .Where(db.Parents.Name == "Marcus") | |
| .ToList(); | |
| } | |
| } | |
| } |
Author
Yes - that is much better! It now returns 3 rows (as expected).
However both properties ("KidName" and "ParentName") is the same, but that might be that Select-issue I presume?
Yes thats the Select issue I mentioned - I've already committed a fix for the select issue ThatRendle/Simple.Data#136 so the next push to nuget will have the fix.
Author
Sweet - the Simple.Data train is running super fast to production :)
On Wed, Jan 4, 2012 at 9:48 AM, richardhopton < ***@***.*** > wrote:
Yes thats the Select issue I mentioned - I've already committed a fix for
the select issue ThatRendle/Simple.Data#136 so
the next push to nuget will have the fix.
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1556391
##
Best wishes
/Marcus Hammarberg
http://www.marcusoft.net/
http://twitter.com/marcusoftnet
marcusoft.net@gmail.com
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this work for SQL? It should work on the InMemoryAdapter but there is another issue hanging around with Select....
private static IEnumerable<dynamic> GetKids() { var db = Database.Open(); return db.Kids.Query() .Where(db.Kids.Parents.Name == "Marcus") .Select(db.Kids.Name.As("KidName"), db.Kids.Parents.Name.As("ParentName")) .ToList(); }