Exemplo n.º 1
0
        public void TestBooleanConstant()
        {
            var constant = new EdmBooleanConstant(true);
            Assert.AreEqual(EdmValueKind.Boolean, constant.ValueKind, "Invalid value kind.");

            this.ValidateEdmValueKindRoundTrip(EdmValueKind.Boolean, EdmCoreModel.Instance.GetBoolean(true), constant);
        }
Exemplo n.º 2
0
        public static void SetComputedAnnotation(EdmModel model, IEdmProperty target)
        {
            EdmUtil.CheckArgumentNull(model, "model");
            EdmUtil.CheckArgumentNull(target, "target");

            IEdmBooleanConstantExpression val = new EdmBooleanConstant(true);
            IEdmValueTerm term = CoreVocabularyModel.ComputedTerm;

            Debug.Assert(term != null, "term!=null");
            EdmAnnotation annotation = new EdmAnnotation(target, term, val);
            annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
            model.SetVocabularyAnnotation(annotation);
        }
        public void EdmBooleanConstant()
        {
            var e = new EdmBooleanConstant(true);
            Assert.AreEqual(EdmExpressionKind.BooleanConstant, e.ExpressionKind, "e.ExpressionKind");
            Assert.IsNull(e.Type, "e.Type");
            Assert.AreEqual(true, e.Value, "e.Value");

            e = new EdmBooleanConstant(EdmCoreModel.Instance.GetBinary(true, null, true), false);
            Assert.AreEqual(true, e.Type.IsNullable, "e.Type.IsNullable");
            Assert.AreEqual(true, e.Type.AsBinary().IsUnbounded, "e.Type.AsBinary().isUnbounded");
            Assert.AreEqual(false, e.Value, "e.Value");

            e = new EdmBooleanConstant(null, true);
            Assert.IsNull(e.Type, "e.Type");

            Assert.IsFalse(e.IsBad(), "Expression not bad.");
            Assert.AreEqual(0, e.Errors().Count(), "Expression has no errors");
        }
Exemplo n.º 4
0
 private static void SetComputedAnnotation(EdmModel model, IEdmProperty target)
 {
     // when 'target' is <Key> property, V4's 'Computed' also has the meaning of OData V3's 'Identity'.
     var val = new EdmBooleanConstant(value: true);
     var annotation = new EdmAnnotation(target, CoreVocabularyModel.ComputedTerm, val);
     annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
     model.SetVocabularyAnnotation(annotation);
 }
Exemplo n.º 5
0
 private static void SetCapabilitiesAnnotation(this EdmModel model, IEdmVocabularyAnnotatable target, IEdmValueTerm term, bool value)
 {
     var expression = new EdmBooleanConstant(value);
     var annotation = new EdmAnnotation(target, term, expression);
     annotation.SetSerializationLocation(model, target.ToSerializationLocation());
     model.AddVocabularyAnnotation(annotation);
 }
