Skip to content

Commit

Permalink
Fixes broken unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mgesing committed Feb 8, 2019
1 parent 404b6cf commit f5b62c3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 106 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* Added options for alternating price display (in badges).
* #1515 Poll: Add result tab with a list of answers and customers for a poll
* BMEcat: Added export and import of product tags.
* Santander instalment purchase.

### Improvements
* (Perf) Significantly increased query performance for products with a lot of category assignments (> 10).
Expand Down
22 changes: 5 additions & 17 deletions src/Libraries/SmartStore.Services/Discounts/DiscountService.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SmartStore.Collections;
using SmartStore.Core;
using SmartStore.Core.Caching;
using SmartStore.Core.Data;
using SmartStore.Core.Domain.Customers;
using SmartStore.Core.Domain.Discounts;
using SmartStore.Core.Events;
using SmartStore.Core.Plugins;
using SmartStore.Core.Domain.Orders;
using SmartStore.Services.Customers;
using SmartStore.Core.Plugins;
using SmartStore.Services.Common;
using SmartStore.Services.Configuration;
using SmartStore.Collections;
using SmartStore.Core.Data.Hooks;
using SmartStore.Services.Customers;

namespace SmartStore.Services.Discounts
{
Expand All @@ -28,21 +25,16 @@ public partial class DiscountService : IDiscountService
private readonly IRequestCache _requestCache;
private readonly IStoreContext _storeContext;
private readonly IGenericAttributeService _genericAttributeService;
private readonly IPluginFinder _pluginFinder;
private readonly IEventPublisher _eventPublisher;
private readonly ISettingService _settingService;
private readonly IProviderManager _providerManager;
private readonly IDictionary<DiscountKey, bool> _discountValidityCache;

public DiscountService(IRequestCache requestCache,
public DiscountService(
IRequestCache requestCache,
IRepository<Discount> discountRepository,
IRepository<DiscountRequirement> discountRequirementRepository,
IRepository<DiscountUsageHistory> discountUsageHistoryRepository,
IStoreContext storeContext,
IGenericAttributeService genericAttributeService,
IPluginFinder pluginFinder,
IEventPublisher eventPublisher,
ISettingService settingService,
IProviderManager providerManager)
{
_requestCache = requestCache;
Expand All @@ -51,9 +43,6 @@ public DiscountService(IRequestCache requestCache,
_discountUsageHistoryRepository = discountUsageHistoryRepository;
_storeContext = storeContext;
_genericAttributeService = genericAttributeService;
_pluginFinder = pluginFinder;
_eventPublisher = eventPublisher;
_settingService = settingService;
_providerManager = providerManager;
_discountValidityCache = new Dictionary<DiscountKey, bool>();
}
Expand Down Expand Up @@ -207,7 +196,6 @@ public virtual Discount GetDiscountByCouponCode(string couponCode, bool showHidd
return discount;
}

private System.Diagnostics.Stopwatch _watch = new System.Diagnostics.Stopwatch();
public virtual bool IsDiscountValid(Discount discount, Customer customer)
{
var couponCodeToValidate = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,16 +431,19 @@ public AutoMapperAdminProfile()
CreateMap<NewsLetterSubscriptionModel, NewsLetterSubscription>()
.ForMember(dest => dest.CreatedOnUtc, mo => mo.Ignore())
.ForMember(dest => dest.NewsLetterSubscriptionGuid, mo => mo.Ignore())
.ForMember(dest => dest.StoreId, mo => mo.Ignore());
//forums
CreateMap<ForumGroup, ForumGroupModel>()
.ForMember(dest => dest.Locales, mo => mo.Ignore())
.ForMember(dest => dest.SeName, mo => mo.MapFrom(src => src.GetSeName(0, true, false)))
.ForMember(dest => dest.AvailableStores, mo => mo.Ignore())
.ForMember(dest => dest.SelectedStoreIds, mo => mo.Ignore())
.ForMember(dest => dest.CreatedOn, mo => mo.Ignore())
.ForMember(dest => dest.ForumModels, mo => mo.Ignore());
CreateMap<ForumGroupModel, ForumGroup>()
.ForMember(dest => dest.StoreId, mo => mo.Ignore())
.ForMember(dest => dest.WorkingLanguageId, mo => mo.Ignore());
//forums
CreateMap<ForumGroup, ForumGroupModel>()
.ForMember(dest => dest.Locales, mo => mo.Ignore())
.ForMember(dest => dest.SeName, mo => mo.MapFrom(src => src.GetSeName(0, true, false)))
.ForMember(dest => dest.AvailableStores, mo => mo.Ignore())
.ForMember(dest => dest.SelectedStoreIds, mo => mo.Ignore())
.ForMember(dest => dest.AvailableCustomerRoles, mo => mo.Ignore())
.ForMember(dest => dest.SelectedCustomerRoleIds, mo => mo.Ignore())
.ForMember(dest => dest.CreatedOn, mo => mo.Ignore())
.ForMember(dest => dest.ForumModels, mo => mo.Ignore());
CreateMap<ForumGroupModel, ForumGroup>()
.ForMember(dest => dest.CreatedOnUtc, mo => mo.Ignore())
.ForMember(dest => dest.UpdatedOnUtc, mo => mo.Ignore())
.ForMember(dest => dest.Forums, mo => mo.Ignore());
Expand Down Expand Up @@ -499,6 +502,7 @@ public AutoMapperAdminProfile()
.ForMember(dest => dest.EndDate, mo => mo.Ignore())
.ForMember(dest => dest.AvailableStores, mo => mo.Ignore())
.ForMember(dest => dest.SelectedStoreIds, mo => mo.Ignore())
.ForMember(dest => dest.AvailableLanguages, mo => mo.Ignore())
.ForMember(dest => dest.UsernamesEnabled, mo => mo.Ignore())
.ForMember(dest => dest.GridPageSize, mo => mo.Ignore());
CreateMap<PollModel, Poll>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Rhino.Mocks;
using SmartStore.Core;
using SmartStore.Core.Caching;
using SmartStore.Core.Data;
using SmartStore.Core.Domain.Customers;
using SmartStore.Core.Domain.Discounts;
using SmartStore.Core.Plugins;
using SmartStore.Core.Domain.Stores;
using SmartStore.Services.Common;
using SmartStore.Services.Discounts;
using SmartStore.Core.Events;
using SmartStore.Tests;
using NUnit.Framework;
using Rhino.Mocks;
using SmartStore.Core;
using SmartStore.Services.Common;
using SmartStore.Core.Domain.Common;
using SmartStore.Services.Configuration;
using SmartStore.Core.Domain.Stores;

namespace SmartStore.Services.Tests.Discounts
{
Expand All @@ -25,11 +21,9 @@ public class DiscountServiceTests : ServiceTest
IRepository<Discount> _discountRepo;
IRepository<DiscountRequirement> _discountRequirementRepo;
IRepository<DiscountUsageHistory> _discountUsageHistoryRepo;
IEventPublisher _eventPublisher;
IGenericAttributeService _genericAttributeService;
IDiscountService _discountService;
IStoreContext _storeContext;
ISettingService _settingService;

[SetUp]
public new void SetUp()
Expand Down Expand Up @@ -62,27 +56,19 @@ public class DiscountServiceTests : ServiceTest

_discountRepo.Expect(x => x.Table).Return(new List<Discount>() { discount1, discount2 }.AsQueryable());

_eventPublisher = MockRepository.GenerateMock<IEventPublisher>();
_eventPublisher.Expect(x => x.Publish(Arg<object>.Is.Anything));

_storeContext = MockRepository.GenerateMock<IStoreContext>();
_storeContext.Expect(x => x.CurrentStore).Return(new Store
{
Id = 1,
Name = "MyStore"
});

_settingService = MockRepository.GenerateMock<ISettingService>();

var cacheManager = new NullCache();
_discountRequirementRepo = MockRepository.GenerateMock<IRepository<DiscountRequirement>>();
_discountUsageHistoryRepo = MockRepository.GenerateMock<IRepository<DiscountUsageHistory>>();
var pluginFinder = new PluginFinder();
_genericAttributeService = MockRepository.GenerateMock<IGenericAttributeService>();

_discountService = new DiscountService(NullRequestCache.Instance, _discountRepo, _discountRequirementRepo,
_discountUsageHistoryRepo, _storeContext, _genericAttributeService, pluginFinder, _eventPublisher,
_settingService, base.ProviderManager);
_discountUsageHistoryRepo, _storeContext, _genericAttributeService, ProviderManager);
}

[Test]
Expand Down Expand Up @@ -133,17 +119,8 @@ public void Should_accept_valid_discount_code()
LastActivityDateUtc = new DateTime(2010, 01, 02)
};

_genericAttributeService.Expect(x => x.GetAttributesForEntity(customer.Id, "Customer"))
.Return(new List<GenericAttribute>()
{
new GenericAttribute()
{
EntityId = customer.Id,
Key = SystemCustomerAttributeNames.DiscountCouponCode,
KeyGroup = "Customer",
Value = "CouponCode 1"
}
});
_genericAttributeService.Expect(x => x.GetAttribute<string>(nameof(Customer), customer.Id, SystemCustomerAttributeNames.DiscountCouponCode, 0))
.Return("CouponCode 1");

var result1 = _discountService.IsDiscountValid(discount, customer);
result1.ShouldEqual(true);
Expand Down Expand Up @@ -174,29 +151,30 @@ public void Should_not_accept_wrong_discount_code()
LastActivityDateUtc = new DateTime(2010, 01, 02)
};

_genericAttributeService.Expect(x => x.GetAttributesForEntity(customer.Id, "Customer"))
.Return(new List<GenericAttribute>()
{
new GenericAttribute()
{
EntityId = customer.Id,
Key = SystemCustomerAttributeNames.DiscountCouponCode,
KeyGroup = "Customer",
Value = "CouponCode 2"
}
});

var result2 = _discountService.IsDiscountValid(discount, customer);
_genericAttributeService.Expect(x => x.GetAttribute<string>(nameof(Customer), customer.Id, SystemCustomerAttributeNames.DiscountCouponCode, 0))
.Return("CouponCode 2");

var result2 = _discountService.IsDiscountValid(discount, customer);
result2.ShouldEqual(false);
}

