Esempio n. 1
0
        public async Task RegisterNewDeviceTypeSettingTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());
            var adapterBuilder    = new AdapterSettingBuilder(dbConnection, CancellationToken.None);
            var deviceType        = UnitTesting.CreateFakeDeviceType();
            var deviceTypeSetting = new DeviceTypeSetting
            {
                Name       = "Device type Setting 1",
                ValueType  = DataType.STRING,
                Value      = "Hello World",
                DeviceType = deviceType
            };

            //act
            var result = await adapterBuilder.RegisterDeviceTypeSettingAsync(deviceTypeSetting);

            DeviceTypeSetting setting;

            using (var context = new ZvsContext(dbConnection))
            {
                setting = await context.DeviceTypeSettings.FirstOrDefaultAsync();
            }

            //assert
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError);
            Assert.IsNotNull(setting, "Setting not saved!");
            Assert.IsTrue(setting.Name == deviceTypeSetting.Name, "Device type setting name mismatch");
        }
Esempio n. 2
0
        public async Task RegisterRemovedDeviceTypeSettingOptionTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var adapterBuilder    = new AdapterSettingBuilder(dbConnection, CancellationToken.None);
            var deviceType        = UnitTesting.CreateFakeDeviceType();
            var deviceTypeSetting = new DeviceTypeSetting
            {
                Name       = "Device Type Setting 1",
                ValueType  = DataType.STRING,
                Value      = "Hello World",
                DeviceType = deviceType
            };
            var option1 = new DeviceTypeSettingOption
            {
                Name = "Option 1",
            };
            var option2 = new DeviceTypeSettingOption
            {
                Name = "Option 2",
            };

            deviceTypeSetting.Options.Add(option1);
            deviceTypeSetting.Options.Add(option2);
            using (var context = new ZvsContext(dbConnection))
            {
                context.DeviceTypeSettings.Add(deviceTypeSetting);
                await context.SaveChangesAsync();
            }

            deviceTypeSetting.Options.Remove(option2);

            //act
            var result = await adapterBuilder.RegisterDeviceTypeSettingAsync(deviceTypeSetting);

            DeviceTypeSetting setting;

            using (var context = new ZvsContext(dbConnection))
            {
                setting = await context.DeviceTypeSettings
                          .Include(o => o.Options)
                          .FirstOrDefaultAsync();
            }

            //assert
            Console.WriteLine(result.Message);
            Assert.IsFalse(result.HasError);
            Assert.IsNotNull(setting, "Setting not found");
            Assert.IsTrue(setting.Options.Count == 1, "Expected 2 options");
            Assert.IsTrue(setting.Options[0].Name == option1.Name, "Name mismatch");
        }
Esempio n. 3
0
        public async Task <Result> RegisterDeviceTypeSettingAsync(DeviceTypeSetting deviceTypeSetting)
        {
            using (var context = new ZvsContext(EntityContextConnection))
            {
                var existingDp = await context.DeviceTypeSettings
                                 .Include(o => o.Options)
                                 .FirstOrDefaultAsync(d =>
                                                      d.UniqueIdentifier == deviceTypeSetting.UniqueIdentifier &&
                                                      d.DeviceTypeId == deviceTypeSetting.DeviceTypeId, CancellationToken);

                var changed = false;
                if (existingDp == null)
                {
                    context.DeviceTypeSettings.Add(deviceTypeSetting);
                    changed = true;
                }
                else
                {
                    PropertyChangedEventHandler handler = (s, a) => changed = true;
                    existingDp.PropertyChanged += handler;

                    existingDp.Name        = deviceTypeSetting.Name;
                    existingDp.Description = deviceTypeSetting.Description;
                    existingDp.ValueType   = deviceTypeSetting.ValueType;
                    existingDp.Value       = deviceTypeSetting.Value;

                    existingDp.PropertyChanged -= handler;

                    var added = deviceTypeSetting.Options.Where(option => existingDp.Options.All(o => o.Name != option.Name)).ToList();
                    foreach (var option in added)
                    {
                        existingDp.Options.Add(option);
                        changed = true;
                    }

                    var removed = existingDp.Options.Where(option => deviceTypeSetting.Options.All(o => o.Name != option.Name)).ToList();
                    foreach (var option in removed)
                    {
                        context.DeviceTypeSettingOptions.Local.Remove(option);
                        changed = true;
                    }
                }

                if (changed)
                {
                    return(await context.TrySaveChangesAsync(CancellationToken));
                }
            }
            return(Result.ReportSuccess("Nothing to update"));
        }