public void Write_Writes_Out_The_Vna_File(string inFileName, string outFileName, int defaultNodeMapIndex, int defaultEdgeMapIndex, bool exportNodeData, bool exportEdgeData, bool isDirected)
        {
            var basicAdjListWriter = new VNABasicAdjListWriter();
            IBasicAdjList net = null;
            using (var reader = new VnaAdjListReader())
            {
                reader.File = Path.Combine(_dataDir, inFileName);
                reader.Read();
                net = reader.Network as IBasicAdjList;
            }

            string outputFile = Path.Combine(_testDir, outFileName);

            basicAdjListWriter.NodeIdentifierAttributeIndex = defaultNodeMapIndex;
            basicAdjListWriter.EdgeIdentifierAttributeIndex = defaultEdgeMapIndex;
            basicAdjListWriter.ExportNodeAttributes = exportNodeData;
            basicAdjListWriter.ExportEdgeAttributes = exportEdgeData;
            basicAdjListWriter.FileName = outputFile;
            basicAdjListWriter.InputNetwork = net;

            basicAdjListWriter.Write();

            bool exists = File.Exists(outputFile);
            Assert.True(exists);
        }
Exemplo n.º 2
0
 public void Clean_Removes_Empty_Strings_From_Fields(int id, string [] inputs, string[] expectedResults)
 {
     var reader = new VnaAdjListReader();
     var fields = reader.Clean(inputs);
     Assert.NotNull(fields);
     Assert.Equal(expectedResults.Length, fields.Length);
     for (int i = 0; i < expectedResults.Length; i++)
     {
         Assert.Equal(expectedResults[i], fields[i]);
     }
 }
Exemplo n.º 3
0
        public void Reads_With_Items_That_Have_Known_Data_Field_Format_Issues(int testItemId)
        {
            // test items with known issues!
            var item = _data.TestItemsWithKnownErrors[testItemId];
            var reader = new VnaAdjListReader();
            reader.File = Path.Combine(_dataPath, item.FileName);
            reader.Read();

            var result = reader.Network as IBasicAdjList;
            Assert.Null(result);
            Assert.True(reader.ErrorsHaveOccurred);
        }
Exemplo n.º 4
0
        public void CheckForAndManageNodeSizeCase_Adds_Node_Height_Attrib_If_Size_Is_Found(int index, string[] headerNames, string[] expectedHeaderNameOrder)
        {
            // assumes size has been identified but changed to Constant.NodeWidth already
            var nodePropertyHeaders = new List<HeaderField>();
            // simulate already constructed list of NodePropertyHeaders
            for (int i = 0; i < headerNames.Length; i++)
            {
                var name = headerNames[i];
                nodePropertyHeaders.Add(new HeaderField(name, VnaFileSection.NodeProperties, i, null));
            }

            var reader = new VnaAdjListReader();
            reader.CheckForAndManageNodeSizeCase(nodePropertyHeaders);

            Assert.Equal(expectedHeaderNameOrder.Length, nodePropertyHeaders.Count);
            for(int i=0; i<expectedHeaderNameOrder.Length; i++)
            {
                Assert.Equal(expectedHeaderNameOrder[i], nodePropertyHeaders[i].AttribColName);
            }
        }
Exemplo n.º 5
0
        public void CreateNodeAttribs_Adds_Cols_To_NodeAttribs_Based_On_Headers_Passed_In(int index, string[] headerNames, Type[] dataTypes, HeaderMatchType[] matchTypes)
        {
            var net = Blob.TestSupport.Network.BasicAdjListGenerator.GenerateAdjListWithDataAttribs(3, 0, false, true, false);
            var headers = new List<HeaderField>();
            for (int i = 0; i < headerNames.Length; i++)
            {
                var name = headerNames[i];
                headers.Add(new HeaderField(name, VnaFileSection.NodeData, i, null) {AttribColumnType = dataTypes[i]});
            }

            Assert.Equal(0, net.NodeDataAttributeCount);

            var reader = new VnaAdjListReader();
            reader.CreateNodeAttribs(net, headers);

            Assert.Equal(headerNames.Length, net.NodeDataAttributeCount);
            for (int i = 0; i < dataTypes.Length; i++)
            {
                Assert.Equal(dataTypes[i], net.NodeDataAttributeTypes[i]);
                Assert.Equal(headerNames[i], net.NodeDataAttributeNames[i]);
            }
        }
Exemplo n.º 6
0
        public void Read_Input_File_And_Verify_NodeAttribs_From_NodeProperties_Section(int testItemId)
        {
            _vnaReader = new VnaAdjListReader();
            var item = _data.TestItems[testItemId];
            var reader = new VnaAdjListReader();
            reader.File = Path.Combine(_dataPath, item.FileName);
            reader.Read();

            var result = reader.Network as IBasicAdjList;
            Assert.NotNull(result);

            bool containsName = false;
            bool matchesType = false;
            Type tmpType = null;
            for (int i = 0; i < item.NodePropertyHeaderCountExpected; i++)
            {
                for (int j = 0; j < result.NodeDataAttributeCount; j++)
                {
                    if (item.NodePropertyHeadersExpected[i] == result.NodeDataAttributeNames[j])
                    {
                        // find the expected header name in the NodeData and match its expcted type
                        containsName = true;
                        matchesType = VnaFileTestItem.IsMatch(item.NodePropertyTypesExpected[i], result.NodeDataAttributeTypes[j]);
                        tmpType = result.NodeDataAttributeTypes[j];
                        break;
                    }
                }

                Assert.True(containsName);
                Assert.True(matchesType);
                containsName = false;
                matchesType = false;
            }
        }
Exemplo n.º 7
0
 public void ReadNodePropertyFields_Throws_If_Not_In_Required_State(VnaFileSection state)
 {
     var reader = new VnaAdjListReader();
     reader.State = state;
     var ex = Assert.Throws<InvalidOperationException>(() => reader.ReadNodePropertyFields(new string[] { "X" }, null, -1, null));
 }
Exemplo n.º 8
0
        public void ReadNodeDataFields_Maps_First_Field_As_Key_To_Node(int index, string[] dataFields, string[] dataFieldNames, Type[] dataTypes, int[] srcFieldIndices)
        {
            // simulate initialized network
            var net = Blob.TestSupport.Network.BasicAdjListGenerator.GenerateAdjListWithDataAttribs(3, 0, false, true, false);
            var builder = new Blob.TestSupport.Network.DataAttributesBuilder();
            builder.AddColumns((INodeAttributes)net, dataTypes, dataFieldNames, false);

            var reader = new VnaAdjListReader();
            reader.State = VnaFileSection.NodeData;
            // simulate the list of HeaderField objects that have already been created in 1st pass
            for (int i = 0; i < dataFields.Length; i++)
                reader.NodeDataHeaders.Add(new HeaderField(dataFieldNames[i], VnaFileSection.NodeData, srcFieldIndices[i], null) { AttribColumnIndex = srcFieldIndices[i] });

            int targetNodeIndex = 1;
            reader.ReadNodeDataFields(dataFields, net, targetNodeIndex, reader.NodeDataHeaders);

            Assert.Equal(1, reader.NodeKeyToNodeIndexMap.Count);
            INode node = net.Nodes[targetNodeIndex];
            Assert.True(reader.NodeKeyToNodeIndexMap.ContainsKey(dataFields[0]));
            Assert.Equal(node, reader.NodeKeyToNodeIndexMap[dataFields[0]]);
        }
