public void GetCalculatedFieldMetadata_Error_FieldHasNoCalculation()
        {
            long       fieldId;
            Field      field;
            EntityType type;
            Mock <IEntityRepository>        mockRepository;
            Mock <IExpressionCompiler>      mockCompiler;
            Mock <IExpression>              mockExpression;
            CalculatedFieldMetadataProvider provider;

            // Mock calculated field
            field = new StringField().As <Field>();
            type  = new EntityType();
            field.FieldIsOnType = type;
            fieldId             = field.Id;

            // Mock repository (loading field data)
            mockRepository = new Mock <IEntityRepository>(MockBehavior.Strict);
            mockRepository
            .Setup(r => r.Get <Field>(It.Is <IEnumerable <long> >(fields => fields.Count() == 1 && fields.First() == fieldId), It.IsAny <string>()))
            .Returns(new Field[] { field });

            // Mock compiler (compiling the calculation)
            mockExpression = new Mock <IExpression>();
            mockCompiler   = new Mock <IExpressionCompiler>(MockBehavior.Strict);

            // Get provider
            provider = new CalculatedFieldMetadataProvider(mockCompiler.Object, mockRepository.Object);

            // Run the test
            Assert.Throws <ArgumentException>(() => provider.GetCalculatedFieldMetadata(new[] { fieldId }, CalculatedFieldSettings.Default));
        }
        public void GetCalculatedFieldMetadata_ParseException()
        {
            long       fieldId;
            Field      field;
            string     calculation;
            EntityType type;
            Mock <IEntityRepository>        mockRepository;
            Mock <IExpressionCompiler>      mockCompiler;
            Mock <IExpression>              mockExpression;
            CalculatedFieldMetadataProvider provider;
            IReadOnlyCollection <CalculatedFieldMetadata> actualResult;
            CalculatedFieldMetadata metadata;
            ParseException          exception = new ParseException("Test error");

            // Mock calculated field
            calculation            = "Name + 'Hello'";
            field                  = new StringField().As <Field>();
            type                   = new EntityType();
            field.FieldIsOnType    = type;
            field.FieldCalculation = calculation;
            fieldId                = field.Id;

            // Mock repository (loading field data)
            mockRepository = new Mock <IEntityRepository>(MockBehavior.Strict);
            mockRepository
            .Setup(r => r.Get <Field>(It.Is <IEnumerable <long> >(fields => fields.Count() == 1 && fields.First() == fieldId), It.IsAny <string>()))
            .Returns(new Field[] { field });

            // Mock compiler (compiling the calculation)
            mockExpression = new Mock <IExpression>();
            mockCompiler   = new Mock <IExpressionCompiler>(MockBehavior.Strict);
            mockCompiler
            .Setup(c => c.Compile(calculation, It.IsAny <BuilderSettings>()))
            .Returns(() => { throw exception; });

            // Get provider
            provider = new CalculatedFieldMetadataProvider(mockCompiler.Object, mockRepository.Object);

            // Run the test
            actualResult = provider.GetCalculatedFieldMetadata(new[] { fieldId }, CalculatedFieldSettings.Default);

            // Verify
            Assert.That(actualResult, Is.Not.Null);
            Assert.That(actualResult.Count, Is.EqualTo(1));
            metadata = actualResult.First();
            Assert.That(metadata, Is.Not.Null);
            Assert.That(metadata.CalculatedFieldId, Is.EqualTo(fieldId));
            Assert.That(metadata.Expression, Is.Null);
            Assert.That(metadata.Exception, Is.EqualTo(exception));
            Assert.That(metadata.Calculation, Is.EqualTo(calculation));

            mockRepository.VerifyAll();
            mockCompiler.VerifyAll();
        }
        public void GetCalculatedFieldMetadata_Error_NotAField()
        {
            Mock <IExpressionCompiler>      mockCompiler;
            CalculatedFieldMetadataProvider provider;

            // Mock compiler (compiling the calculation)
            mockCompiler = new Mock <IExpressionCompiler>(MockBehavior.Strict);

            // Get provider
            provider = new CalculatedFieldMetadataProvider(mockCompiler.Object, Factory.EntityRepository);

            // Run the test
            Assert.Throws <ArgumentException>(() => provider.GetCalculatedFieldMetadata(new long[] { 1 }, CalculatedFieldSettings.Default));
        }
        public void GetCalculatedFieldMetadata_Error_NullFieldList()
        {
            Mock <IEntityRepository>        mockRepository;
            Mock <IExpressionCompiler>      mockCompiler;
            CalculatedFieldMetadataProvider provider;

            // Mock repository (loading field data)
            mockRepository = new Mock <IEntityRepository>(MockBehavior.Strict);

            // Mock compiler (compiling the calculation)
            mockCompiler = new Mock <IExpressionCompiler>(MockBehavior.Strict);

            // Get provider
            provider = new CalculatedFieldMetadataProvider(mockCompiler.Object, mockRepository.Object);

            // Run the test
            Assert.Throws <ArgumentNullException>(() => provider.GetCalculatedFieldMetadata(null, CalculatedFieldSettings.Default));

            // Verify
            mockRepository.VerifyAll();
            mockCompiler.VerifyAll();
        }