public void ViewModelFieldConstructorWithValueTest()
        {
            var expectedValue = "expected value";

            var instance = new ModelFixture <string>(expectedValue);

            Assert.IsTrue(expectedValue.Equals(instance.Value), "Value was wrong.");
        }
        public void ViewModelFieldToStringTest()
        {
            var expectedValue = "expected value";

            var instance = new ModelFixture <string>(expectedValue);

            Assert.IsTrue(expectedValue.Equals(instance.Value), "Value was wrong.");
            Assert.IsTrue(expectedValue.Equals(instance.ToString()), "ToString() was wrong.");
        }
        public void TestLocate_Field_ThrowsForNonExistentFields()
        {
            // Arrange
            var tModel = ModelFixture.Create();
            var ast    = KayleeHelper.Parse(tModel);
            var user   = ast.Locate("auth", new[] { "User" });

            // Act & Assert
            Assert.Throws <FieldNotFoundException>(() => user.Locate("VoidField"));
        }
Exemplo n.º 4
0
        public void SetProperty_NewValue_ShouldSetValue()
        {
            // Prepare
            var value        = 10;
            var modelFixture = new ModelFixture
            {
                // Act
                Number = value
            };

            // Assert
            Assert.Equal(value, modelFixture.Number);
        }
        public void TestLocate_Field_PartOfParentKey()
        {
            // Arrange
            var tModel          = ModelFixture.Create();
            var ast             = KayleeHelper.Parse(tModel);
            var tenantProcedure = ast.Locate("tenant", new[] { "Tenant", "Procedure" });
            var fieldName       = "TenantId";
            var tenantId        = tenantProcedure.Parent.Fields.Single(f => f.Name == fieldName);
            // Act
            var field = tenantProcedure.Locate(fieldName);

            // Assert
            Assert.Equal(tenantId, field);
        }
        public void TestLocate_Field()
        {
            // Arrange
            var tModel    = ModelFixture.Create();
            var ast       = KayleeHelper.Parse(tModel);
            var user      = ast.Locate("auth", new[] { "User" });
            var fieldName = "UserId";
            var userId    = user.Fields.Single(f => f.Name == fieldName);
            // Act
            var field = user.Locate(fieldName);

            // Assert
            Assert.Equal(userId, field);
        }
Exemplo n.º 7
0
        public void Constructor_PropertyDependency_ShouldAddPropertyDependencies()
        {
            // Prepare
            var modelFixture = new ModelFixture
            {
                Number = 10,
                Text   = "test text"
            };

            // Assert
            var propertyDepedencies = modelFixture.GetPropertyDependencies("Text");

            Assert.Contains("TextDependency", propertyDepedencies);
            Assert.DoesNotContain("Number", propertyDepedencies);
        }
        public void TestGetFullPrimaryKey()
        {
            // Arrange
            var tModel    = ModelFixture.Create();
            var ast       = KayleeHelper.Parse(tModel);
            var procedure = ast.Locate("tenant", new[] { "Tenant", "Procedure" });
            // Act
            var fullPrimaryKey = procedure.GetFullPrimaryKey();
            // Assert
            var expected = new[] {
                procedure.Parent.PrimaryKey.Single(),
                procedure.PrimaryKey.Single()
            };

            Assert.Equal(expected, fullPrimaryKey);
        }
Exemplo n.º 9
0
        public void ValidateIsInRangeTest()
        {
            var model = new ModelFixture <int>
            {
                Value = 80
            };

            model.Validator = new Action <IObservableClass>(arg =>
            {
                var i = arg as ModelFixture <int>;

                if (!Enumerable.Range(1, 500).Contains(i.Value))
                {
                    i.Properties[nameof(i.Value)].Errors.Add("Portnumber is not in range [1,100]");
                }
            });

            Assert.IsTrue(model.Validate());
        }
