|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using Microsoft.Graph.ODataTemplateWriter.Extensions; |
| 5 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 6 | +using Vipr.Core; |
| 7 | +using Vipr.Core.CodeModel; |
| 8 | +using Vipr.Reader.OData.v4; |
| 9 | + |
| 10 | +namespace GraphODataTemplateWriter.Test |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Test GetServiceCollectionNavigationPropertyForPropertyType method |
| 14 | + /// |
| 15 | + /// We have revised the LINQ statement to support the following OData rules |
| 16 | + /// regarding containment: |
| 17 | + /// |
| 18 | + /// 1. If a navigation property specifies "ContainsTarget='true'", it is self-contained. |
| 19 | + /// Generate a direct path to the item (ie "parent/child"). |
| 20 | + /// 2. If a navigation property does not specify ContainsTarget but there is a defined EntitySet |
| 21 | + /// of the given type, it is a reference relationship. Generate a reference path to the item (ie "item/$ref"). |
| 22 | + /// 3. If a navigation property does not have a defined EntitySet but there is a Singleton which has |
| 23 | + /// a self-contained reference to the given type, we can make a relationship to the implied EntitySet of |
| 24 | + /// the singleton. Generate a reference path to the item (ie "singleton/item/$ref"). |
| 25 | + /// 4. If none of the above pertain to the navigation property, it should be treated as a metadata error. |
| 26 | + /// </summary> |
| 27 | + [TestClass] |
| 28 | + public class ContainmentTests |
| 29 | + { |
| 30 | + /// <summary> |
| 31 | + /// The object model of the test containment metadata |
| 32 | + /// </summary> |
| 33 | + public OdcmModel model; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// These tests load a test metadata file from the file system using VIPR's OData V4 reader |
| 37 | + /// </summary> |
| 38 | + [TestInitialize] |
| 39 | + public void Initialize() |
| 40 | + { |
| 41 | + string dir = Directory.GetCurrentDirectory(); |
| 42 | + dir = dir.Replace("\\bin\\Debug", ""); |
| 43 | + |
| 44 | + string edmx = File.ReadAllText(dir + "\\Edmx\\Containment.xml"); |
| 45 | + OdcmReader reader = new OdcmReader(); |
| 46 | + |
| 47 | + model = reader.GenerateOdcmModel(new List<TextFile> { new TextFile("$metadata", edmx) }); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Test an implicit entity set is found for a given navigation property without |
| 52 | + /// containment or an explicit entity set |
| 53 | + /// </summary> |
| 54 | + [TestMethod] |
| 55 | + public void TestImplicitEntitySet() |
| 56 | + { |
| 57 | + var type = model.GetEntityTypes().Where(t => t.Name == "testEntity").First(); |
| 58 | + var prop = type.Properties.Where(p => p.Name == "testNav").First(); |
| 59 | + |
| 60 | + OdcmProperty result = OdcmModelExtensions.GetServiceCollectionNavigationPropertyForPropertyType(prop); |
| 61 | + var singleton = model.GetEntityTypes().Where(t => t.Name == "testSingleton").First(); |
| 62 | + Assert.AreEqual(singleton.Name, result.Name); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Test that null is returned when there is no valid explicit or implicit entity set for a |
| 67 | + /// given navigation property |
| 68 | + /// </summary> |
| 69 | + [TestMethod] |
| 70 | + public void TestNoValidEntitySet() |
| 71 | + { |
| 72 | + var type = model.GetEntityTypes().Where(t => t.Name == "testEntity").First(); |
| 73 | + var prop = type.Properties.Where(p => p.Name == "testInvalidNav").First(); ; |
| 74 | + |
| 75 | + OdcmProperty result = OdcmModelExtensions.GetServiceCollectionNavigationPropertyForPropertyType(prop); |
| 76 | + Assert.IsNull(result); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Test that an explicit entity set is returned instead of an implicit entity set when both |
| 81 | + /// are present in the metadata |
| 82 | + /// </summary> |
| 83 | + [TestMethod] |
| 84 | + public void TestExplicitEntitySet() |
| 85 | + { |
| 86 | + var type = model.GetEntityTypes().Where(t => t.Name == "testEntity").First(); |
| 87 | + var prop = type.Properties.Where(p => p.Name == "testExplicitNav").First(); |
| 88 | + |
| 89 | + OdcmProperty result = OdcmModelExtensions.GetServiceCollectionNavigationPropertyForPropertyType(prop); |
| 90 | + |
| 91 | + var entitySet = model.EntityContainer.Properties.Where(t => t.Name == "testTypes").First(); |
| 92 | + Assert.AreEqual(entitySet.Name, result.Name); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments