Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jk mar4 #14

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,8 @@ ModelManifest.xml

# FAKE - F# Make
.fake/

.FakeBlogERD.vpp.lck
FakeBlogERD.vpp
FakeBlogERD.vpp.bak_000f
FakeBlogERD.vpp.bak_001d
55 changes: 46 additions & 9 deletions FakeBlog.Tests/App.config
Original file line number Diff line number Diff line change
@@ -1,14 +1,51 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<!--
Note: Add entries to the App.config file for configuration settings
that apply only to the Test project.
-->
<configuration>
<appSettings>

</appSettings>

<connectionStrings>

</connectionStrings>
</configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings></appSettings>
<connectionStrings></connectionStrings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
89 changes: 89 additions & 0 deletions FakeBlog.Tests/DAL/FakeBlogRepoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FakeBlog.DAL;
using FakeBlog.Models;
using System.Linq;
using Moq;
using System.Collections.Generic;
using System.Data.Entity;

namespace FakeBlog.Tests.DAL
{
[TestClass]
public class FakeBlogRepoTests
{
public Mock<FakeBlogContext> fake_context { get; set; }
public FakeBlogRepository repo { get; set; }
public Mock<DbSet<PublishedPost>> mock_posts_set { get; set; }
public IQueryable<PublishedPost> query_posts { get; set; }
public List<PublishedPost> fake_post_table { get; set; }
public ApplicationUser gwen { get; set; }
public ApplicationUser finn { get; set; }


[TestInitialize]
public void Setup()
{
fake_post_table = new List<PublishedPost>();
fake_context = new Mock<FakeBlogContext>();
mock_posts_set = new Mock<DbSet<PublishedPost>>();
repo = new FakeBlogRepository(fake_context.Object);

gwen = new ApplicationUser { Id = "gwen-id-1" };
finn = new ApplicationUser { Id = "finn-id-1" };
}

public void CreateFakeDatabase()
{
query_posts = fake_post_table.AsQueryable();

mock_posts_set.As<IQueryable<PublishedPost>>().Setup(p => p.Provider).Returns(query_posts.Provider);
mock_posts_set.As<IQueryable<PublishedPost>>().Setup(p => p.Expression).Returns(query_posts.Expression);
mock_posts_set.As<IQueryable<PublishedPost>>().Setup(p => p.ElementType).Returns(query_posts.ElementType);
mock_posts_set.As<IQueryable<PublishedPost>>().Setup(p => p.GetEnumerator()).Returns(query_posts.GetEnumerator());

mock_posts_set.Setup(b => b.Add(It.IsAny<PublishedPost>())).Callback((PublishedPost post) => fake_post_table.Add(post));
mock_posts_set.Setup(b => b.Remove(It.IsAny<PublishedPost>())).Callback((PublishedPost post) => fake_post_table.Remove(post));
fake_context.Setup(p => p.Posts).Returns(mock_posts_set.Object);

}

[TestMethod]
public void EnsureICanCreateInstanceOfRepo()
{
FakeBlogRepository repo = new FakeBlogRepository();

Assert.IsNotNull(repo);
}

[TestMethod]
public void EnsureIHaveNotNullContext()
{
FakeBlogRepository repo = new FakeBlogRepository();

Assert.IsNotNull(repo.Context);
}

[TestMethod]
public void EnsureICanInjectContextInstance()
{
FakeBlogContext context = new FakeBlogContext();
FakeBlogRepository repo = new FakeBlogRepository(context);

Assert.IsNotNull(repo.Context);
}

[TestMethod]
public void EnsureICanAddPublishedPost()
{
CreateFakeDatabase();

ApplicationUser a_user = new ApplicationUser();
{
Id = 1,
UserName = "Sammy",
Email = "[email protected]"
};
}
}
}
30 changes: 29 additions & 1 deletion FakeBlog.Tests/FakeBlog.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,34 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\packages\Castle.Core.4.0.0\lib\net45\Castle.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.AspNet.Identity.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.Identity.Core.2.2.1\lib\net45\Microsoft.AspNet.Identity.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.AspNet.Identity.EntityFramework, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.Identity.EntityFramework.2.2.1\lib\net45\Microsoft.AspNet.Identity.EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Moq, Version=4.7.1.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.7.1\lib\net45\Moq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
Expand Down Expand Up @@ -82,6 +108,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="DAL\FakeBlogRepoTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Controllers\HomeControllerTest.cs" />
</ItemGroup>
Expand All @@ -97,6 +124,7 @@
<Name>FakeBlog</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
5 changes: 5 additions & 0 deletions FakeBlog.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="4.0.0" targetFramework="net461" />
<package id="EntityFramework" version="6.1.3" targetFramework="net461" />
<package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net461" />
<package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.1" targetFramework="net461" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net461" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
<package id="Moq" version="4.7.1" targetFramework="net461" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net461" />
</packages>
Binary file added FakeBlog.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions FakeBlog/DAL/FakeBlogContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using FakeBlog.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace FakeBlog.DAL
{
public class FakeBlogContext : ApplicationDbContext
{
public virtual DbSet<PublishedPost> Posts { get; set; }
}
}
75 changes: 75 additions & 0 deletions FakeBlog/DAL/FakeBlogRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FakeBlog.Models;

namespace FakeBlog.DAL
{
public class FakeBlogRepository : IRepository
{
public FakeBlogContext Context { get; set; }

public FakeBlogRepository()
{
Context = new FakeBlogContext();
}

public FakeBlogRepository(FakeBlogContext context)
{
Context = context;
}

private List<PublishedPost> fake_post_table;

public void AddPublishedPost(string title, ApplicationUser owner)
{
PublishedPost post = new PublishedPost { Title = title, Owner = owner};
Context.Posts.Add(post);
Context.SaveChanges();
}

public bool AttachAuthor(int authorId, int postId)
{
throw new NotImplementedException();
}

public PublishedPost GetPublishedPost(int postId)
{
PublishedPost found_Post = Context.Posts.FirstOrDefault(p => p.PublishedPostId == postId);
return found_Post;
}

public List<PublishedPost> GetPublishedPostFromAuthor(string userId)
{
return Context.Posts.Where(p => p.Owner.Id == userId).ToList();
}

public bool IsDraft(int postId)
{
throw new NotImplementedException();
}

public bool PostDraft(int postId)
{
throw new NotImplementedException();
}

public bool RomovePublishedPost(int postId)
{
PublishedPost found_Post = GetPublishedPost(postId);
if (found_Post != null)
{
Context.Posts.Remove(found_Post);
Context.SaveChanges();
return true;
}
return false;
}

public List<PublishedPost> GetPublishedPostFromAuthor(int authorId)
{
throw new NotImplementedException();
}
}
}
29 changes: 29 additions & 0 deletions FakeBlog/DAL/IRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using FakeBlog.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FakeBlog.DAL
{
public interface IRepository
{
//Create
void AddPublishedPost(string title, ApplicationUser owner);

//Read
List<PublishedPost> GetPublishedPostFromAuthor(int authorId);

PublishedPost GetPublishedPost(int postId);

//Update
bool AttachAuthor(int authorId, int postId);

bool IsDraft(int postId);

bool PostDraft(int postId);

//Delete
bool RomovePublishedPost(int postId);
}
}
Loading