main
  1using System;
  2using developwithpassion.bdd.contexts;
  3using Gorilla.Commons.Testing;
  4using gorilla.commons.utility;
  5
  6namespace momoney.database.transactions
  7{
  8    public class TransactionSpecs
  9    {
 10    }
 11
 12    [Concern(typeof (Transaction))]
 13    public class behaves_like_transaction : concerns_for<ITransaction, Transaction>
 14    {
 15        context c = () =>
 16        {
 17            database = the_dependency<IDatabase>();
 18            factory = the_dependency<IChangeTrackerFactory>();
 19        };
 20
 21        static protected IDatabase database;
 22        static protected IChangeTrackerFactory factory;
 23    }
 24
 25    [Concern(typeof (Transaction))]
 26    public class when_creating_an_identity_map_for_a_specific_entity : behaves_like_transaction
 27    {
 28        it should_return_a_new_identity_map = () => result.should_not_be_null();
 29
 30        because b = () => { result = sut.create_for<Identifiable<Guid>>(); };
 31
 32        static IIdentityMap<Guid, Identifiable<Guid>> result;
 33    }
 34
 35    [Concern(typeof (Transaction))]
 36    public class when_committing_a_transaction_and_an_item_in_the_identity_map_has_changed : behaves_like_transaction
 37    {
 38        it should_commit_the_changes_to_that_item =
 39            () => tracker.was_told_to(x => x.commit_to(database));
 40
 41        context c = () =>
 42        {
 43            movie = new Movie("Goldeneye");
 44            tracker = an<IChangeTracker<IMovie>>();
 45
 46            when_the(factory).is_told_to(x => x.create_for<IMovie>()).it_will_return(tracker);
 47            when_the(tracker).is_told_to(x => x.is_dirty()).it_will_return(true);
 48        };
 49
 50
 51        because b = () =>
 52        {
 53            sut.create_for<IMovie>().add(movie.id, movie);
 54            movie.change_name_to("Austin Powers");
 55            sut.commit_changes();
 56        };
 57
 58        static IMovie movie;
 59        static IChangeTracker<IMovie> tracker;
 60    }
 61
 62    [Concern(typeof (Transaction))]
 63    public class when_deleting_a_set_of_entities_from_the_database : behaves_like_transaction
 64    {
 65        it should_prepare_to_delete_that_item_form_the_database = () => tracker.was_told_to(x => x.delete(movie));
 66
 67        it should_delete_all_items_marked_for_deletion = () => tracker.was_told_to(x => x.commit_to(database));
 68
 69        context c = () =>
 70        {
 71            movie = new Movie("Goldeneye");
 72            tracker = an<IChangeTracker<IMovie>>();
 73
 74            when_the(factory).is_told_to(x => x.create_for<IMovie>()).it_will_return(tracker);
 75            when_the(tracker).is_told_to(x => x.is_dirty()).it_will_return(true);
 76        };
 77
 78        because b = () =>
 79        {
 80            var map = sut.create_for<IMovie>();
 81            map.add(movie.id, movie);
 82            map.evict(movie.id);
 83            sut.commit_changes();
 84        };
 85
 86        static IMovie movie;
 87        static IChangeTracker<IMovie> tracker;
 88    }
 89
 90    public interface IMovie : Identifiable<Guid>
 91    {
 92        string name { get; }
 93        void change_name_to(string name);
 94    }
 95
 96    internal class Movie : IMovie
 97    {
 98        public Movie(string name)
 99        {
100            id = Guid.NewGuid();
101            this.name = name;
102        }
103
104        public string name { get; set; }
105
106        public void change_name_to(string new_name)
107        {
108            name = new_name;
109        }
110
111        public Id<Guid> id { get; set; }
112    }
113}