Ejemplo n.º 1
0
        public void Save(Equipment equipment)
        {
            if (equipment == null)
                throw new ArgumentNullException("equipment");

            using (var context = new DeviceHiveContext())
            {
                context.DeviceClasses.Attach(equipment.DeviceClass);
                context.Equipments.Add(equipment);
                if (equipment.ID > 0)
                {
                    context.Entry(equipment).State = EntityState.Modified;
                }
                context.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        public void EquipmentTest()
        {
            var deviceClass = new DeviceClass("D1", "V1");
            DeviceClassRepository.Save(deviceClass);

            var equipment = new Equipment("Test", "Code", "Type", deviceClass);
            EquipmentRepository.Save(equipment);

            // test GetByDeviceClass
            var equipments = EquipmentRepository.GetByDeviceClass(deviceClass.ID);
            Assert.Greater(equipments.Count, 0);

            // test Get(id)
            var equipment1 = EquipmentRepository.Get(equipment.ID);
            Assert.IsNotNull(equipment1);
            Assert.AreEqual("Test", equipment1.Name);
            Assert.AreEqual("Code", equipment1.Code);
            Assert.AreEqual("Type", equipment1.Type);
            Assert.AreEqual(deviceClass.ID, equipment1.DeviceClassID);
            Assert.IsNotNull(equipment1.DeviceClass);

            // test Save
            equipment.Name = "Test2";
            equipment.Code = "Code2";
            equipment.Type = "Type2";
            EquipmentRepository.Save(equipment);
            var equipment2 = EquipmentRepository.Get(equipment.ID);
            Assert.AreEqual("Test2", equipment2.Name);
            Assert.AreEqual("Code2", equipment2.Code);
            Assert.AreEqual("Type2", equipment2.Type);

            // test update relationship
            var deviceClass2 = new DeviceClass("D2", "V2");
            DeviceClassRepository.Save(deviceClass2);
            equipment.DeviceClass = deviceClass2;
            EquipmentRepository.Save(equipment);
            var equipment3 = EquipmentRepository.Get(equipment.ID);
            Assert.AreEqual(deviceClass2.ID, equipment3.DeviceClassID);
            Assert.IsNotNull(equipment3.DeviceClass);

            // test Delete
            EquipmentRepository.Delete(equipment.ID);
            var equipment4 = EquipmentRepository.Get(equipment.ID);
            Assert.IsNull(equipment4);
        }