public void RemovesBenefit()
        {
            object id = 0;

            using (var tx = Session.BeginTransaction())
            {
                var employee = new Employee
                {
                    Firstname = "John",
                    Lastname  = "Smith"
                };
                employee.AddBenefit(new SeasonTicketLoan());
                employee.AddBenefit(new Leave());
                employee.AddBenefit(new SkillsEnhancementAllowance());
                id = Session.Save(employee);

                tx.Commit();
            }

            Session.Clear();

            using (var tx = Session.BeginTransaction())
            {
                var emp   = Session.Get <Employee>(id);
                var leave = emp.Benefits.OfType <Leave>().FirstOrDefault();

                emp.RemoveBenefit(leave);

                tx.Commit();
            }

            Session.Clear();

            using (var tx = Session.BeginTransaction())
            {
                var employee = Session.Get <Employee>(id);
                var leave    = employee.Benefits.OfType <Leave>().FirstOrDefault();

                Assert.That(leave, Is.Null);
                tx.Commit();
            }
        }
        public void DeleteEmployee()
        {
            object id = 0;

            using (var tx = Session.BeginTransaction())
            {
                var employee = new Employee
                {
                    Firstname = "John",
                    Lastname  = "Smith"
                };
                employee.AddBenefit(new Leave
                {
                    AvailableEntitlement = 25,
                    RemainingEntitlement = 14
                });
                employee.AddCommunity(new Community
                {
                    Name = "New joiners"
                });

                id = Session.Save(employee);

                tx.Commit();
            }

            Session.Clear();

            using (var tx = Session.BeginTransaction())
            {
                var emp = Session.Get <Employee>(id);
                Session.Delete(emp);
                tx.Commit();
            }

            Session.Clear();

            using (var tx = Session.BeginTransaction())
            {
                var employee = Session.Get <Employee>(id);
                Assert.That(employee, Is.Null);
                tx.Commit();
            }
        }
        public void BenefitsAssociationIsSavedCorrectly()
        {
            using (var tx = Session.BeginTransaction())
            {
                var employee = new Employee
                {
                    Id = new EmployeeId
                    {
                        Firstname = "firstName",
                        Lastname  = "lastName",
                    },
                    DateOfJoining = new DateTime(1999, 2, 26)
                };
                employee.AddBenefit(new Benefit());

                Session.Save(employee);

                tx.Commit();
            }

            Session.Clear();

            using (var tx = Session.BeginTransaction())
            {
                var id = new EmployeeId
                {
                    Firstname = "firstName",
                    Lastname  = "lastName"
                };

                var employee = Session.Get <Employee>(id);
                Assert.That(employee.DateOfJoining.Year, Is.EqualTo(1999));
                Assert.That(employee.Benefits.Count, Is.EqualTo(1));
                tx.Commit();
            }
        }
        public void MapsBenefits()
        {
            object id = 0;

            using (var transaction = Session.BeginTransaction())
            {
                var skillsEnhancementAllowance = new SkillsEnhancementAllowance
                {
                    Entitlement = 1000, RemainingEntitlement = 250
                };
                var seasonTicketLoan = new SeasonTicketLoan
                {
                    Amount = 1416, MonthlyInstalment = 118, StartDate = new DateTime(2014, 4, 25), EndDate = new DateTime(2015, 3, 25)
                };
                var leave = new Leave
                {
                    AvailableEntitlement = 30, RemainingEntitlement = 15, Type = LeaveType.Paid
                };
                var employee = new Employee
                {
                    EmployeeNumber = "123456789"
                };
                employee.AddBenefit(skillsEnhancementAllowance);
                employee.AddBenefit(seasonTicketLoan);
                employee.AddBenefit(leave);

                id = Session.Save(employee);
                transaction.Commit();
            }

            Session.Clear();

            using (var transaction = Session.BeginTransaction())
            {
                var employee = Session.Get <Employee>(id);

                Assert.That(employee.Benefits.Count, Is.EqualTo(3));

                var seasonTicketLoan = employee.Benefits.FirstOrDefault(b => b.GetType().GetBaseTypes().Contains(typeof(SeasonTicketLoan)));
                Assert.That(seasonTicketLoan, Is.Not.Null);
                if (seasonTicketLoan != null)
                {
                    Assert.That(seasonTicketLoan.Employee.EmployeeNumber, Is.EqualTo("123456789"));
                }

                var skillsEnhancementAllowance = employee.Benefits.FirstOrDefault(b => b.GetType().GetBaseTypes().Contains(typeof(SkillsEnhancementAllowance)));
                Assert.That(skillsEnhancementAllowance, Is.Not.Null);
                if (skillsEnhancementAllowance != null)
                {
                    Assert.That(skillsEnhancementAllowance.Employee.EmployeeNumber, Is.EqualTo("123456789"));
                }

                var leave = employee.Benefits.FirstOrDefault(b => b.GetType().GetBaseTypes().Contains(typeof(Leave)));
                Assert.That(leave, Is.Not.Null);
                if (leave != null)
                {
                    Assert.That(leave.Employee.EmployeeNumber, Is.EqualTo("123456789"));
                }

                transaction.Commit();
            }
        }
Esempio n. 5
0
        public QueryTest()
        {
            Database = new InMemoryDatabase();
            Database.Initialize();

            var johnSmith = new Employee
            {
                Firstname          = "John",
                Lastname           = "Smith",
                DateOfJoining      = new DateTime(2014, 5, 5),
                ResidentialAddress = new Address
                {
                    AddressLine1 = "123 Planet place",
                    AddressLine2 = "12 Gomez street",
                    City         = "London",
                    Postcode     = "SW7 4FG",
                    Country      = "United Kingdom"
                }
            };

            johnSmith.AddCommunity(new Community
            {
                Name = "NHibernate Beginners"
            });
            johnSmith.AddBenefit(new SeasonTicketLoan
            {
                Amount            = 1320,
                MonthlyInstalment = 110
            });
            johnSmith.AddBenefit(new Leave
            {
                AvailableEntitlement = 12,
                RemainingEntitlement = 2
            });

            var hillaryGamble = new Employee
            {
                Firstname          = "hillary",
                Lastname           = "Gamble",
                DateOfJoining      = new DateTime(2013, 5, 5),
                ResidentialAddress = new Address
                {
                    AddressLine1 = "102 Oxygen",
                    AddressLine2 = "34 Western Gateway",
                    City         = "London",
                    Postcode     = "NW5 7AC",
                    Country      = "United Kingdom"
                }
            };

            hillaryGamble.AddCommunity(new Community
            {
                Name = "NHibernate experts"
            });

            hillaryGamble.AddBenefit(new SeasonTicketLoan
            {
                Amount            = 600,
                MonthlyInstalment = 50
            });



            var smithPearson = new Employee
            {
                Firstname          = "Smith",
                Lastname           = "Pearson",
                DateOfJoining      = new DateTime(2013, 5, 5),
                ResidentialAddress = new Address
                {
                    AddressLine1 = "102 Oxygen",
                    AddressLine2 = "34 Western Gateway",
                    City         = "London",
                    Postcode     = "NW5 7AC",
                    Country      = "United Kingdom"
                }
            };

            smithPearson.AddCommunity(new Community
            {
                Name = "NHibernate expers"
            });

            smithPearson.AddBenefit(new SkillsEnhancementAllowance
            {
                Entitlement = 1000
            });

            Database.SeedUsing(new List <Employee>
            {
                johnSmith,
                hillaryGamble,
                smithPearson
            });
        }