[Test]
public void Can_validate_discount_dateRange()
{
var discount = new Discount
var customer = new Customer
{
CustomerGuid = Guid.NewGuid(),
AdminComment = "",
Active = true,
Deleted = false,
CreatedOnUtc = new DateTime(2010, 01, 01),
LastActivityDateUtc = new DateTime(2010, 01, 02)
};

var discount1 = new Discount
{
DiscountType = DiscountType.AssignedToSkus,
Name = "Discount 2",
Name = "Discount 1",
UsePercentage = false,
DiscountPercentage = 0,
DiscountAmount = 5,
Expand All @@ -206,21 +184,23 @@ public void Can_validate_discount_dateRange()
DiscountLimitation = DiscountLimitationType.Unlimited,
};

var customer = new Customer
var discount2 = new Discount
{
CustomerGuid = Guid.NewGuid(),
AdminComment = "",
Active = true,
Deleted = false,
CreatedOnUtc = new DateTime(2010, 01, 01),
LastActivityDateUtc = new DateTime(2010, 01, 02)
DiscountType = DiscountType.AssignedToSkus,
Name = "Discount 2",
UsePercentage = false,
DiscountPercentage = 0,
DiscountAmount = 5,
StartDateUtc = DateTime.UtcNow.AddDays(1),
EndDateUtc = DateTime.UtcNow.AddDays(2),
RequiresCouponCode = false,
DiscountLimitation = DiscountLimitationType.Unlimited,
};

var result1 = _discountService.IsDiscountValid(discount, customer);
var result1 = _discountService.IsDiscountValid(discount1, customer);
result1.ShouldEqual(true);

discount.StartDateUtc = DateTime.UtcNow.AddDays(1);
var result2 = _discountService.IsDiscountValid(discount, customer);
var result2 = _discountService.IsDiscountValid(discount2, customer);
result2.ShouldEqual(false);
}
}
Expand Down
35 changes: 12 additions & 23 deletions src/Tests/SmartStore.Services.Tests/Helpers/DateTimeHelperTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks;
using SmartStore.Core;
using SmartStore.Core.Domain.Common;
using SmartStore.Core.Domain.Customers;
using SmartStore.Core.Domain.Stores;
using SmartStore.Core.Domain.Common;
using SmartStore.Services.Common;
using SmartStore.Services.Configuration;
using SmartStore.Services.Customers;
using SmartStore.Services.Helpers;
using SmartStore.Services.Common;
using SmartStore.Tests;
using NUnit.Framework;
using Rhino.Mocks;