Exemplo n.º 9
0
        public void ReadEdgePropertyFields_Reads_Fields_Into_EdgeAttribs_From_StarEdgeData_Section(int index, int nodeCount, string[] dataFields, int[] nodeIndices, string[] dataFieldNames, Type[] dataTypes, int[] srcFieldIndices)
        {
            // simulate initialized network
            var net = Blob.TestSupport.Network.BasicAdjListGenerator.GenerateAdjListWithDataAttribs(nodeCount, 0, false, true, true);
            var builder = new Blob.TestSupport.Network.DataAttributesBuilder();
            builder.AddColumns((IEdgeAttributes)net, dataTypes, dataFieldNames, false);

            var reader = new VnaAdjListReader();
            reader.State = VnaFileSection.EdgeData;
            // simulate already the populated NodeKeyToNodeIndexMap
            INode nodeA = net.Nodes[nodeIndices[0]];
            INode nodeB = net.Nodes[nodeIndices[1]];
            reader.NodeKeyToNodeIndexMap.Add(dataFields[0], nodeA);
            reader.NodeKeyToNodeIndexMap.Add(dataFields[1], nodeB);

            // simulate the list of HeaderField objects that have already been created in 1st pass
            for (int i = 0; i < dataFields.Length; i++)
                reader.EdgeDataHeaders.Add(new HeaderField("f-" + i.ToString(), VnaFileSection.EdgeData, i, null) { AttribColumnIndex = i });

            Assert.Equal(0, net.EdgeCount);
            reader.ReadEdgeDataFields(dataFields, net, reader.EdgeDataHeaders);

            IEdge owner = net.Edges[0];
            for (int i = 2; i < dataTypes.Length; i++)
            {
                if (dataTypes[i] == typeof(string))
                {
                    var val = net.EdgeData.GetValue<string>(owner, i);
                    Assert.Equal(dataFields[i], val);
                }
                else if (dataTypes[i] == typeof(int))
                {
                    var val = net.EdgeData.GetValue<int>(owner, i);
                    Assert.Equal(Convert.ToInt32(dataFields[i]), val);
                }
                else if (dataTypes[i] == typeof(float))
                {
                    var val = net.EdgeData.GetValue<float>(owner, i);
                    Assert.Equal(Convert.ToSingle(dataFields[i]), val);
                }
                else if (dataTypes[i] == typeof(double))
                {
                    var val = net.EdgeData.GetValue<double>(owner, i);
                    Assert.Equal(Convert.ToDouble(dataFields[i]), val);
                }
                else
                {
                    Assert.True(false); //force a fail
                }
            }
        }
Exemplo n.º 10
0
        public void RemoveDuplicatesFromSecondList_Removes_Duplicates_From_Second_List( int index, string[] inputs1, string[] inputs2, string[] expectedResults2)
        {
            var reader = new VnaAdjListReader();
            // build test lists, Section and index do not matter
            var list1 = new List<HeaderField>();
            var list2 = new List<HeaderField>();
            for (int i = 0; i < inputs1.Length; i++)
                list1.Add(new HeaderField(inputs1[i], VnaFileSection.None, -1, null));
            for (int i = 0; i < inputs2.Length; i++)
                list2.Add(new HeaderField(inputs2[i], VnaFileSection.None, -1, null));
            reader.RemoveDuplicatesFromSecondList(list1, list2);

            Assert.Equal(expectedResults2.Length, list2.Count);
            for (int i = 0; i < expectedResults2.Length; i++)
            {
                Assert.Equal(expectedResults2[i], list2[i].AttribColName);
            }
        }
Exemplo n.º 11
0
        public void Read_With_Items_That_Have_Known_Data_Field_Format_Issues_With_DisableDataConversions_Set_To_True(int testItemId)
        {
            // test items with known issues!
            _vnaReader = new VnaAdjListReader();
            var item = _data.TestItemsWithKnownErrors[testItemId];
            var reader = new VnaAdjListReader();
            reader.DisableDataConversions = true;
            reader.File = Path.Combine(_dataPath, item.FileName);
            reader.Read();

            var result = reader.Network as IBasicAdjList;
            Assert.NotNull(result);
            Assert.False(reader.ErrorsHaveOccurred);

            Assert.Equal(item.NodeCount, result.NodeCount);
            Assert.Equal(item.EdgeCount, result.EdgeCount);
        }
Exemplo n.º 12
0
        public void Read_Input_File_And_Verify_Random_Id_Assigned(int testItemId)
        {
            _vnaReader = new VnaAdjListReader();
            var item = _data.TestItems[testItemId];
            var reader = new VnaAdjListReader();
            reader.File = Path.Combine(_dataPath, item.FileName);
            reader.Read();

            var result = reader.Network;
            Assert.NotEqual(Guid.Empty, result.Id);
        }
