Permalink
Browse files

Tweaking model and tests

  • Loading branch information...
1 parent abbf86b commit 3c0238306a3ee8a332fc9ed058efbcf5d5e76175 @jchadwick jchadwick committed Dec 21, 2011
View
1 Core/DataAccess/DataContext.cs
@@ -5,6 +5,7 @@ namespace Ebuy.DataAccess
public partial class DataContext : DbContext
{
public DbSet<Auction> Auctions { get; set; }
+ public DbSet<Bid> Bids { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Review> Reviews { get; set; }
View
18 Core/Model/Auction.cs
@@ -42,17 +42,25 @@ public Auction()
}
- internal void PostBid(Bid bid)
+ public void PostBid(User user, Currency bidAmount)
{
- Contract.Requires(bid != null);
+ PostBid(user, bidAmount, DateTime.UtcNow);
+ }
+
+ protected internal void PostBid(User user, Currency bidAmount, DateTime timestamp)
+ {
+ Contract.Requires(user != null);
+ Contract.Requires(bidAmount != null);
+
+ var bid = new Bid(user, this, bidAmount, timestamp);
+
+ Bids.Add(bid);
// TODO: Support multiple currencies
- if (WinningBid == null || bid.Price.Amount > WinningBid.Price.Amount)
+ if (WinningBid == null || bidAmount.Amount > WinningBid.Price.Amount)
{
WinningBid = bid;
}
-
- Bids.Add(bid);
}
View
19 Core/Model/Bid.cs
@@ -5,6 +5,7 @@ namespace Ebuy
{
public class Bid
{
+ [Key]
public virtual Guid Id
{
get
@@ -18,7 +19,7 @@ public virtual Guid Id
}
private Guid? _id;
- public virtual Auction Auction { get; set; }
+ public virtual Auction Auction { get; private set; }
public virtual bool IsWinningBid
{
@@ -29,22 +30,30 @@ public virtual bool IsWinningBid
}
}
- public virtual Currency Price { get; set; }
+ public virtual Currency Price { get; private set; }
public virtual DateTime Timestamp { get; private set; }
- public virtual User User { get; set; }
+ public virtual User User { get; private set; }
- public Bid() : this(DateTime.UtcNow)
+ public Bid(User user, Auction auction, Currency price)
+ : this(user, auction, price, DateTime.UtcNow)
{
}
- protected internal Bid(DateTime timestamp)
+ protected internal Bid(User user, Auction auction, Currency price, DateTime timestamp)
{
+ User = user;
+ Auction = auction;
+ Price = price;
Timestamp = timestamp;
}
+ private Bid()
+ {
+ }
+
public class Metadata
{
[Required]
View
38 Core/Model/Currency.cs
@@ -1,3 +1,4 @@
+using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.Contracts;
@@ -6,7 +7,7 @@
namespace Ebuy
{
[ComplexType]
- public class Currency
+ public class Currency : IEquatable<Currency>
{
public static IDictionary<char, string> CurrencyCodesBySymbol = new Dictionary<char, string>() {
{ '', "EUR" },
@@ -39,15 +40,50 @@ public Currency(string currency)
}
+ 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;
+ }
+
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ if (ReferenceEquals(this, obj)) return true;
+ if (obj.GetType() != typeof (Currency)) return false;
+ return Equals((Currency) obj);
+ }
+
public override string ToString()
{
var symbol = CurrencyCodesBySymbol.Single(x => x.Value == Code).Key;
return string.Format("{0}{1}", symbol, Amount);
}
+ public static bool operator ==(Currency x, Currency y)
+ {
+ if(x == null || y == null) return false;
+ return x.Equals(y);
+ }
+
+ public static bool operator !=(Currency x, Currency y)
+ {
+ if (x == null || y == null) return true;
+ return !x.Equals(y);
+ }
+
public static implicit operator Currency(string currency)
{
return new Currency(currency);
}
+
+ public override int GetHashCode()
+ {
+ unchecked
+ {
+ return ((Code != null ? Code.GetHashCode() : 0)*397) ^ Amount.GetHashCode();
+ }
+ }
}
}
View
12 Core/Model/User.cs
@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.Contracts;
-using System.Linq;
using CustomExtensions.DataAnnotations;
namespace Ebuy
@@ -37,18 +36,17 @@ protected override string GenerateKey()
}
- public void Bid(Auction auction, Currency currency)
+ public void Bid(Auction auction, Currency bidAmount)
{
- Bid(auction, currency, DateTime.Now);
+ Bid(auction, bidAmount, DateTime.Now);
}
- protected internal virtual void Bid(Auction auction, Currency currency, DateTime timestamp)
+ protected internal virtual void Bid(Auction auction, Currency bidAmount, DateTime timestamp)
{
Contract.Requires(auction != null);
- Contract.Requires(currency != null);
+ Contract.Requires(bidAmount != null);
- var bid = new Bid(timestamp) { User = this, Price = currency };
- auction.PostBid(bid);
+ auction.PostBid(this, bidAmount, timestamp);
}
View
11 IntegrationTests/Core/DataAccess/AuctionRepositoryTests.cs
@@ -18,25 +18,24 @@ public void ShouldFindAuctionById()
AssertCanFindById<Auction>();
}
-// [TestMethod]
+ [TestMethod]
public void ShouldPersistBids()
{
var auction = CreateAndSaveNewEntity<Auction>();
var user1 = CreateAndSaveNewEntity<User>();
var user2 = CreateAndSaveNewEntity<User>();
- auction.PostBid(new Bid { Price = "$10", User = user1 });
- auction.PostBid(new Bid { Price = "$20", User = user2 });
- auction.PostBid(new Bid { Price = "$30", User = user1 });
+ auction.PostBid(user1, "$10");
+ auction.PostBid(user2, "$20");
+ auction.PostBid(user1, "$30");
DataContext.SaveChanges();
ExecuteInNewContext(context => {
var savedAuction = context.Auctions.Find(auction.Id);
- Assert.IsNotNull(savedAuction.WinningBid);
- Assert.AreEqual(30, savedAuction.WinningBid.Price.Amount);
Assert.AreEqual(3, savedAuction.Bids.Count);
+ Assert.AreEqual("$30", savedAuction.WinningBid.Price);
});
}
}
View
10 IntegrationTests/Core/DataAccess/DataContextTestFixture.cs
@@ -62,17 +62,19 @@ protected virtual TEntity CreateNewEntity<TEntity>()
protected virtual TEntity CreateAndSaveNewEntity<TEntity>()
where TEntity : class, IEntity
{
- var entity = TestDataGenerator.GenerateValid<TEntity>();
+ long entityId = 0;
ExecuteInNewContext(context => {
+ var entity = TestDataGenerator.GenerateValid<TEntity>();
context.Set<TEntity>().Add(entity);
context.SaveChanges();
+
+ entityId = entity.Id;
});
- // Make sure the Id was updated!
- Assert.AreNotEqual(0, entity.Id);
+ Assert.AreNotEqual(0, entityId);
- return entity;
+ return DataContext.Set<TEntity>().Find(entityId);
}
}
}

0 comments on commit 3c02383

Please sign in to comment.