namespace SmartStore.Services.Tests.Helpers
{
Expand All @@ -33,18 +32,17 @@ public class DateTimeHelperTests : ServiceTest

_workContext = MockRepository.GenerateMock<IWorkContext>();

_store = new Store() { Id = 1 };
_store = new Store { Id = 1 };
_storeContext = MockRepository.GenerateMock<IStoreContext>();
_storeContext.Expect(x => x.CurrentStore).Return(_store);

_dateTimeSettings = new DateTimeSettings()
_dateTimeSettings = new DateTimeSettings
{
AllowCustomersToSetTimeZone = false,
DefaultStoreTimeZoneId = ""
};

_dateTimeHelper = new DateTimeHelper(_workContext, _genericAttributeService,
_settingService, _dateTimeSettings);
_dateTimeHelper = new DateTimeHelper(_workContext, _genericAttributeService, _settingService, _dateTimeSettings);
}

[Test]
Expand All @@ -67,25 +65,16 @@ public void Can_get_all_systemTimeZones()
public void Can_get_customer_timeZone_with_customTimeZones_enabled()
{
_dateTimeSettings.AllowCustomersToSetTimeZone = true;
_dateTimeSettings.DefaultStoreTimeZoneId = "E. Europe Standard Time"; //(GMT+02:00) Minsk;
_dateTimeSettings.DefaultStoreTimeZoneId = "E. Europe Standard Time"; // (GMT+02:00) Minsk;

var customer = new Customer()
var customer = new Customer
{
Id = 10
};

_genericAttributeService.Expect(x => x.GetAttributesForEntity(customer.Id, "Customer"))
.Return(new List<GenericAttribute>()
{
new GenericAttribute()
{
StoreId = 0,
EntityId = customer.Id,
Key = SystemCustomerAttributeNames.TimeZoneId,
KeyGroup = "Customer",
Value = "Russian Standard Time" //(GMT+03:00) Moscow, St. Petersburg, Volgograd
}
});
_genericAttributeService
.Expect(x => x.GetAttribute<string>(nameof(Customer), customer.Id, SystemCustomerAttributeNames.TimeZoneId, 0))
.Return("Russian Standard Time"); // (GMT+03:00) Moscow, St. Petersburg, Volgograd

var timeZone = _dateTimeHelper.GetCustomerTimeZone(customer);
timeZone.ShouldNotBeNull();
Expand Down

0 comments on commit f5b62c3

Please sign in to comment.