Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tweaking model and tests

  • Loading branch information...
commit 50b82344d42f00423e0e8b03b6fc2920d48ff821 1 parent 65a9001
@jchadwick jchadwick authored
View
183 Core/DataAccess/DataContext.DemoDataInitializer.cs
@@ -0,0 +1,183 @@
+using System;
+using System.Collections.ObjectModel;
+using System.Linq;
+
+namespace Ebuy.DataAccess
+{
+ public partial class DataContext
+ {
+ public class DemoDataInitializer : Initializer
+ {
+ protected override void Seed(DataContext context)
+ {
+ base.Seed(context);
+
+ var frankSinatra = new User()
+ {
+ DisplayName = "Old Blue Eyes",
+ EmailAddress = "frank@theratpack.com",
+ FullName = "Frank Sinatra",
+ };
+ context.Users.Add(frankSinatra);
+
+ var freddieMercury = new User()
+ {
+ DisplayName = "Freddie Mercury",
+ EmailAddress = "freddie@queenband.com",
+ FullName = "Farrokh Bulsara",
+ };
+ context.Users.Add(freddieMercury);
+
+ var johnLennon = new User()
+ {
+ DisplayName = "The Reverend Fred Gherkin",
+ EmailAddress = "lenny@thebeatles.com",
+ FullName = "John Lennon",
+ };
+ context.Users.Add(johnLennon);
+
+
+ var videoGameSystems = context.Categories.Local.Single(x => x.Name == "Video Game Systems");
+ var sports = context.Categories.Local.Single(x => x.Name == "Sporting Goods");
+ var collectibles = context.Categories.Local.Single(x => x.Name == "Collectibles");
+
+
+ context.Products.Add(new Product
+ {
+ Categories = new[] { videoGameSystems },
+ Name = "Xbox 360 Elite",
+ Description = "The Xbox 360 Elite gaming system is the ultimate in gaming",
+ Images = new WebsiteImage[] { "~/Content/images/products/xbox360elite.jpg" },
+ Reviews = new[] {
+ new Review { User = frankSinatra, Description = "It's really awesome!", Rating = 4.5 },
+ new Review { User = johnLennon, Description = "It's quite horrible", Rating = 2 },
+ new Review { User = freddieMercury, Description = "I love mine so much I want another!", Rating = 4 },
+ }
+ });
+
+ context.Products.Add(new Product
+ {
+ Categories = new[] { videoGameSystems },
+ Name = "Sony PSP Go",
+ Description = "The smallest and mightiest PSP system yet.",
+ Images = new WebsiteImage[] { "~/Content/images/products/psp.jpg" },
+ Reviews = new[] {
+ new Review { User = frankSinatra, Description = "It's really awesome!", Rating = 4.5 },
+ new Review { User = johnLennon, Description = "It's quite horrible", Rating = 2 },
+ }
+ });
+
+ context.Products.Add(new Product
+ {
+ Categories = new[] { videoGameSystems },
+ Name = "Xbox 360 Kinect Sensor with Game Bundle",
+ Description = "You are the controller with Kinect for Xbox 360!",
+ Images = new WebsiteImage[] { "~/Content/images/products/kinect.jpg" },
+ Reviews = new[] {
+ new Review { User = frankSinatra, Description = "It's really awesome!", Rating = 4.5 },
+ new Review { User = johnLennon, Description = "It's quite horrible", Rating = 2 },
+ }
+ });
+
+ context.Products.Add(new Product
+ {
+ Categories = new[] { videoGameSystems },
+ Name = "Sony Playstation 3 120GB Slim Console",
+ Description = "The fourth generation of hardware released for the PlayStation 3 entertainment platform, the PlayStation 3 120GB system is the next stage in the evolution of Sony's console gaming powerhouse.",
+ Images = new WebsiteImage[] { "~/Content/images/products/ps3.jpg" },
+ Reviews = new[] {
+ new Review { User = frankSinatra, Description = "It's really awesome!", Rating = 4.5 },
+ new Review { User = johnLennon, Description = "It's quite horrible", Rating = 2 },
+ }
+ });
+
+ context.Products.Add(new Product
+ {
+ Categories = new[] { videoGameSystems },
+ Name = "Nintendo Wii Console Black",
+ Description = "Wii Sports Resort takes the inclusive, fun and intuitive controls of the original Wii Sports to the next level, introducing a whole new set of entertaining and physically immersive activities.",
+ Images = new WebsiteImage[] { "~/Content/images/products/wii.jpg" },
+ Reviews = new[] {
+ new Review { User = frankSinatra, Description = "It's really awesome!", Rating = 4.5 },
+ new Review { User = johnLennon, Description = "It's quite horrible", Rating = 2 },
+ }
+ });
+
+ context.Products.Add(new Product
+ {
+ Categories = new Collection<Category> { sports },
+ Name = "Burton Mayhem snow board",
+ Description = "Burton Mayhem snow board: 159cm wide",
+ Images = new WebsiteImage[] { "~/Content/images/products/burtonMayhem.jpg" },
+ Reviews = new[] {
+ new Review { User = frankSinatra, Description = "It's really awesome!", Rating = 4.5 },
+ new Review { User = johnLennon, Description = "It's quite horrible", Rating = 2 },
+ }
+ });
+
+ context.Products.Add(new Product
+ {
+ Categories = new Collection<Category> { collectibles },
+ Name = "Lock of John Lennon's hair",
+ Description = "Lock of John Lennon's hair",
+ Images = new WebsiteImage[] { "~/Content/images/products/lockOfHair.jpg" },
+ Reviews = new[] {
+ new Review { User = frankSinatra, Description = "It's really awesome!", Rating = 4.5 },
+ new Review { User = johnLennon, Description = "It's quite horrible", Rating = 2 },
+ }
+ });
+
+
+ var rand = new Random();
+ var users = context.Users.Local.ToArray();
+
+ foreach (var product in context.Products.Local)
+ {
+ var startTime =
+ DateTime.UtcNow
+ .AddDays(rand.Next(-10, -1))
+ .AddHours(rand.Next(1, 24))
+ .AddHours(rand.Next(1, 60));
+
+ var auction = new Auction()
+ {
+ Bids = new Collection<Bid>(),
+ Categories = product.Categories.ToArray(),
+ Description = product.Description,
+ EndTime = startTime.AddDays(rand.Next(3, 14)),
+ Images = product.Images.ToArray(),
+ Owner = users[rand.Next(users.Length)],
+ Product = product,
+ StartingPrice = "$1",
+ StartTime = startTime,
+ Title = product.Name,
+ };
+
+ context.Auctions.Add(auction);
+ }
+
+
+ foreach (var auction in context.Auctions.Local)
+ {
+ var nonOwners = users.Except(new[] { auction.Owner }).ToArray();
+ var lastBid = new Bid(auction.Owner, auction, auction.StartingPrice);
+
+ for (int x = 0; x < rand.Next(4, 20); x++)
+ {
+ var user = nonOwners[rand.Next(nonOwners.Length)];
+ var amount = new Currency(String.Format("${0}", lastBid.Amount.Value + rand.Next(1, 20)));
+
+ using (Clock.ModifiedTime(lastBid.Timestamp.AddMinutes(rand.Next(1, 1200))))
+ {
+ lastBid = new Bid(user, auction, amount);
+ }
+
+ context.Bids.Add(lastBid);
+ }
+
+ auction.WinningBidId = lastBid.Id;
+ }
+ }
+ }
+ }
+}
View
5 Core/DataAccess/DataContext.cs
@@ -1,4 +1,7 @@
-using System.Data.Entity;
+using System;
+using System.Collections.ObjectModel;
+using System.Data.Entity;
+using System.Linq;
namespace Ebuy.DataAccess
{
View
1  Core/Ebuy.Core.csproj
@@ -47,6 +47,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="DataAccess\DataContext.DemoDataInitializer.cs" />
<Compile Include="DataAccess\DataContext.cs" />
<Compile Include="DataAccess\IRepository.cs" />
<Compile Include="DataAccess\Repository.cs" />
View
5 Core/Model/Auction.cs
@@ -16,6 +16,8 @@ public class Auction : Entity
public virtual DateTime EndTime { get; set; }
public virtual Currency StartingPrice { get; set; }
+ [ForeignKey("WinndingBid")]
+ protected internal Guid? WinningBidId { get; set; }
public virtual Bid WinningBid { get; private set; }
public bool IsCompleted
@@ -34,6 +36,7 @@ public bool IsCompleted
public virtual User Owner { get; set; }
+
public Auction()
{
Bids = new Collection<Bid>();
@@ -52,7 +55,7 @@ public void PostBid(User user, Currency bidAmount)
Bids.Add(bid);
// TODO: Support multiple currencies
- if (WinningBid == null || bidAmount.Amount > WinningBid.Price.Amount)
+ if (WinningBid == null || bidAmount.Value > WinningBid.Amount.Value)
{
WinningBid = bid;
}
View
9 Core/Model/Bid.cs
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel.DataAnnotations;
+using System.Diagnostics.Contracts;
namespace Ebuy
{
@@ -29,7 +30,7 @@ public bool IsWinningBid
}
}
- public Currency Price { get; private set; }
+ public Currency Amount { get; private set; }
public DateTime Timestamp { get; private set; }
@@ -38,9 +39,13 @@ public bool IsWinningBid
public Bid(User user, Auction auction, Currency price)
{
+ Contract.Requires(user != null);
+ Contract.Requires(auction != null);
+ Contract.Requires(price != null);
+
User = user;
Auction = auction;
- Price = price;
+ Amount = price;
Timestamp = Clock.Now;
}
View
14 Core/Model/Currency.cs
@@ -17,13 +17,13 @@ public class Currency : IEquatable<Currency>
};
public string Code { get; private set; }
- public decimal Amount { get; private set; }
+ public decimal Value { get; private set; }
- public Currency(string code, decimal amount)
+ public Currency(string code, decimal value)
{
Code = code;
- Amount = amount;
+ Value = value;
}
public Currency(string currency)
@@ -32,7 +32,7 @@ public Currency(string currency)
Contract.Requires(currency.Length > 1);
Code = CurrencyCodesBySymbol[currency[0]];
- Amount = decimal.Parse(currency.Substring(1));
+ Value = decimal.Parse(currency.Substring(1));
}
private Currency()
@@ -44,7 +44,7 @@ public bool Equals(Currency other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
- return Equals(other.Code, Code) && other.Amount == Amount;
+ return Equals(other.Code, Code) && other.Value == Value;
}
public override bool Equals(object obj)
@@ -58,7 +58,7 @@ public override bool Equals(object obj)
public override string ToString()
{
var symbol = CurrencyCodesBySymbol.Single(x => x.Value == Code).Key;
- return string.Format("{0}{1}", symbol, Amount);
+ return string.Format("{0}{1}", symbol, Value);
}
public static bool operator ==(Currency x, Currency y)
@@ -82,7 +82,7 @@ public override int GetHashCode()
{
unchecked
{
- return ((Code != null ? Code.GetHashCode() : 0)*397) ^ Amount.GetHashCode();
+ return ((Code != null ? Code.GetHashCode() : 0)*397) ^ Value.GetHashCode();
}
}
}
View
31 Core/Util/Clock.cs
@@ -4,11 +4,38 @@ namespace Ebuy
{
public static class Clock
{
- internal static Func<DateTime> NowThunk = () => DateTime.UtcNow;
+ private static Func<DateTime> _nowThunk = () => DateTime.UtcNow;
public static DateTime Now
{
- get { return NowThunk(); }
+ get { return _nowThunk(); }
+ }
+
+
+ internal static ModifiedTimeLock ModifiedTime(DateTime time)
+ {
+ return new ModifiedTimeLock(() => time);
+ }
+
+ internal static ModifiedTimeLock ModifiedTime(Func<DateTime> time)
+ {
+ return new ModifiedTimeLock(time);
+ }
+
+ internal class ModifiedTimeLock : IDisposable
+ {
+ private readonly Func<DateTime> _previousThunk;
+
+ public ModifiedTimeLock(Func<DateTime> time)
+ {
+ _previousThunk = _nowThunk;
+ _nowThunk = time;
+ }
+
+ public void Dispose()
+ {
+ _nowThunk = _previousThunk;
+ }
}
}
}
View
4 IntegrationTests/Core/DataAccess/AuctionPersistenceTests.cs
@@ -35,11 +35,11 @@ public void ShouldPersistBids()
ExecuteInNewContext(context => {
var savedAuction = context.Auctions.Find(auction.Id);
Assert.AreEqual(3, savedAuction.Bids.Count);
- Assert.AreEqual("$30", savedAuction.WinningBid.Price);
+ Assert.AreEqual("$30", savedAuction.WinningBid.Amount);
var savedUser = context.Users.Find(user1.Id);
Assert.AreEqual(2, savedUser.Bids.Count);
- Assert.IsTrue(savedUser.Bids.OrderBy(x => x.Price.Amount).Last().IsWinningBid);
+ Assert.IsTrue(savedUser.Bids.OrderBy(x => x.Amount.Value).Last().IsWinningBid);
});
}
}
View
2  IntegrationTests/Core/DataAccess/RepositoryInitializer.cs
@@ -10,7 +10,7 @@ public class RepositoryInitializer
[AssemblyInitialize]
public static void InitializeDatabase(TestContext context)
{
- Database.SetInitializer(new DataContext.Initializer());
+ Database.SetInitializer(new DataContext.DemoDataInitializer());
}
}
}
View
4 IntegrationTests/IntegrationTests.csproj
@@ -90,6 +90,10 @@
<Project>{EFD6F198-A4E8-419E-ABBC-8573B29ACC42}</Project>
<Name>Ebuy.Core</Name>
</ProjectReference>
+ <ProjectReference Include="..\Website\Ebuy.Website.csproj">
+ <Project>{D739D3C5-5C15-4D36-B177-818DC676D57A}</Project>
+ <Name>Ebuy.Website</Name>
+ </ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
View
172 Website/App_Start/DataAccess.cs
@@ -1,7 +1,4 @@
-using System;
-using System.Collections.ObjectModel;
using System.Data.Entity;
-using System.Linq;
using Ebuy.DataAccess;
using Ebuy.Website.App_Start;
@@ -14,177 +11,10 @@ public static class DataAccess
public static void Start()
{
#if(DEBUG)
- Database.SetInitializer(new DemoDataContextInitializer());
+ Database.SetInitializer(new DataContext.DemoDataInitializer());
#else
Database.SetInitializer(new DataContext.Initializer());
#endif
}
-
- class DemoDataContextInitializer : DataContext.Initializer
- {
- protected override void Seed(DataContext context)
- {
- base.Seed(context);
-
- var frankSinatra = new User()
- {
- DisplayName = "Old Blue Eyes",
- EmailAddress = "frank@theratpack.com",
- FullName = "Frank Sinatra",
- };
- context.Users.Add(frankSinatra);
-
- var freddieMercury = new User()
- {
- DisplayName = "Freddie Mercury",
- EmailAddress = "freddie@queenband.com",
- FullName = "Farrokh Bulsara",
- };
- context.Users.Add(freddieMercury);
-
- var johnLennon = new User()
- {
- DisplayName = "The Reverend Fred Gherkin",
- EmailAddress = "lenny@thebeatles.com",
- FullName = "John Lennon",
- };
- context.Users.Add(johnLennon);
-
-
- var videoGameSystems = context.Categories.Local.Single(x => x.Name == "Video Game Systems");
- var sports = context.Categories.Local.Single(x => x.Name == "Sporting Goods");
- var collectibles = context.Categories.Local.Single(x => x.Name == "Collectibles");
-
-
- context.Products.Add(new Product
- {
- Categories = new[] { videoGameSystems },
- Name = "Xbox 360 Elite",
- Description = "The Xbox 360 Elite gaming system is the ultimate in gaming",
- Images = new WebsiteImage[] { "~/Content/images/products/xbox360elite.jpg" },
- Reviews = new[] {
- new Review { User = frankSinatra, Description = "It's really awesome!", Rating = 4.5 },
- new Review { User = johnLennon, Description = "It's quite horrible", Rating = 2 },
- new Review { User = freddieMercury, Description = "I love mine so much I want another!", Rating = 4 },
- }
- });
-
- context.Products.Add(new Product
- {
- Categories = new[] { videoGameSystems },
- Name = "Sony PSP Go",
- Description = "The smallest and mightiest PSP system yet.",
- Images = new WebsiteImage[] { "~/Content/images/products/psp.jpg" },
- Reviews = new[] {
- new Review { User = frankSinatra, Description = "It's really awesome!", Rating = 4.5 },
- new Review { User = johnLennon, Description = "It's quite horrible", Rating = 2 },
- }
- });
-
- context.Products.Add(new Product
- {
- Categories = new[] { videoGameSystems },
- Name = "Xbox 360 Kinect Sensor with Game Bundle",
- Description = "You are the controller with Kinect for Xbox 360!",
- Images = new WebsiteImage[] { "~/Content/images/products/kinect.jpg" },
- Reviews = new[] {
- new Review { User = frankSinatra, Description = "It's really awesome!", Rating = 4.5 },
- new Review { User = johnLennon, Description = "It's quite horrible", Rating = 2 },
- }
- });
-
- context.Products.Add(new Product
- {
- Categories = new[] { videoGameSystems },
- Name = "Sony Playstation 3 120GB Slim Console",
- Description = "The fourth generation of hardware released for the PlayStation 3 entertainment platform, the PlayStation 3 120GB system is the next stage in the evolution of Sony's console gaming powerhouse.",
- Images = new WebsiteImage[] { "~/Content/images/products/ps3.jpg" },
- Reviews = new[] {
- new Review { User = frankSinatra, Description = "It's really awesome!", Rating = 4.5 },
- new Review { User = johnLennon, Description = "It's quite horrible", Rating = 2 },
- }
- });
-
- context.Products.Add(new Product
- {
- Categories = new[] { videoGameSystems },
- Name = "Nintendo Wii Console Black",
- Description = "Wii Sports Resort takes the inclusive, fun and intuitive controls of the original Wii Sports to the next level, introducing a whole new set of entertaining and physically immersive activities.",
- Images = new WebsiteImage[] { "~/Content/images/products/wii.jpg" },
- Reviews = new[] {
- new Review { User = frankSinatra, Description = "It's really awesome!", Rating = 4.5 },
- new Review { User = johnLennon, Description = "It's quite horrible", Rating = 2 },
- }
- });
-
- context.Products.Add(new Product
- {
- Categories = new Collection<Category> { sports },
- Name = "Burton Mayhem snow board",
- Description = "Burton Mayhem snow board: 159cm wide",
- Images = new WebsiteImage[] { "~/Content/images/products/burtonMayhem.jpg" },
- Reviews = new[] {
- new Review { User = frankSinatra, Description = "It's really awesome!", Rating = 4.5 },
- new Review { User = johnLennon, Description = "It's quite horrible", Rating = 2 },
- }
- });
-
- context.Products.Add(new Product
- {
- Categories = new Collection<Category> { collectibles },
- Name = "Lock of John Lennon's hair",
- Description = "Lock of John Lennon's hair",
- Images = new WebsiteImage[] { "~/Content/images/products/lockOfHair.jpg" },
- Reviews = new[] {
- new Review { User = frankSinatra, Description = "It's really awesome!", Rating = 4.5 },
- new Review { User = johnLennon, Description = "It's quite horrible", Rating = 2 },
- }
- });
-
-
- var rand = new Random();
- var users = context.Users.Local.ToArray();
-
- foreach (var product in context.Products.Local)
- {
- var startTime =
- DateTime.UtcNow
- .AddDays(rand.Next(-2, 4))
- .AddHours(rand.Next(1, 24))
- .AddHours(rand.Next(1, 60));
-
- var auction = new Auction()
- {
- Bids = new Collection<Bid>(),
- Categories = product.Categories.ToArray(),
- Description = product.Description,
- EndTime = startTime.AddDays(rand.Next(3, 14)),
- Images = product.Images.ToArray(),
- Owner = users[rand.Next(users.Length)],
- Product = product,
- StartingPrice = "$1",
- StartTime = startTime,
- Title = product.Name,
- };
-
- context.Auctions.Add(auction);
- }
-
-/*
- foreach (var auction in context.Auctions)
- {
- var nonOwners = users.Except(new[] { auction.Owner }).ToArray();
- var lastBid = new Bid { Price = auction.StartingPrice, Timestamp = auction.StartTime };
-
- for (int x = 0; x < rand.Next(4, 20); x++)
- {
- var user = nonOwners[rand.Next(nonOwners.Length)];
- var amount = new Currency(string.Format("${0}", lastBid.Price.Amount + rand.Next(1, 20)));
- user.Bid(auction, amount);
- }
- }
-*/
- }
- }
}
}
View
2  Website/Models/AuctionViewModel.cs
@@ -99,7 +99,7 @@ public Currency WinningBidPrice
get
{
if (HasWinningBid)
- return WinningBid.Price;
+ return WinningBid.Amount;
return null;
}
Please sign in to comment.
Something went wrong with that request. Please try again.