Exemplo n.º 13
0
        public void Read_Input_File_And_Verify_NodeCount(int testItemId)
        {
            _vnaReader = new VnaAdjListReader();
            var item = _data.TestItems[testItemId];
            var reader = new VnaAdjListReader();
            reader.File = Path.Combine(_dataPath, item.FileName);
            reader.Read();

            var result = reader.Network;
            Assert.NotNull(result);
            Assert.Equal(item.NodeCount,  result.NodeCount);
        }
Exemplo n.º 14
0
        public void Read_Input_File_And_Verify_NodeAttribs_Has_Expected_Count(int testItemId)
        {
            _vnaReader = new VnaAdjListReader();
            var item = _data.TestItems[testItemId];
            var reader = new VnaAdjListReader();
            reader.File = Path.Combine(_dataPath, item.FileName);
            reader.Read();

            var result = reader.Network as IBasicAdjList;
            Assert.NotNull(result);

            int expectedCount = item.NodeDataHeaderCountExpected + item.NodePropertyHeaderCountExpected;
            Assert.Equal(expectedCount, result.NodeDataAttributeCount);
        }
Exemplo n.º 15
0
        public void DoFirstPass_With_Parser_And_Verify_NodePropertyHeaders(int testItemId)
        {
            // only test items with 0 known errors
            var item = _data.TestItems[testItemId];
            var inputFile = Path.Combine(_dataPath, item.FileName);

            var reader = new VnaAdjListReader();
            int nodeCount = -1;
            int edgeCount = -1;
            using (var sr = new StreamReader(inputFile))
            {
                using (var parser = new TextFieldParser(sr))
                {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.Delimiters = new string[] { ",", " ", "\t" };
                    parser.HasFieldsEnclosedInQuotes = true;
                    parser.TrimWhiteSpace = true;
                    reader.DoFirstPass(sr, parser, out nodeCount, out edgeCount);
                }
            }

            Assert.NotNull(reader.NodePropertyHeaders);
            Assert.Equal(item.NodePropertyHeaderCount, reader.NodePropertyHeaders.Count);

            for (int i = 0; i < reader.NodePropertyHeaders.Count; i++)
            {
                Assert.Equal(item.NodePropertyHeaders[i], reader.NodePropertyHeaders[i].AttribColName);
            }
        }
Exemplo n.º 16
0
        public void ReadEdgePropertyFields_Creates_An_Edge(int index, int nodeCount, string[] dataFields, int[] nodeIndices, string[] dataFieldNames, Type[] dataTypes, int[] srcFieldIndices)
        {
            // simulate initialized network
            var net = Blob.TestSupport.Network.BasicAdjListGenerator.GenerateAdjListWithDataAttribs(nodeCount, 0, false, false, true);
            var builder = new Blob.TestSupport.Network.DataAttributesBuilder();
            builder.AddColumns((IEdgeAttributes)net, dataTypes, dataFieldNames, false);

            // get actual nodes
            INode nodeA = net.Nodes[nodeIndices[0]];
            INode nodeB = net.Nodes[nodeIndices[1]];

            var reader = new VnaAdjListReader();
            reader.State = VnaFileSection.EdgeData;
            // simulate already the populated NodeKeyToNodeIndexMap
            reader.NodeKeyToNodeIndexMap.Add(dataFields[0], nodeA);
            reader.NodeKeyToNodeIndexMap.Add(dataFields[1], nodeB);

            // simulate the list of HeaderField objects that have already been created in 1st pass
            for (int i = 0; i < dataFields.Length;i++ )
                reader.EdgeDataHeaders.Add(new HeaderField("f-" + i.ToString(), VnaFileSection.EdgeData, i, null) { AttribColumnIndex = i });

            Assert.Equal(0, net.EdgeCount);
            reader.ReadEdgeDataFields(dataFields, net, reader.EdgeDataHeaders);

            Assert.Equal(1, net.EdgeCount);
            IEdge edge = net.Edges[0];
            Assert.Same(nodeA, edge.SourceNode);
            Assert.Same(nodeB, edge.DestinationNode);
        }
