public void AddAdvertisement_WithValidAd_AddsCurrentNewspaperItemToNewspapersCollectionInTheAddedAd() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var detailViewModel = new NewspaperDetailViewModel(repository); var collectionViewModel = new NewspaperCollectionViewModel(repository); var paperModel1 = new Newspaper() {Name = "New paper 1"}; var paperItemViewModel1 = new NewspaperItemViewModel(repository) {Model = paperModel1}; var adModel = new Advertisement() {Name = "Ad 1 Name", Text = "Ad 1 text."}; var adItemViewModel = new AdvertisementItemViewModel(repository) {Model = adModel}; collectionViewModel.Newspapers.Add(paperItemViewModel1); collectionViewModel.CurrentItem.Should().Be(paperItemViewModel1, "The current item is the item that was added."); detailViewModel.ItemViewModel.Should().Be(collectionViewModel.CurrentItem, "The detail view model references the collection's current item."); paperItemViewModel1.Advertisements.Count.Should().Be(0, "No newspapers are referenced in the advertisement."); adItemViewModel.Newspapers.Count.Should().Be(0, "There are no papers in this ad's collection."); // Act paperItemViewModel1.Advertisements.Add(adItemViewModel); // Assert paperItemViewModel1.Advertisements.Count.Should().Be(1, "One item was added"); adItemViewModel.Newspapers.Count.Should().Be(1, "One newspaper reference was added to this advertisement."); adItemViewModel.Newspapers.ToList().First().Should().Be(collectionViewModel.CurrentItem, "The paper added to the advertisment's newspaper collection was the Newspaper collection's CurrentItem"); }
public void DeleteCommand_WhenItemDeletedFromTheCollection_CallsRepositoryDelete() { var repository = Substitute.For<INewspaperAdRepository>(); repository.GetAllAdvertisements().Returns(new List<Advertisement>()); bool deleteCalled; Newspaper deletedModel; var model1 = new Newspaper {Name = "Paper 1"}; var model2 = new Newspaper {Name = "Paper 2"}; var paperItemVm1 = new NewspaperItemViewModel(repository) {Model = model1}; var paperItemVm2 = new NewspaperItemViewModel(repository) {Model = model2}; var collectionViewModel = new NewspaperCollectionViewModel(repository); repository.When(x => x.Delete(model1)).Do(x => RepositoryUpdateFields(x, out deleteCalled, out deletedModel)); collectionViewModel.Newspapers.Add(paperItemVm1); collectionViewModel.Newspapers.Add(paperItemVm2); // Act collectionViewModel.Newspapers.Remove(paperItemVm1); paperItemVm1.DbStatus.Should().Be(DbModificationState.Deleted, "State of object should be deleted."); // Assert repository.Received().Save(model1); }
public NewspaperViewModel(Newspaper model, IRepository<Newspaper, Guid> repository) : base(model) { _model = model; _repository = repository; SaveCommand = new RelayCommand(new Action<object>(SaveCommand_Handler)); DeleteCommand = new RelayCommand(new Action<object>(DeleteCommand_Handler)); }
public void AddNewspaper(Newspaper paper) { if (paper == null) { return; } _newspapers.Add(paper); }
public void AddNewspaper_MultipleTimesWithValidNewspaperObjects_CreatesAReferenceToEachPaper() { // Arrange var paper1 = new Newspaper() {Name = "Paper 1 name"}; var paper2 = new Newspaper() {Name = "Paper 2 name"}; var ad = new Advertisement {Name = "Test ad 1 name", Text = "Test ad 1 text."}; // Act ad.AddNewspaper(paper1); ad.AddNewspaper(paper2); // Assert ad.Newspapers.ShouldBeEquivalentTo(new List<Newspaper> {paper2, paper1}); }
public void AddAdvertisement_WithValidAdvertisement_AddsNewspaperToAdvertisementNewspaperssCollection() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var paperModel = new Newspaper(); var adModel = new Advertisement(); var adItemViewModel = new AdvertisementItemViewModel(repository) {Model = adModel}; var adDetailViewModel = new AdvertisementDetailViewModel(repository) {ItemViewModel = adItemViewModel}; var detailViewModel = new NewspaperDetailViewModel(repository) {ItemViewModel = new NewspaperItemViewModel(repository) {Model = paperModel}}; // Act detailViewModel.Advertisements.Add(adItemViewModel); // Assert detailViewModel.Advertisements.ToList().First().Newspapers.Contains(detailViewModel.ItemViewModel); }
public void AddAdvertisement_WithValidAdvertisement_AddsAdvertisementToAdvertisementsCollection() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var paperModel = new Newspaper(); var adModel = new Advertisement(); var adItemViewModel = new AdvertisementItemViewModel(repository) {Model = adModel}; var adDetailViewModel = new AdvertisementDetailViewModel(repository) {ItemViewModel = adItemViewModel}; var detailViewModel = new NewspaperDetailViewModel(repository) {ItemViewModel = new NewspaperItemViewModel(repository) {Model = paperModel}}; // Act detailViewModel.Advertisements.Add(adItemViewModel); // Assert detailViewModel.Advertisements.Count.Should().Be(1, "There should be one item in the collection"); }
public void AddNewspapers_WithListOfValidNewspaperstestAddsAllItemsToNewspapersCollection() { // Arrange var paper1 = new Newspaper() {Name = "Paper 1 name"}; var paper2 = new Newspaper() {Name = "Paper 2 name"}; var paper3 = new Newspaper() {Name = "Paper 3 name"}; var paperList = new List<Newspaper> {paper1, paper2, paper3}; var ad = new Advertisement {Name = "Test ad 1 name", Text = "Test ad 1 text."}; // Act ad.AddNewspapers(paperList); //Assert ad.Newspapers.ShouldAllBeEquivalentTo(paperList); }
public void AddNewspapers_WithNullNewspaperList_ReturnsTheOriginalNewspapersCollection() { // Arrange var paper1 = new Newspaper() {Name = "Paper 1 name"}; var paper2 = new Newspaper() {Name = "Paper 2 name"}; var paper3 = new Newspaper() {Name = "Paper 3 name"}; var ad = new Advertisement {Name = "Test ad 1 name", Text = "Test ad 1 text."}; var paperList = new List<Newspaper> {paper1, paper3, paper2}; ad.AddNewspaper(paper1); ad.AddNewspaper(paper2); ad.AddNewspaper(paper3); // Act ad.AddNewspapers(null); // Assert ad.Newspapers.ShouldAllBeEquivalentTo(paperList); }
public void CurrentItem_AfterDeletingLastItemOnNewspapersCollection_SetToLastItemInNewspapersCollection() { // Arrange var repository = GetNewspaperRepository(); var model1 = new Newspaper {Name = "Paper 1"}; var model2 = new Newspaper {Name = "Paper 2"}; var model3 = new Newspaper {Name = "Paper 3"}; var paperItemVm1 = new NewspaperItemViewModel(repository) {Model = model1}; var paperItemVm2 = new NewspaperItemViewModel(repository) {Model = model2}; var paperItemVm3 = new NewspaperItemViewModel(repository) {Model = model3}; var collectionViewModel = new NewspaperCollectionViewModel(repository); collectionViewModel.Newspapers.Add(paperItemVm1); collectionViewModel.Newspapers.Add(paperItemVm2); collectionViewModel.Newspapers.Add(paperItemVm3); collectionViewModel.CurrentItem.Should().Be(paperItemVm3, "CurrentItem should be the last item added."); // Act collectionViewModel.Newspapers.Remove(paperItemVm3); // Assert collectionViewModel.CurrentItem.Should().Be(paperItemVm2, "Removing the last entry makes the current last entry the CurrentItem"); }
public void NewspaperDetailItem_WhenNewspaperCurrentItemChangedMessageReceived_ResetsItemViewModelToValueSentInMessage() { // Arrange var repository = Substitute.For<INewspaperAdRepository>(); var detailViewModel = new NewspaperDetailViewModel(repository); var collectionViewModel = new NewspaperCollectionViewModel(repository); var paperModel1 = new Newspaper() {Name = "New paper 1"}; var paperModel2 = new Newspaper() {Name = "New paper 2"}; var paperItemViewModel1 = new NewspaperItemViewModel(repository) {Model = paperModel1}; var paperItemViewModel2 = new NewspaperItemViewModel(repository) {Model = paperModel2}; collectionViewModel.Newspapers.Add(paperItemViewModel1); collectionViewModel.Newspapers.Add(paperItemViewModel2); collectionViewModel.CurrentItem.Should().Be(paperItemViewModel2); // Act collectionViewModel.CurrentItem = paperItemViewModel1; // Assert detailViewModel.ItemViewModel.Should().Be(paperItemViewModel1); }
public void SaveCommand_WhenPopulatedAndNewItemAdded_IsExecuted() { // Arrange var saveCommandCalled = false; var repository = GetNewspaperRepository(); var model1 = new Newspaper {Name = "Paper 1"}; var paperItemVm1 = new NewspaperItemViewModel(repository) {Model = model1}; var collectionViewModel = new NewspaperCollectionViewModel(repository); collectionViewModel.SaveCommand = new RelayCommand(() => { saveCommandCalled = true; }); // Act collectionViewModel.Newspapers.Add(paperItemVm1); // Assert repository.Received().Save(model1); }
public void SelectedItemChangedHandler_WhenCollectionChanged_IsCalled() { // Arrange var repository = GetNewspaperRepository(); var collectionChangedCalled = false; var model1 = new Newspaper {Name = "Paper 1"}; var paperItemVm1 = new NewspaperItemViewModel(repository) {Model = model1}; var collectionViewModel = new NewspaperCollectionViewModel(repository); collectionViewModel.SelectedItemChangedCommand = new RelayCommand<RoutedPropertyChangedEventArgs<object>>((args) => { collectionChangedCalled = true; }); collectionViewModel.Newspapers.Add(paperItemVm1); // Act collectionViewModel.SelectedItemChangedCommand.Execute(null); // Assert collectionChangedCalled.Should().Be(true, "The event was called"); }
private void AddingNewspaperItemMessageHandler(AddingNewspaperItemMessage message) { var model = new Newspaper(); var vm = new NewspaperItemViewModel(_adRepository) {Model = model}; Newspapers.Add(vm); var sortedNewspapers = Newspapers.OrderBy(n => n.Name).ToList(); Newspapers.Clear(); sortedNewspapers.ForEach(Newspapers.Add); Messenger.Default.Send(new CurrentNewspaperItemChangedMessage(vm)); }
public void RemovingItemMessageHandler_WhenInvoked_SendsCurrentItemChangedMessage() { // Arrange var itemChangedMessageReceived = false; NewspaperItemViewModel changedItemViewModel = null; var repository = GetNewspaperRepository(); var model1 = new Newspaper {Name = "Paper 1"}; var model2 = new Newspaper {Name = "Paper 2"}; var model3 = new Newspaper {Name = "Paper 3"}; var paperItemVm1 = new NewspaperItemViewModel(repository) {Model = model1}; var paperItemVm2 = new NewspaperItemViewModel(repository) {Model = model2}; var paperItemVm3 = new NewspaperItemViewModel(repository) {Model = model3}; var collectionViewModel = new NewspaperCollectionViewModel(repository); collectionViewModel.Newspapers.Add(paperItemVm1); collectionViewModel.Newspapers.Add(paperItemVm2); collectionViewModel.Newspapers.Add(paperItemVm3); collectionViewModel.CurrentItem.Should().Be(paperItemVm3, "CurrentItem should be the last item added."); collectionViewModel.Newspapers.Count.Should().Be(3, "There are now three items in the list."); var message = new RemovingNewspaperItemMessage(paperItemVm2); Messenger.Default.Register<CurrentNewspaperItemChangedMessage>(this, (msg) => { itemChangedMessageReceived = true; changedItemViewModel = msg.ItemViewModel; }); // Act Messenger.Default.Send<RemovingNewspaperItemMessage>(message); // Assert itemChangedMessageReceived.Should().Be(true, "The message was received from the collection view model."); changedItemViewModel.Should().Be(collectionViewModel.CurrentItem, "The item sent is the current item from the collection view model."); }
private Newspaper GetExistingNewspaper() { var paper = new Newspaper {Name = TEST_PAPER_NAME}; paper.DbStatus = DbModificationState.Unchanged; return paper; }
public void RemoveAdvertisement_WithAnAdvertisementInTheAdvertisementsCollection_RemovesThatAdvertisementFromTheCollection() { // Arrange var paper = new Newspaper(); var ad1 = new Advertisement {Name = "Test Ad 1 Name", Text = "Test Ad 1 Text"}; var ad2 = new Advertisement {Name = "Test Ad 2 Name", Text = "Test Ad 2 Text"}; paper.AddAdvertisement(ad1); paper.AddAdvertisement(ad2); // act paper.RemoveAdvertisement(ad2); // Assert paper.Advertisements.Should().BeEquivalentTo(new List<Advertisement> {ad1}, "Removing the second added Ad results in only the first being left."); }
public void Save(Newspaper entity) { // if (entity.DbStatus == DbModificationState.Unchanged) // return; if (entity.DbStatus != DbModificationState.Deleted) { if (_newspaperCache.ContainsKey(entity.UKey)) { _newspaperCache[entity.UKey] = entity; UpdateAdCacheFromPaper(entity); var deletedAds = entity.Advertisements.ToList().Where(a => a.DbStatus == DbModificationState.Deleted).ToList(); deletedAds.ForEach(a=>entity.Advertisements.Remove(a)); OnItemChanged(entity); } else { _newspaperCache.Add(entity.UKey, entity); UpdateAdCacheFromPaper(entity); OnItemAdded(entity); } } else { if (_newspaperCache.ContainsKey(entity.UKey)) { DeleteNewspaper(entity); UpdateAdCacheFromPaper(entity); OnItemRemoved(entity); } } _serializer.SaveCache(_newspaperCache); }
//jlg bad logic private void UpdateAdCacheFromPaper(Newspaper entity) { List<Advertisement> deletedAds = new List<Advertisement>(); foreach (var ad in entity.Advertisements) { switch (entity.DbStatus) { case DbModificationState.Unchanged: case DbModificationState.Added: case DbModificationState.Modified: if(!_advertisementCache.ContainsKey(ad.UKey)) _advertisementCache.Add(ad.UKey,ad); deletedAds.AddRange(entity.Advertisements.Where(a=>a.DbStatus==DbModificationState.Deleted)); break; case DbModificationState.Deleted: foreach (var cacheAd in _advertisementCache.Values.ToList()) { cacheAd.Newspapers.Remove(entity); } break; } } deletedAds.ForEach(a=>_advertisementCache.Remove(a.UKey)); }
private void OnItemRemoved(Newspaper paper) { if (ItemRemoved != null) ItemRemoved(this, new RepositoryItemRemovedEventArgs {Entity = paper}); }
private void DeleteNewspaper(Newspaper entity) { if (_newspaperCache.ContainsKey(entity.UKey)) { _newspaperCache.Remove(entity.UKey); foreach (var ad in entity.Advertisements) _advertisementCache[ad.UKey].Newspapers.Remove(entity); } _serializer.SaveCache(_newspaperCache); }
private NewspaperItemViewModel GetValidNewspaperItemViewModel(Newspaper paper = null, INewspaperAdRepository adRepository = null) { var model = paper ?? new Newspaper() {Name = "<New Newspaper>"}; var repo = adRepository ?? Substitute.For<INewspaperAdRepository>(); return new NewspaperItemViewModel(repo) {Model = model}; }
public void Name_WhenModifiedFromValidToInvalidChangesHasErrors_PerformsErrorsChangedCallback() { // Arrange const string PAPER_NAME = "Paper 1 name"; const string NEW_PAPER_NAME = ""; var errorChangedCalled = false; var paper = new Newspaper() {Name = PAPER_NAME}; paper.ErrorsChanged += delegate(object sender, DataErrorsChangedEventArgs e) { errorChangedCalled = true; }; paper.Name = NEW_PAPER_NAME; errorChangedCalled.Should().Be(true, "Event callback should have occurred."); }
public void Name_WhenModified_PerformsPropertyChangedCallback() { // Arrange const string PAPER_NAME = "Paper 1 name"; const string NEW_PAPER_NAME = "New paper 1 name"; var paper = new Newspaper() {Name = PAPER_NAME}; var eventAssert = new PropertyChangedEventAssert(paper); paper.Name.Should().Be(PAPER_NAME, "Ad name set properly"); eventAssert.ExpectNothing(); paper.Name = NEW_PAPER_NAME; eventAssert.Expect("Name"); }
public void Newspapers_SetToNull_SetsNewspapersToAnEmptyCollection() { // Arrange var testAd = new Advertisement {Name = "Test ad 1 name", Text = "Test ad 1 text."}; var testPaper1 = new Newspaper() {Name = "Test Paper 1 Name"}; var testPaper2 = new Newspaper() {Name = "Test Paper 2 Name"}; var testPaper3 = new Newspaper() {Name = "Test Paper 3 Name"}; testAd.Newspapers = new ObservableCollection<Newspaper>() {testPaper1, testPaper2, testPaper3}; testAd.Newspapers.Count.Should().Be(3, "All papers were added to the ad"); // Act testAd.Newspapers = null; // Assert testAd.Newspapers.ShouldBeEquivalentTo(new List<Newspaper>(), "Newspaper collection is set to empty list when null is assigned to it."); }
public void RemoveAdvertisement_WithANullAdvertisement_DoesNotChangeTheAdvertisementsConnection() { // Arrange var paper = new Newspaper(); var ad1 = new Advertisement {Name = "Test Ad 1 Name", Text = "Test Ad 1 Text"}; var ad2 = new Advertisement {Name = "Test Ad 2 Name", Text = "Test Ad 2 Text"}; paper.AddAdvertisement(ad1); paper.AddAdvertisement(ad2); // act paper.RemoveAdvertisement(null); // Assert paper.Advertisements.Should().BeEquivalentTo(new List<Advertisement> {ad1, ad2}, "Removing a null item results in nothing being removed."); }
public void SelectedItemChangedHandler_WhenInvoked_SetsCurrentItemToNewSelection() { // Arrange var repository = GetNewspaperRepository(); var model1 = new Newspaper {Name = "Paper 1"}; var paperItemVm1 = new NewspaperItemViewModel(repository) {Model = model1}; var collectionViewModel = new NewspaperCollectionViewModel(repository); collectionViewModel.CurrentItem.Should().Be(null, "There is no current item in an empty list."); // Act collectionViewModel.Newspapers.Add(paperItemVm1); // Assert collectionViewModel.CurrentItem.Should().Be(paperItemVm1, "The current item is now"); }
public void AddNewspaper(Newspaper paperModel) { if (paperModel == null) return; }
public void AddNewspaper_WithValidNewspaperObject_CreatesAReferenceToThatNewspaper() { // Arrange var paper = new Newspaper() {Name = "Test Paper 1"}; var advertisement = new Advertisement {Name = "Test Ad 1", Text = "Test ad 1 text."}; // Act advertisement.AddNewspaper(paper); //Assert advertisement.Newspapers.ShouldAllBeEquivalentTo(new List<Newspaper> {paper}, "A paper added to an advertisement adds a reference to that paper."); }
private static NewspaperViewModel GetNewspaperViewModel(Newspaper paper, IRepository<Newspaper, Guid> repository = null) { var repo = repository ?? Substitute.For<IRepository<Newspaper, Guid>>(); return new NewspaperViewModel(paper, repo); }
public void CurrentItem_AfterDeletingNotLastExistingItem_IsTheFollowingItem() { // Arrange var repository = GetNewspaperRepository(); var model1 = new Newspaper {Name = "Paper 1"}; var model2 = new Newspaper {Name = "Paper 2"}; var model3 = new Newspaper {Name = "Paper 3"}; var model4 = new Newspaper {Name = "Paper 4"}; var paperItemVm1 = new NewspaperItemViewModel(repository) {Model = model1}; var paperItemVm2 = new NewspaperItemViewModel(repository) {Model = model2}; var paperItemVm3 = new NewspaperItemViewModel(repository) {Model = model3}; var paperItemVm4 = new NewspaperItemViewModel(repository) {Model = model4}; var collectionViewModel = new NewspaperCollectionViewModel(repository); collectionViewModel.Newspapers.Add(paperItemVm1); collectionViewModel.Newspapers.Add(paperItemVm2); collectionViewModel.Newspapers.Add(paperItemVm3); collectionViewModel.Newspapers.Add(paperItemVm4); collectionViewModel.CurrentItem.Should().Be(paperItemVm4, "CurrentItem should be the last item added."); // Act collectionViewModel.Newspapers.Remove(paperItemVm2); // Assert collectionViewModel.CurrentItem.Should().Be(paperItemVm3, "CurrentItem should be the first item following the one removed"); }