Exemplo n.º 10
0
        public void ValidateStringLengthIsInRangeTest()
        {
            var model = new ModelFixture <string>
            {
                Value = "192.168.1.0"
            };

            model.Validator = new Action <IObservableClass>(arg =>
            {
                var i = arg as ModelFixture <string>;

                if (!Enumerable.Range(1, 35).Contains(i.Value.Length))
                {
                    i.Properties[nameof(i.Value)].Errors.Add("String should have length of 1-35 characters");
                }
            });

            Assert.IsTrue(model.Validate());
        }
Exemplo n.º 11
0
        public void ValidateIsNotNullTest()
        {
            var model = new ModelFixture <string>
            {
                Value = "192.168.1.0"
            };

            model.Validator = new Action <IObservableClass>(arg =>
            {
                var i = arg as ModelFixture <string>;

                if (string.IsNullOrEmpty(i.Value))
                {
                    i.Properties[nameof(i.Value)].Errors.Add("Value is null");
                }
            });

            Assert.IsTrue(model.Validate());
        }
        public void UpdateCollection_NewItem_ShouldAddItemToList()
        {
            // Prepare
            var collection = new ObservableCollection <ModelFixture>();
            var model1     = new ModelFixture {
                Id = 1
            };

            // Assert
            Assert.DoesNotContain(collection, item => item.Id == 1);

            // Act
            collection.UpdateCollection(new List <ModelFixture> {
                model1
            });

            // Assert
            Assert.Contains(collection, item => item.Id == 1);
        }
        public void ViewModelFieldNotifyPropertyChangedDoesNotNotifyOnSameValueTest()
        {
            var originalValue = "original value";

            var instance = new ModelFixture <string>(originalValue);

            Assert.IsInstanceOfType(instance, typeof(INotifyPropertyChanged));

            var gotEvent = false;

            ((INotifyPropertyChanged)instance).PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
            {
                gotEvent = true;
                Assert.IsFalse(gotEvent, "Should not get any PropertyChanged events.");
            };

            instance.Value = originalValue;

            Assert.IsFalse(gotEvent, "Should not have gotten the PropertyChanged event.");
        }
        public void UpdateCollection_UpdatedItem_ShouldUpdateItemInList()
        {
            // Prepare
            var collection = new ObservableCollection <ModelFixture>();
            var model1     = new ModelFixture {
                Id = 1, Text = "Old Text"
            };

            collection.Add(model1);

            model1.Text = "New Text";

            // Act
            collection.UpdateCollection(new List <ModelFixture> {
                model1
            });

            // Assert
            Assert.Equal(model1, collection[0]);
        }
Exemplo n.º 15
0
        public async Task GiveZipModelWhenUploaded_WhenDownloadAndDeleted_ShouldVerify()
        {
            ModelFixture modelFixture = ModelFixture.GetModelFixture();

            string tempZipFile = FileTools.WriteResourceToTempFile("TestZip.Zip", nameof(MlPackageActivityTests), typeof(MlPackageActivityTests), _testZipResourceId);

            ModelId modelId = new ModelId($"test-zip-{Guid.NewGuid()}/v100");

            IOption option = new Option
            {
                PackageFile = tempZipFile,
                ModelName   = modelId.ModelName,
                VersionId   = modelId.VersionId,
            };

            await new UploadModelActivity(option, modelFixture.ModelRepository, new NullLogger <UploadModelActivity>())
            .Upload(CancellationToken.None);

            string toZipFile = Path.GetDirectoryName(tempZipFile).Func(x => Path.Combine(x !, "TestZip-Copy.Zip"));

            IOption downloadOption = new Option
            {
                PackageFile = toZipFile,
                ModelName   = option.ModelName,
                VersionId   = option.VersionId,
            };

            await new DownloadModelActivity(downloadOption, modelFixture.ModelRepository, new NullLogger <DownloadModelActivity>()).Download(CancellationToken.None);

            byte[] originalZipHash = FileTools.GetFileHash(tempZipFile);
            byte[] downloadZipHash = FileTools.GetFileHash(toZipFile);
            originalZipHash.SequenceEqual(downloadZipHash).Should().BeTrue();

            DeleteModelActivity uploadDeleteActivity = new DeleteModelActivity(option, modelFixture.ModelRepository, new NullLogger <DeleteModelActivity>());
            await uploadDeleteActivity.Delete(CancellationToken.None);

            (await modelFixture.ModelRepository.Exist(modelId, CancellationToken.None)).Should().BeFalse();

            File.Delete(tempZipFile);
            File.Delete(toZipFile);
        }
Exemplo n.º 16
0
        public void OnPropertyChanged_Raised_ShouldExtractPropertyName()
        {
            // Prepare
            var invoked      = false;
            var modelFixture = new ModelFixture();

            modelFixture.PropertyChanged += (o, e) =>
            {
                // Act
                if (e.PropertyName.Equals(nameof(modelFixture.Number)))
                {
                    invoked = true;
                }
            };

            // Act
            modelFixture.InvokePropertyChanged(nameof(modelFixture.Number));

            //Assert
            Assert.True(invoked);
        }
Exemplo n.º 17
0
        public void SetProperty_PropertyDependency_ShouldRaisePropertyChanged()
        {
            // Prepare
            var modelFixture = new ModelFixture();
            var invoked      = false;

            modelFixture.PropertyChanged += (o, e) =>
            {
                // Act
                if (e.PropertyName.Equals(nameof(modelFixture.TextDependency)))
                {
                    invoked = true;
                }
            };

            // Act
            modelFixture.Text = "New Text";

            // Assert
            Assert.True(invoked);
        }
Exemplo n.º 18
0
        public async Task GivenZipModel_WhenUploaded_ShouldPass()
        {
            ModelFixture modelFixture = ModelFixture.GetModelFixture();

            string tempZipFile = FileTools.WriteResourceToTempFile("TestZip.Zip", nameof(MlPackageActivityTests), typeof(MlPackageActivityTests), _testZipResourceId);

            ModelId modelId = new ModelId($"test-zip-{Guid.NewGuid()}/v100");

            IOption option = new Option
            {
                PackageFile = tempZipFile,
                ModelName   = modelId.ModelName,
                VersionId   = modelId.VersionId,
            };

            await new UploadModelActivity(option, modelFixture.ModelRepository, new NullLogger <UploadModelActivity>()).Upload(CancellationToken.None);

            (await modelFixture.ModelRepository.Exist(modelId, CancellationToken.None)).Should().BeTrue();

            File.Delete(tempZipFile);
        }
Exemplo n.º 19
0
        public void SetProperty_NewValue_ShouldRaisePropertyChanged()
        {
            // Prepare
            var value        = 10;
            var modelFixture = new ModelFixture();
            var invoked      = false;

            modelFixture.PropertyChanged += (o, e) =>
            {
                // Act
                if (e.PropertyName.Equals(nameof(modelFixture.Number)))
                {
                    invoked = true;
                }
            };

            // Act
            modelFixture.Number = value;

            // Assert
            Assert.True(invoked);
        }
        public void ViewModelFieldNotifyPropertyChangedNotifiesOnNewValueTest()
        {
            var originalValue = "original value";

            var instance = new ModelFixture <string>(originalValue);

            Assert.IsInstanceOfType(instance, typeof(INotifyPropertyChanged));

            var gotEvent = false;

            ((INotifyPropertyChanged)instance).PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
            {
                gotEvent = true;
                Assert.IsTrue(e.PropertyName.Equals("Value") | e.PropertyName.Equals("IsValid"), "PropertyName was wrong.");
            };

            var newValue = "new value";

            instance.Value = newValue;

            Assert.IsTrue(newValue.Equals(instance.Value), "Value didn't change.");
            Assert.IsTrue(gotEvent, "Didn't get the PropertyChanged event.");
        }