Exemplo n.º 17
0
        public void RemoveDuplicatesFromSecondList_Removes_Duplicates_From_Second_List_Using_Node_Headers(int testItemId)
        {
            var item = _data.TestItems[testItemId];

            var reader = new VnaAdjListReader();
            // build test lists, Section and index do not matter
            var list1 = new List<HeaderField>();
            var list2 = new List<HeaderField>();

            for (int i = 0; i < item.NodeDataHeadersExpected.Length; i++)
                list1.Add(new HeaderField(item.NodeDataHeadersExpected[i], VnaFileSection.None, -1, null));
            for (int i = 0; i < item.NodePropertyHeadersExpected.Length; i++)
                list2.Add(new HeaderField(item.NodePropertyHeadersExpected[i], VnaFileSection.None, -1, null));
            reader.RemoveDuplicatesFromSecondList(list1, list2);

            Assert.Equal(item.NodePropertyHeadersExpected.Length, list2.Count);
        }
Exemplo n.º 18
0
        public void ReadHeadersIntoTargetList_Populates_List_Of_Header_Fields(int index, string[] inputs, string[] expectedResults, int[] expectedIndices, VnaFileSection state)
        {
            var reader = new VnaAdjListReader();
            reader.State = state;
            var target = new List<HeaderField>();
            var count = reader.ReadHeadersIntoTargetList(inputs, target);

            Assert.Equal(expectedResults.Length, count);
            Assert.Equal(expectedResults.Length, target.Count);
            for (int i = 0; i < expectedResults.Length; i++)
            {
                Assert.False(string.IsNullOrWhiteSpace(target[i].AttribColName));
                Assert.Equal(expectedResults[i], target[i].AttribColName);
                Assert.Equal(state, target[i].Section);
                Assert.Equal(expectedIndices[i], target[i].SourceFieldIndex);
            }
        }
Exemplo n.º 19
0
 public void ConvertToBool_Safely_Converts_VNA_Active_Strings_To_Boolean(string input, bool expectedResult)
 {
     var reader = new VnaAdjListReader();
     var result = reader.ConvertToBool(input);
     Assert.Equal(expectedResult, result);
 }
Exemplo n.º 20
0
        public void ReadHeaders_Populates_NodeAttribHeaders_List_If_State_Is_NodeProperties(int id, string[] inputs, string[] expectedResults)
        {
            var reader = new VnaAdjListReader();
            Assert.Equal(0, reader.NodeDataHeaders.Count);
            Assert.Equal(0, reader.EdgeDataHeaders.Count);
            Assert.Equal(0, reader.NodePropertyHeaders.Count);
            Assert.Equal(0, reader.EdgePropertyHeaders.Count);

            reader.ReadHeaders(inputs, VnaFileSection.NodeProperties);

            Assert.Equal(0, reader.NodeDataHeaders.Count);
            Assert.Equal(0, reader.EdgeDataHeaders.Count);
            Assert.Equal(expectedResults.Length, reader.NodePropertyHeaders.Count);
            Assert.Equal(0, reader.EdgePropertyHeaders.Count);

            for (int i = 0; i < expectedResults.Length; i++)
            {
                Assert.False(string.IsNullOrWhiteSpace(reader.NodePropertyHeaders[i].AttribColName));
                Assert.Equal(expectedResults[i], reader.NodePropertyHeaders[i].AttribColName);
            }
        }