Exemplo n.º 6
0
        public void TestCoreIsLanguageDependentAnnotation()
        {
            EdmModel model = new EdmModel();

            EdmEntityContainer container = new EdmEntityContainer("Ns", "Container");
            var personType = new EdmEntityType("Ns", "Person");
            var propertyId = personType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false));
            personType.AddKeys(propertyId);
            var structuralProperty = personType.AddStructuralProperty("Concurrency", EdmCoreModel.Instance.GetInt32(true));
            model.AddElement(personType);
            var entitySet = container.AddEntitySet("People", personType);

            var stringTerm = new EdmTerm("Ns", "HomeAddress", EdmCoreModel.Instance.GetString(true));
            model.AddElement(stringTerm);
            model.AddElement(container);

            IEdmValueTerm term = CoreVocabularyModel.IsLanguageDependentTerm;
            IEdmBooleanConstantExpression trueExpression = new EdmBooleanConstant(true);
            IEdmBooleanConstantExpression falseExpression = new EdmBooleanConstant(false);

            EdmAnnotation personIdAnnotation = new EdmAnnotation(propertyId, term, trueExpression);
            personIdAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
            model.SetVocabularyAnnotation(personIdAnnotation);

            EdmAnnotation structuralPropertyAnnotation = new EdmAnnotation(structuralProperty, term, falseExpression);
            structuralPropertyAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
            model.SetVocabularyAnnotation(structuralPropertyAnnotation);

            EdmAnnotation stringTermAnnotation = new EdmAnnotation(stringTerm, term, trueExpression);
            stringTermAnnotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
            model.SetVocabularyAnnotation(stringTermAnnotation);

            var idAnnotationValue = (model.FindVocabularyAnnotations<IEdmValueAnnotation>(propertyId, term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value;
            var structuralPropertyAnnotationValue = (model.FindVocabularyAnnotations<IEdmValueAnnotation>(structuralProperty, term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value;
            var stringTermAnnotationValue = (model.FindVocabularyAnnotations<IEdmValueAnnotation>(stringTerm, term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value;
            var entitySetAnnotation = model.FindVocabularyAnnotations<IEdmValueAnnotation>(entitySet, term).FirstOrDefault();

            Assert.AreEqual(true, idAnnotationValue);
            Assert.AreEqual(false, structuralPropertyAnnotationValue);
            Assert.AreEqual(true, stringTermAnnotationValue);
            Assert.IsNull(entitySetAnnotation);

            const string expected = @"<?xml version=""1.0"" encoding=""utf-16""?>
<Schema Namespace=""Ns"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
  <EntityType Name=""Person"">
    <Key>
      <PropertyRef Name=""Id"" />
    </Key>
    <Property Name=""Id"" Type=""Edm.Int32"" Nullable=""false"">
      <Annotation Term=""Org.OData.Core.V1.IsLanguageDependent"" Bool=""true"" />
    </Property>
    <Property Name=""Concurrency"" Type=""Edm.Int32"">
      <Annotation Term=""Org.OData.Core.V1.IsLanguageDependent"" Bool=""false"" />
    </Property>
  </EntityType>
  <Term Name=""HomeAddress"" Type=""Edm.String"">
    <Annotation Term=""Org.OData.Core.V1.IsLanguageDependent"" Bool=""true"" />
  </Term>
  <EntityContainer Name=""Container"">
    <EntitySet Name=""People"" EntityType=""Ns.Person"" />
  </EntityContainer>
</Schema>";

            IEnumerable<EdmError> errors;
            StringWriter sw = new StringWriter();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.Encoding = System.Text.Encoding.UTF8;
            XmlWriter xw = XmlWriter.Create(sw, settings);
            model.TryWriteCsdl(xw, out errors);
            xw.Flush();
            xw.Close();
            var actual = sw.ToString();

            Assert.AreEqual(expected, actual);

            IEdmModel parsedModel;
            IEnumerable<EdmError> errs;
            bool parsed = CsdlReader.TryParse(new XmlReader[] { XmlReader.Create(new StringReader(expected)) }, out parsedModel, out errs);
            Assert.IsTrue(parsed, "parsed");
            Assert.IsTrue(!errors.Any(), "No errors");

            var idAnnotation = parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(propertyId, term).FirstOrDefault();
            var propertyAnnotation = parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(structuralProperty, term).FirstOrDefault();
            var termAnnotation = parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(stringTerm, term).FirstOrDefault();
            entitySetAnnotation = parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(entitySet, term).FirstOrDefault();

            Assert.AreEqual(null, propertyAnnotation);
            Assert.AreEqual(null, idAnnotation);
            Assert.AreEqual(null, termAnnotation);
            Assert.AreEqual(null, entitySetAnnotation);

            var parsedPersonType = parsedModel.FindType("Ns.Person") as IEdmEntityType;
            idAnnotationValue = (parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(parsedPersonType.FindProperty("Id"), term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value;
            structuralPropertyAnnotationValue = (parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(parsedPersonType.FindProperty("Concurrency"), term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value;
            stringTermAnnotationValue = (parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(parsedModel.FindValueTerm("Ns.HomeAddress"), term).FirstOrDefault().Value as IEdmBooleanConstantExpression).Value;
            entitySetAnnotation = parsedModel.FindVocabularyAnnotations<IEdmValueAnnotation>(parsedModel.FindDeclaredEntitySet("People"), term).FirstOrDefault();

            Assert.IsNotNull(parsedPersonType);
            Assert.AreEqual(false, structuralPropertyAnnotationValue);
            Assert.AreEqual(true, idAnnotationValue);
            Assert.AreEqual(true, stringTermAnnotationValue);
            Assert.IsNull(entitySetAnnotation);
        }