Пример #1
0
        public void CreateFarmTestsWithObjectAsParameter()
        {
            //Instantiate a new DbContext with the in memory database
            var InMemoryContext = new AppleTreeDbContext(options);

            //Create an instance of FarmHelper and pass the in memory context as a parameter
            FarmHelper farmHelper = new FarmHelper(InMemoryContext);

            //Delete all farms
            farmHelper.DeleteFarmAll();

            //Check if the database is empty
            Assert.AreEqual(InMemoryContext.Farms.Count(), 0);



            //Create an in memory farm
            var farm = new Farm {
                Name       = "Second Testfarm",
                AppleTrees = AppleTreeDbInitializer.InitialiseTrees()
            };

            farmHelper.CreateFarm(farm);

            //Check if farm was inserted
            Assert.AreEqual(InMemoryContext.Farms.Count(), 1);

            //Check the inserted farm's properties
            Assert.AreEqual("Second Testfarm", InMemoryContext.Farms.FirstOrDefault().Name);
            Assert.AreEqual(10, InMemoryContext.Farms.FirstOrDefault().AppleTrees.Count);
        }
Пример #2
0
        public void CreateFarmAndGetFarmAllTests()
        {
            //Instantiate a new DbContext with the in memory database
            var InMemoryContext = new AppleTreeDbContext(options);

            //Create an instance of FarmHelper and pass the in memory context as a parameter
            FarmHelper farmHelper = new FarmHelper(InMemoryContext);

            farmHelper.DeleteFarmAll();

            //Check if the database is empty
            Assert.AreEqual(InMemoryContext.Farms.Count(), 0);

            //Create an in memory farm
            var farm = farmHelper.CreateFarm("Testfarm", AppleTreeDbInitializer.InitialiseTrees());

            //Check if farm was inserted
            Assert.AreEqual(InMemoryContext.Farms.Count(), 1);

            //Check the inserted farm's properties
            Assert.AreEqual("Testfarm", InMemoryContext.Farms.FirstOrDefault().Name);
            Assert.AreEqual(10, InMemoryContext.Farms.FirstOrDefault().AppleTrees.Count);

            ObservableCollection <Farm> farms = new ObservableCollection <Farm>(farmHelper.GetFarmAll());

            Assert.AreEqual(farms.FirstOrDefault().AppleTrees.Count, 10);
        }
        public static List <Farm> InitialiseFarms()
        {
            AppleTreeDbContext con = new AppleTreeDbContext();

            if (con.Farms.Count() == 0)
            {
                FarmHelper helper = new FarmHelper(con);

                List <Farm> farms = new List <Farm>
                {
                    new Farm
                    {
                        Name       = "First Farm",
                        AppleTrees = InitialiseTrees()
                    },
                    new Farm
                    {
                        Name       = "Second Farm",
                        AppleTrees = InitialiseTrees()
                    },
                    new Farm
                    {
                        Name       = "Third Farm",
                        AppleTrees = InitialiseTrees()
                    }
                };

                foreach (Farm farm in farms)
                {
                    if (!con.Farms.Where(c => c.Name == farm.Name).Any())
                    {
                        helper.CreateFarm(farm);
                    }
                }
                return(farms);
            }
            return(null);
        }