Exemplo n.º 21
0
        public void TranslateNodeHeaderNames_For_NodeDataHeaders(int index, string[] srcHeaderNames, string[] expectedNames)
        {
            var mockHelper = new Mock<IKnownHeaderNamesReaderHelper>();
            var net = Blob.TestSupport.Network.BasicAdjListGenerator.GenerateAdjListWithDataAttribs(3, 0, false, true, false);
            var headers = new List<HeaderField>();
            for (int i = 0; i < srcHeaderNames.Length; i++)
            {
                var name = srcHeaderNames[i];
                headers.Add(new HeaderField(null, VnaFileSection.NodeData, i, name));
                var expectedName = expectedNames[i];
                mockHelper.Setup(m => m.GetKnownNodeHeader(name)).Returns(new HeaderMatch(HeaderMatchType.NodeStyle) {NameToUse = expectedName, DataType = typeof(Guid)});
            }

            var reader = new VnaAdjListReader();
            reader.DisableDataConversions = true;
            reader.TranslateNodeHeaderNames(headers, mockHelper.Object);

            for (int i = 0; i < headers.Count; i++)
            {
                Assert.Equal(expectedNames[i], headers[i].AttribColName);
                Assert.Equal(typeof(Guid), headers[i].AttribColumnType);
            }
        }
Exemplo n.º 22
0
        public void ReadNodePropertyFields_Reads_Fields_Into_NodeAttribs_From_StarNodeData_Section(int index, string[] dataFields, string[] dataFieldNames, Type[] dataTypes, int[] srcFieldIndices)
        {
            int nodeCount = 3;
            // simulate initialized network
            var net = Blob.TestSupport.Network.BasicAdjListGenerator.GenerateAdjListWithDataAttribs(nodeCount, 0, false, true, false);
            var builder = new Blob.TestSupport.Network.DataAttributesBuilder();
            builder.AddColumns((INodeAttributes)net, dataTypes, dataFieldNames, false);

            var reader = new VnaAdjListReader();
            reader.State = VnaFileSection.NodeProperties;
            // simulate the list of HeaderField objects that would have been created in 1st pass
            for (int i = 0; i < dataFields.Length; i++)
                reader.NodePropertyHeaders.Add(new HeaderField(dataFieldNames[i], VnaFileSection.NodeProperties, srcFieldIndices[i], null) { AttribColumnIndex = srcFieldIndices[i] });

            int targetNodeIndex = 1;
            INode owner = net.Nodes[targetNodeIndex];
            reader.ReadNodePropertyFields(dataFields, net, targetNodeIndex, reader.NodePropertyHeaders);

            for (int i = 0; i < dataTypes.Length; i++)
            {
                if (dataTypes[i] == typeof(string))
                {
                    var val = net.NodeData.GetValue<string>(owner, i);
                    Assert.Equal(dataFields[i], val);
                }
                else if (dataTypes[i] == typeof(int))
                {
                    var val = net.NodeData.GetValue<int>(owner, i);
                    Assert.Equal(Convert.ToInt32(dataFields[i]), val);
                }
                else if (dataTypes[i] == typeof(float))
                {
                    var val = net.NodeData.GetValue<float>(owner, i);
                    Assert.Equal(Convert.ToSingle(dataFields[i]), val);
                }
                else if (dataTypes[i] == typeof(double))
                {
                    var val = net.NodeData.GetValue<double>(owner, i);
                    Assert.Equal(Convert.ToDouble(dataFields[i]), val);
                }
                else
                {
                    Assert.True(false); //force a fail
                }
            }
        }
Exemplo n.º 23
0
 public void ValidateHeaderFields_Checks_For_Min_Required_Fields(VnaFileSection state, string[] headerNames, bool expectedResult)
 {
     string mssg = null;
     var reader = new VnaAdjListReader();
     bool result = reader.ValidateHeaderFields(headerNames, state, out mssg);
     Assert.Equal(expectedResult, result);
     if (expectedResult)
         Assert.Null(mssg);
     else
         Assert.NotNull(mssg);
 }
Exemplo n.º 24
0
 public void ReadStarLine_Changes_State_If_Star_Line_Match_Is_Found(string inputLine, VnaFileSection expectedState)
 {
     var reader = new VnaAdjListReader();
     Assert.Equal(VnaFileSection.None, reader.State);
     var state = reader.ReadStarLine(inputLine);
     Assert.Equal(expectedState, state);
 }
Exemplo n.º 25
0
 public void ConvertToBool_Throws_When_Input_Is_Invalid(string input)
 {
     var reader = new VnaAdjListReader();
     var ex = Assert.Throws<FormatException>(() => reader.ConvertToBool(input));
 }