Exemplo n.º 21
0
        public void TestParse()
        {
            // Arrange
            var tModel = ModelFixture.Create();
            // Act
            var ast = KayleeHelper.Parse(tModel);

            // Assert
            Assert.Collection(ast.Schemata,
                              s =>
            {
                Assert.Equal("auth", s.Name);
                Assert.Collection(s.Entities, e =>
                {
                    Assert.Equal("User", e.Name);
                    Assert.False(e.IsQuery);
                    Assert.Collection(e.Fields, f =>
                    {
                        Assert.Equal("UserId", f.Name);
                        Assert.Equal(FieldType.GUID, f.Type);
                        Assert.Equal("NEWID()", f.DefaultExpression);
                        Assert.False(f.Nullable);
                        Assert.False(f.Computed);
                    }, f =>
                    {
                        Assert.Equal("CreatedAt", f.Name);
                        Assert.Equal(FieldType.DATE, f.Type);
                        Assert.Equal("SYSDATETIMEOFFSET()", f.DefaultExpression);
                        Assert.False(f.Nullable);
                        Assert.False(f.Computed);
                    }, f =>
                    {
                        Assert.Equal("FirstName", f.Name);
                        Assert.Equal(FieldType.TEXT, f.Type);
                        Assert.Equal(100, f.Size.Size);
                        Assert.Equal(0, f.Size.Precision);
                        Assert.Null(f.DefaultExpression);
                        Assert.True(f.Nullable);
                        Assert.False(f.Computed);
                    }, f =>
                    {
                        Assert.Equal("LastName", f.Name);
                        Assert.Equal(FieldType.TEXT, f.Type);
                        Assert.Equal(100, f.Size.Size);
                        Assert.Equal(0, f.Size.Precision);
                        Assert.Null(f.DefaultExpression);
                        Assert.True(f.Nullable);
                        Assert.False(f.Computed);
                    }, f =>
                    {
                        Assert.Equal("ContactEmail", f.Name);
                        Assert.Equal(FieldType.TEXT, f.Type);
                        Assert.Equal(254, f.Size.Size);
                        Assert.Equal(0, f.Size.Precision);
                        Assert.Null(f.DefaultExpression);
                        Assert.True(f.Nullable);
                        Assert.False(f.Computed);
                    }, f =>
                    {
                        Assert.Equal("ContactPhone", f.Name);
                        Assert.Equal(FieldType.TEXT, f.Type);
                        Assert.Equal(50, f.Size.Size);
                        Assert.Equal(0, f.Size.Precision);
                        Assert.Null(f.DefaultExpression);
                        Assert.True(f.Nullable);
                        Assert.False(f.Computed);
                    }, f =>
                    {
                        Assert.Equal("Verified", f.Name);
                        Assert.Equal(FieldType.BIT, f.Type);
                        Assert.Equal("0", f.DefaultExpression);
                        Assert.False(f.Nullable);
                        Assert.False(f.Computed);
                    }, f =>
                    {
                        Assert.Equal("SuperUser", f.Name);
                        Assert.Equal(FieldType.BIT, f.Type);
                        Assert.Equal("0", f.DefaultExpression);
                        Assert.False(f.Nullable);
                        Assert.False(f.Computed);
                    }, f =>
                    {
                        Assert.Equal("NormalizedContactEmail", f.Name);
                        Assert.Equal(FieldType.TEXT, f.Type);
                        Assert.Equal(254, f.Size.Size);
                        Assert.Equal(0, f.Size.Precision);
                        Assert.Null(f.DefaultExpression);
                        Assert.False(f.Nullable);
                        Assert.True(f.Computed);
                    }, f =>
                    {
                        Assert.Equal("Hash", f.Name);
                        Assert.Equal(FieldType.BINARY, f.Type);
                        Assert.Equal(128, f.Size.Size);
                        Assert.Equal(0, f.Size.Precision);
                        Assert.Null(f.DefaultExpression);
                        Assert.False(f.Nullable);
                        Assert.False(f.Computed);
                    }, f =>
                    {
                        Assert.Equal("Picture", f.Name);
                        Assert.Equal(FieldType.VARBINARY, f.Type);
                        Assert.Equal(8192, f.Size.Size);
                        Assert.Equal(0, f.Size.Precision);
                        Assert.Null(f.DefaultExpression);
                        Assert.False(f.Nullable);
                        Assert.False(f.Computed);
                    }, f =>
                    {
                        Assert.Equal("CV", f.Name);
                        Assert.Equal(FieldType.VARBINARY, f.Type);
                        Assert.True(f.Size.IsMax);
                        Assert.Null(f.DefaultExpression);
                        Assert.False(f.Nullable);
                        Assert.False(f.Computed);
                    }, f =>
                    {
                        Assert.Equal("RAM4", f.Name);
                        Assert.Equal(FieldType.BIGINT, f.Type);
                        Assert.Null(f.DefaultExpression);
                        Assert.False(f.Nullable);
                        Assert.False(f.Computed);
                    }, f =>
                    {
                        Assert.Equal("Price", f.Name);
                        Assert.Equal(FieldType.DECIMAL, f.Type);
                        Assert.Equal(19, f.Size.Size);
                        Assert.Equal(4, f.Size.Precision);
                        Assert.Null(f.DefaultExpression);
                        Assert.False(f.Nullable);
                        Assert.False(f.Computed);
                    });
                    Assert.Collection(e.PrimaryKey, fr =>
                    {
                        Assert.Equal("UserId", fr.FieldName);
                        Assert.True(fr.IsResolved);
                        Assert.Equal(e.Fields.Single(f => f.Name == "UserId"), fr.ResolvedField);
                    });
                    Assert.Collection(e.Mutations, m =>
                    {
                        Assert.Equal("Name", m.Name);
                        Assert.Collection(m.FieldReferences, fr =>
                        {
                            Assert.Equal("FirstName", fr.FieldName);
                            Assert.True(fr.IsResolved);
                            Assert.Equal(e.Fields.Single(f => f.Name == "FirstName"), fr.ResolvedField);
                        }, fr =>
                        {
                            Assert.Equal("LastName", fr.FieldName);
                            Assert.True(fr.IsResolved);
                            Assert.Equal(e.Fields.Single(f => f.Name == "LastName"), fr.ResolvedField);
                        });
                    }, m =>
                    {
                        Assert.Equal("ContactInformation", m.Name);
                        Assert.Collection(m.FieldReferences, fr =>
                        {
                            Assert.Equal("ContactEmail", fr.FieldName);
                            Assert.True(fr.IsResolved);
                            Assert.Equal(e.Fields.Single(f => f.Name == "ContactEmail"), fr.ResolvedField);
                        }, fr =>
                        {
                            Assert.Equal("ContactPhone", fr.FieldName);
                            Assert.True(fr.IsResolved);
                            Assert.Equal(e.Fields.Single(f => f.Name == "ContactPhone"), fr.ResolvedField);
                        });
                    }, m =>
                    {
                        Assert.Equal("Verified", m.Name);
                        Assert.Collection(m.FieldReferences, fr =>
                        {
                            Assert.Equal("Verified", fr.FieldName);
                            Assert.True(fr.IsResolved);
                            Assert.Equal(e.Fields.Single(f => f.Name == "Verified"), fr.ResolvedField);
                        });
                    });
                });
            },
                              s =>
            {
                Assert.Equal("tenant", s.Name);
                Assert.Collection(s.Entities, e =>
                {
                    Assert.Equal("Tenant", e.Name);
                    Assert.False(e.IsQuery);
                    Assert.Collection(e.Children, e =>
                    {
                        Assert.Equal("Task", e.Name);
                        Assert.True(e.IsQuery);
                        Assert.Collection(e.PrimaryKey, fr =>
                        {
                            Assert.Equal("TaskId", fr.FieldName);
                            Assert.Equal(e.Fields.Single(f => f.Name == "TaskId"), fr.ResolvedField);
                        });
                    }, e =>
                    {
                        Assert.Equal("Procedure", e.Name);
                        Assert.False(e.IsQuery);
                        Assert.Collection(e.Children, e =>
                        {
                            Assert.Equal("Revision", e.Name);
                            Assert.False(e.IsQuery);
                            Assert.Collection(e.Children, e =>
                            {
                                Assert.Equal("Execution", e.Name);
                                Assert.False(e.IsQuery);
                                Assert.Collection(e.Children, e =>
                                {
                                    Assert.Equal("Comment", e.Name);
                                    Assert.False(e.IsQuery);
                                    Assert.Collection(e.Children, e =>
                                    {
                                        Assert.Equal("Delivery", e.Name);
                                        Assert.False(e.IsQuery);
                                        Assert.Collection(e.References, r =>
                                        {
                                            Assert.Collection(r.Source, fr =>
                                            {
                                                Assert.Equal("UserId", fr.FieldName);
                                                Assert.True(fr.IsResolved);
                                                Assert.Equal(e.Fields.Single(f => f.Name == "UserId"), fr.ResolvedField);
                                            });
                                            Assert.Collection(r.Target, fr =>
                                            {
                                                Assert.Equal("UserId", fr.FieldName);
                                                Assert.True(fr.IsResolved);
                                                Assert.Equal(s.Ast.Locate("auth", new[] { "User" }).Fields.Single(f => f.Name == "UserId"), fr.ResolvedField);
                                            });
                                        });
                                        Assert.Collection(e.References, r =>
                                        {
                                            Assert.Collection(r.Source, fr =>
                                            {
                                                Assert.Equal("UserId", fr.FieldName);
                                                Assert.True(fr.IsResolved);
                                                Assert.Equal(e.Fields.Single(f => f.Name == "UserId"), fr.ResolvedField);
                                            });
                                            Assert.Collection(r.Target, fr =>
                                            {
                                                Assert.Equal("UserId", fr.FieldName);
                                                Assert.True(fr.IsResolved);
                                                Assert.Equal(s.Ast.Locate("auth", new[] { "User" }).Fields.Single(f => f.Name == "UserId"), fr.ResolvedField);
                                            });
                                        });
                                    }, e =>
                                    {
                                        Assert.Equal("Reaction", e.Name);
                                        Assert.False(e.IsQuery);
                                        Assert.Collection(e.References, r =>
                                        {
                                            Assert.Collection(r.Source, fr =>
                                            {
                                                Assert.Equal("UserId", fr.FieldName);
                                                Assert.True(fr.IsResolved);
                                                Assert.Equal(e.Fields.Single(f => f.Name == "UserId"), fr.ResolvedField);
                                            });
                                            Assert.Collection(r.Target, fr =>
                                            {
                                                Assert.Equal("UserId", fr.FieldName);
                                                Assert.True(fr.IsResolved);
                                                Assert.Equal(s.Ast.Locate("auth", new[] { "User" }).Fields.Single(f => f.Name == "UserId"), fr.ResolvedField);
                                            });
                                        });
                                        Assert.Collection(e.UniqueKeys, k =>
                                        {
                                            Assert.Collection(k.FieldReferences, fr =>
                                            {
                                                Assert.Equal("CommentId", fr.FieldName);
                                                Assert.True(fr.IsResolved);
                                                Assert.Equal(e.Parent.Fields.Single(f => f.Name == "CommentId"), fr.ResolvedField);
                                            }, fr =>
                                            {
                                                Assert.Equal("Emoji", fr.FieldName);
                                                Assert.True(fr.IsResolved);
                                                Assert.Equal(e.Fields.Single(f => f.Name == "Emoji"), fr.ResolvedField);
                                            });
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        }
        public void ViewModelFieldConstructorEmptyTest()
        {
            var instance = new ModelFixture <string>();

            Assert.IsTrue(instance.Value is null, "Value should be null.");
        }