Skip to content

Commit 1ce712e

Browse files
committed
Initial commit. Alpha quality.
0 parents  commit 1ce712e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+3007
-0
lines changed

.nuget/NuGet.Config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<solution>
4+
<add key="disableSourceControlIntegration" value="true" />
5+
</solution>
6+
</configuration>

.nuget/NuGet.exe

760 KB
Binary file not shown.

.nuget/NuGet.targets

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir>
5+
6+
<!-- Enable the restore command to run before builds -->
7+
<RestorePackages Condition=" '$(RestorePackages)' == '' ">false</RestorePackages>
8+
9+
<!-- Property that enables building a package from a project -->
10+
<BuildPackage Condition=" '$(BuildPackage)' == '' ">false</BuildPackage>
11+
12+
<!-- Determines if package restore consent is required to restore packages -->
13+
<RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>
14+
15+
<!-- Download NuGet.exe if it does not already exist -->
16+
<DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">false</DownloadNuGetExe>
17+
</PropertyGroup>
18+
19+
<ItemGroup Condition=" '$(PackageSources)' == '' ">
20+
<!-- Package sources used to restore packages. By default, registered sources under %APPDATA%\NuGet\NuGet.Config will be used -->
21+
<!-- The official NuGet package source (https://www.nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
22+
<!--
23+
<PackageSource Include="https://www.nuget.org/api/v2/" />
24+
<PackageSource Include="https://my-nuget-source/nuget/" />
25+
-->
26+
</ItemGroup>
27+
28+
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
29+
<!-- Windows specific commands -->
30+
<NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
31+
</PropertyGroup>
32+
33+
<PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
34+
<!-- We need to launch nuget.exe with the mono command if we're not on windows -->
35+
<NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
36+
</PropertyGroup>
37+
38+
<PropertyGroup>
39+
<PackagesProjectConfig Condition=" '$(OS)' == 'Windows_NT'">$(MSBuildProjectDirectory)\packages.$(MSBuildProjectName.Replace(' ', '_')).config</PackagesProjectConfig>
40+
<PackagesProjectConfig Condition=" '$(OS)' != 'Windows_NT'">$(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config</PackagesProjectConfig>
41+
</PropertyGroup>
42+
43+
<PropertyGroup>
44+
<PackagesConfig Condition="Exists('$(MSBuildProjectDirectory)\packages.config')">$(MSBuildProjectDirectory)\packages.config</PackagesConfig>
45+
<PackagesConfig Condition="Exists('$(PackagesProjectConfig)')">$(PackagesProjectConfig)</PackagesConfig>
46+
</PropertyGroup>
47+
48+
<PropertyGroup>
49+
<!-- NuGet command -->
50+
<NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\NuGet.exe</NuGetExePath>
51+
<PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>
52+
53+
<NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
54+
<NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 "$(NuGetExePath)"</NuGetCommand>
55+
56+
<PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>
57+
58+
<RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
59+
<NonInteractiveSwitch Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' ">-NonInteractive</NonInteractiveSwitch>
60+
61+
<PaddedSolutionDir Condition=" '$(OS)' == 'Windows_NT'">"$(SolutionDir) "</PaddedSolutionDir>
62+
<PaddedSolutionDir Condition=" '$(OS)' != 'Windows_NT' ">"$(SolutionDir)"</PaddedSolutionDir>
63+
64+
<!-- Commands -->
65+
<RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)</RestoreCommand>
66+
<BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols</BuildCommand>
67+
68+
<!-- We need to ensure packages are restored prior to assembly resolve -->
69+
<BuildDependsOn Condition="$(RestorePackages) == 'true'">
70+
RestorePackages;
71+
$(BuildDependsOn);
72+
</BuildDependsOn>
73+
74+
<!-- Make the build depend on restore packages -->
75+
<BuildDependsOn Condition="$(BuildPackage) == 'true'">
76+
$(BuildDependsOn);
77+
BuildPackage;
78+
</BuildDependsOn>
79+
</PropertyGroup>
80+
81+
<Target Name="CheckPrerequisites">
82+
<!-- Raise an error if we're unable to locate nuget.exe -->
83+
<Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
84+
<!--
85+
Take advantage of MsBuild's build dependency tracking to make sure that we only ever download nuget.exe once.
86+
This effectively acts as a lock that makes sure that the download operation will only happen once and all
87+
parallel builds will have to wait for it to complete.
88+
-->
89+
<MsBuild Targets="_DownloadNuGet" Projects="$(MSBuildThisFileFullPath)" Properties="Configuration=NOT_IMPORTANT;DownloadNuGetExe=$(DownloadNuGetExe)" />
90+
</Target>
91+
92+
<Target Name="_DownloadNuGet">
93+
<DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
94+
</Target>
95+
96+
<Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
97+
<Exec Command="$(RestoreCommand)"
98+
Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />
99+
100+
<Exec Command="$(RestoreCommand)"
101+
LogStandardErrorAsError="true"
102+
Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
103+
</Target>
104+
105+
<Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
106+
<Exec Command="$(BuildCommand)"
107+
Condition=" '$(OS)' != 'Windows_NT' " />
108+
109+
<Exec Command="$(BuildCommand)"
110+
LogStandardErrorAsError="true"
111+
Condition=" '$(OS)' == 'Windows_NT' " />
112+
</Target>
113+
114+
<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
115+
<ParameterGroup>
116+
<OutputFilename ParameterType="System.String" Required="true" />
117+
</ParameterGroup>
118+
<Task>
119+
<Reference Include="System.Core" />
120+
<Using Namespace="System" />
121+
<Using Namespace="System.IO" />
122+
<Using Namespace="System.Net" />
123+
<Using Namespace="Microsoft.Build.Framework" />
124+
<Using Namespace="Microsoft.Build.Utilities" />
125+
<Code Type="Fragment" Language="cs">
126+
<![CDATA[
127+
try {
128+
OutputFilename = Path.GetFullPath(OutputFilename);
129+
130+
Log.LogMessage("Downloading latest version of NuGet.exe...");
131+
WebClient webClient = new WebClient();
132+
webClient.DownloadFile("https://www.nuget.org/nuget.exe", OutputFilename);
133+
134+
return true;
135+
}
136+
catch (Exception ex) {
137+
Log.LogErrorFromException(ex);
138+
return false;
139+
}
140+
]]>
141+
</Code>
142+
</Task>
143+
</UsingTask>
144+
</Project>
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<configSections>
4+
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
5+
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
6+
</configSections>
7+
<connectionStrings>
8+
<add name="TestEntities" providerName="System.Data.SqlClient" connectionString="data source=(localdb)\v11.0;initial catalog=JSONAPITestEntities;integrated security=True;multipleactiveresultsets=True;App=EntityFramework" />
9+
</connectionStrings>
10+
<startup>
11+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
12+
</startup>
13+
<runtime>
14+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
15+
<dependentAssembly>
16+
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
17+
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
18+
</dependentAssembly>
19+
</assemblyBinding>
20+
</runtime>
21+
<entityFramework>
22+
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
23+
<parameters>
24+
<parameter value="v11.0" />
25+
</parameters>
26+
</defaultConnectionFactory>
27+
<providers>
28+
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
29+
</providers>
30+
</entityFramework>
31+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"comments":[{"id":29,"body":"Nuh uh!"}]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"posts":[{"id": "{{GUID}}", "title": "Not at all linkbait!" } ]
2+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
using System;
2+
using System.Text;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using Newtonsoft.Json;
7+
using JSONAPI.Core;
8+
using JSONAPI.Json;
9+
using JSONAPI.EntityFramework;
10+
using JSONAPI.EntityFramework.Tests.Models;
11+
using System.IO;
12+
using System.Diagnostics;
13+
using System.Reflection;
14+
using System.Data;
15+
using System.Text.RegularExpressions;
16+
17+
namespace JSONAPI.EntityFramework.Tests
18+
{
19+
[TestClass]
20+
public class EntityConverterTests
21+
{
22+
private TestEntities context;
23+
private Author a, a2;
24+
private Post p, p2, p3;
25+
private Comment c, c2, c3, c4;
26+
27+
[TestInitialize]
28+
public void SetupEntities()
29+
{
30+
//- See http://stackoverflow.com/a/19130718/489116
31+
var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
32+
//-
33+
34+
context = new TestEntities();
35+
//JSONAPI.EntityFramework.Json.ContractResolver.ObjectContext = context;
36+
37+
38+
// Clear it out!
39+
foreach (Comment o in context.Comments) context.Comments.Remove(o);
40+
foreach (Post o in context.Posts) context.Posts.Remove(o);
41+
foreach (Author o in context.Authors) context.Authors.Remove(o);
42+
context.SaveChanges();
43+
44+
45+
a = new Author
46+
{
47+
Id = Guid.NewGuid().ToString(),
48+
Name = "Jason Hater"
49+
};
50+
context.Authors.Add(a);
51+
52+
p = new Post()
53+
{
54+
Title = "Linkbait!",
55+
Author = a
56+
};
57+
p2 = new Post
58+
{
59+
Title = "Rant #1023",
60+
Author = a
61+
};
62+
p3 = new Post
63+
{
64+
Title = "Polemic in E-flat minor #824",
65+
Author = a
66+
};
67+
68+
a.Posts.Add(p);
69+
a.Posts.Add(p2);
70+
a.Posts.Add(p3);
71+
72+
p.Comments.Add(
73+
c = new Comment()
74+
{
75+
Body = "Nuh uh!",
76+
Post = p
77+
}
78+
);
79+
p.Comments.Add(
80+
c2 = new Comment()
81+
{
82+
Body = "Yeah huh!",
83+
Post = p
84+
}
85+
);
86+
p.Comments.Add(
87+
c3 = new Comment()
88+
{
89+
Body = "Third Reich.",
90+
Post = p
91+
}
92+
);
93+
94+
p2.Comments.Add(
95+
c4 = new Comment
96+
{
97+
Body = "I laughed, I cried!",
98+
Post = p2
99+
}
100+
);
101+
102+
context.SaveChanges();
103+
}
104+
105+
[TestMethod]
106+
public void SerializeTest()
107+
{
108+
// Arrange
109+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter();
110+
formatter.PluralizationService = new JSONAPI.Core.PluralizationService();
111+
MemoryStream stream = new MemoryStream();
112+
113+
// Act
114+
formatter.WriteToStreamAsync(typeof(Post), p.Comments.First(), stream, (System.Net.Http.HttpContent)null, (System.Net.TransportContext)null);
115+
116+
// Assert
117+
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
118+
Trace.WriteLine(output);
119+
120+
}
121+
122+
[TestMethod]
123+
[DeploymentItem(@"Data\Post.json")]
124+
public void DeserializePostIntegrationTest()
125+
{
126+
// Arrange
127+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter();
128+
formatter.PluralizationService = new JSONAPI.Core.PluralizationService();
129+
MemoryStream stream = new MemoryStream();
130+
131+
EntityFrameworkMaterializer materializer = new EntityFrameworkMaterializer(context);
132+
133+
// Serialize a post and change the JSON... Not "unit" at all, I know, but a good integration test I think...
134+
formatter.WriteToStreamAsync(typeof(Post), p, stream, (System.Net.Http.HttpContent)null, (System.Net.TransportContext)null);
135+
string serializedPost = System.Text.Encoding.ASCII.GetString(stream.ToArray());
136+
// Change the post title (a scalar value)
137+
serializedPost = serializedPost.Replace("Linkbait!", "Not at all linkbait!");
138+
// Remove a comment (Note that order is undefined/not deterministic!)
139+
serializedPost = Regex.Replace(serializedPost, String.Format(@"(""comments""\s*:\s*\[[^]]*)(,""{0}""|""{0}"",)", c3.Id), @"$1");
140+
141+
// Reread the serialized JSON...
142+
stream.Dispose();
143+
stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(serializedPost));
144+
145+
// Act
146+
Post pUpdated;
147+
pUpdated = (Post)formatter.ReadFromStreamAsync(typeof(Post), stream, (System.Net.Http.HttpContent)null, (System.Net.Http.Formatting.IFormatterLogger)null).Result;
148+
pUpdated = materializer.MaterializeUpdate<Post>(pUpdated);
149+
150+
// Assert
151+
Assert.AreEqual(a, pUpdated.Author);
152+
Assert.AreEqual("Not at all linkbait!", pUpdated.Title);
153+
Assert.AreEqual(2, pUpdated.Comments.Count());
154+
Assert.IsFalse(pUpdated.Comments.Contains(c3));
155+
//Debug.WriteLine(sw.ToString());
156+
}
157+
158+
}
159+
}

0 commit comments

Comments
 (0)