Fix for duplicates in selectivesync display code
[pithos-ms-client] / trunk / Pithos.Client.WPF.Test / NodeTest.cs
index e37d87a..3842eeb 100644 (file)
@@ -3,170 +3,288 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using NUnit.Framework;
+using Newtonsoft.Json;
 using Pithos.Client.WPF.SelectiveSynch;
 using Pithos.Client.WPF.Utils;
 using Pithos.Interfaces;
+using System.IO;
 
 namespace Pithos.Client.WPF.Test
 {
     [TestFixture]
-    class NodeTest
+    internal class NodeTest
     {
         [Test]
         public void TestIteration()
         {
-            var root = new Node<int>{Path = "Root",
-                                Children =
-                                    {
-                                        new Node<int> {Path = "Root/Path1",
-                                            Children =
-                                                {
-                                                    new Node<int>{Path="Root/Path1/Path11",
-                                                        Children=
-                                                            {
-                                                                new Node<int>{Path="Root/Path1/Path11/Path111",
-                                                                Children=
-                                                                    {
-                                                                        new Node<int>{Path="Root/Path1/Path11/Path111/File1"}        
-                                                                    }}
-                                                            }
-                                                    },
-                                                    new Node<int>{Path="Root/Path1/File2"}
-                                                }
-                                        },                                        
-                                    }
-                            };
+            var root = new Node<int>
+                           {
+                               Path = "Root",
+                               Children =
+                                   {
+                                       new Node<int>
+                                           {
+                                               Path = "Root/Path1",
+                                               Children =
+                                                   {
+                                                       new Node<int>
+                                                           {
+                                                               Path = "Root/Path1/Path11",
+                                                               Children =
+                                                                   {
+                                                                       new Node<int>
+                                                                           {
+                                                                               Path = "Root/Path1/Path11/Path111",
+                                                                               Children =
+                                                                                   {
+                                                                                       new Node<int>
+                                                                                           {
+                                                                                               Path =
+                                                                                                   "Root/Path1/Path11/Path111/File1"
+                                                                                           }
+                                                                                   }
+                                                                           }
+                                                                   }
+                                                           },
+                                                       new Node<int> {Path = "Root/Path1/File2"}
+                                                   }
+                                           },
+                                   }
+                           };
             Assert.That(root.Count(), Is.EqualTo(6));
         }
-        
+
         [Test]
         public void TestEquals()
         {
-            var target = new Node<int>{Path = "Root",
-                                Children =
-                                    {
-                                        new Node<int> {Path = "Root/Path1",
-                                            Children =
-                                                {
-                                                    new Node<int>{Path="Root/Path1/Path11",
-                                                        Children=
-                                                            {
-                                                                new Node<int>{Path="Root/Path1/Path11/Path111",
-                                                                Children=
-                                                                    {
-                                                                        new Node<int>{Path="Root/Path1/Path11/Path111/File1"}        
-                                                                    }}
-                                                            }
-                                                    },
-                                                    new Node<int>{Path="Root/Path1/File2"}
-                                                }
-                                        },                                        
-                                    }
-                            };
-            var source= new Node<int>{Path = "Root",
-                                Children =
-                                    {
-                                        new Node<int>{Path = "Root/Path1",
-                                            Children =
-                                                {
-                                                    new Node<int>{Path="Root/Path1/Path11",
-                                                        Children=
-                                                            {
-                                                                new Node<int>{Path="Root/Path1/Path11/Path111",
-                                                                Children=
-                                                                    {
-                                                                        new Node<int>{Path="Root/Path1/Path11/Path111/File1"}        
-                                                                    }}
-                                                            }
-                                                    },
-                                                    new Node<int>{Path="Root/Path1/File2"}
-                                                }
-                                        },                                        
-                                    }
-                            };
-            Assert.That(source.Equals(target), Is.True);            
+            var target = new Node<int>
+                             {
+                                 Path = "Root",
+                                 Children =
+                                     {
+                                         new Node<int>
+                                             {
+                                                 Path = "Root/Path1",
+                                                 Children =
+                                                     {
+                                                         new Node<int>
+                                                             {
+                                                                 Path = "Root/Path1/Path11",
+                                                                 Children =
+                                                                     {
+                                                                         new Node<int>
+                                                                             {
+                                                                                 Path = "Root/Path1/Path11/Path111",
+                                                                                 Children =
+                                                                                     {
+                                                                                         new Node<int>
+                                                                                             {
+                                                                                                 Path =
+                                                                                                     "Root/Path1/Path11/Path111/File1"
+                                                                                             }
+                                                                                     }
+                                                                             }
+                                                                     }
+                                                             },
+                                                         new Node<int> {Path = "Root/Path1/File2"}
+                                                     }
+                                             },
+                                     }
+                             };
+            var source = new Node<int>
+                             {
+                                 Path = "Root",
+                                 Children =
+                                     {
+                                         new Node<int>
+                                             {
+                                                 Path = "Root/Path1",
+                                                 Children =
+                                                     {
+                                                         new Node<int>
+                                                             {
+                                                                 Path = "Root/Path1/Path11",
+                                                                 Children =
+                                                                     {
+                                                                         new Node<int>
+                                                                             {
+                                                                                 Path = "Root/Path1/Path11/Path111",
+                                                                                 Children =
+                                                                                     {
+                                                                                         new Node<int>
+                                                                                             {
+                                                                                                 Path =
+                                                                                                     "Root/Path1/Path11/Path111/File1"
+                                                                                             }
+                                                                                     }
+                                                                             }
+                                                                     }
+                                                             },
+                                                         new Node<int> {Path = "Root/Path1/File2"}
+                                                     }
+                                             },
+                                     }
+                             };
+            Assert.That(source.Equals(target), Is.True);
         }
 
         [Test]
         public void TestToTree()
         {
-            var target = new Node<int>{Path = "Root",
-                                Children =
-                                    {
-                                        new Node<int>{Path = "Root/Path1",
-                                            Children =
-                                                {
-                                                    new Node<int>{Path="Root/Path1/File2"},
-                                                    new Node<int>{Path="Root/Path1/Path11",
-                                                        Children=
-                                                            {
-                                                                new Node<int>{Path="Root/Path1/Path11/Path111",
-                                                                Children=
-                                                                    {
-                                                                        new Node<int>{Path="Root/Path1/Path11/Path111/File1"}        
-                                                                    }}
-                                                            }
-                                                    },
-                                                }
-                                        },                                        
-                                    }
-                            };
-            var source= new[]
-                            {
-                                Tuple.Create("Root",0),
-                                Tuple.Create("Root/Path1",0),
-                                Tuple.Create("Root/Path1/Path11",0),
-                                Tuple.Create("Root/Path1/Path11/Path111",0),
-                                Tuple.Create("Root/Path1/Path11/Path111/File1",0),
-                                Tuple.Create("Root/Path1/File2",0)
-                            };
+            var target = new Node<int>
+                             {
+                                 Path = "Root",
+                                 Children =
+                                     {
+                                         new Node<int>
+                                             {
+                                                 Path = "Root/Path1",
+                                                 Children =
+                                                     {
+                                                         new Node<int> {Path = "Root/Path1/File2"},
+                                                         new Node<int>
+                                                             {
+                                                                 Path = "Root/Path1/Path11",
+                                                                 Children =
+                                                                     {
+                                                                         new Node<int>
+                                                                             {
+                                                                                 Path = "Root/Path1/Path11/Path111",
+                                                                                 Children =
+                                                                                     {
+                                                                                         new Node<int>
+                                                                                             {
+                                                                                                 Path =
+                                                                                                     "Root/Path1/Path11/Path111/File1"
+                                                                                             }
+                                                                                     }
+                                                                             }
+                                                                     }
+                                                             },
+                                                     }
+                                             },
+                                     }
+                             };
+            var source = new[]
+                             {
+                                 Tuple.Create("Root", 0),
+                                 Tuple.Create("Root/Path1", 0),
+                                 Tuple.Create("Root/Path1/Path11", 0),
+                                 Tuple.Create("Root/Path1/Path11/Path111", 0),
+                                 Tuple.Create("Root/Path1/Path11/Path111/File1", 0),
+                                 Tuple.Create("Root/Path1/File2", 0)
+                             };
+
+            Assert.That(source.ToTree(s => s.Item1, s => s.Item2).First().Equals(target), Is.True);
+        }
+
+        [Test]
 
-            Assert.That(source.ToTree(s=>s.Item1,s=>s.Item2).First().Equals(target), Is.True);
-        } [Test]
-        
         public void TestObjectInfoToTree()
         {
-            var target = new DirectoryRecord{ DisplayName= "Root",
-                                Directories =
-                                    {
-                                        new DirectoryRecord{DisplayName = "Root/DisplayName1",
-                                            Directories =
-                                                {
-                                                    new DirectoryRecord{DisplayName="Root/DisplayName1/File2"},
-                                                    new DirectoryRecord{DisplayName="Root/DisplayName1/DisplayName11",
-                                                        Directories=
-                                                            {
-                                                                new DirectoryRecord{DisplayName="Root/DisplayName1/DisplayName11/DisplayName111",
-                                                                Directories=
-                                                                    {
-                                                                        new DirectoryRecord{DisplayName="Root/DisplayName1/DisplayName11/DisplayName111/File1"}        
-                                                                    }}
-                                                            }
-                                                    },
-                                                }
-                                        },                                        
-                                    }
-                            };
+           
+            var account = "someaccount";
+            var container = "Root";
+            var source = new[]
+                             {
+                                 new ObjectInfo
+                                     {
+                                         Account = account,
+                                         Container = container,
+                                         Name = "Path1",
+                                         Content_Type = "application/directory"
+                                     },
+                                 new ObjectInfo
+                                     {
+                                         Account = account,
+                                         Container = container,
+                                         Name = "Path1/Path11",
+                                         Content_Type = "application/folder"
+                                     },
+                                 new ObjectInfo
+                                     {Account = account, Container = container, Name = "Path1/Path11/Path111"},
+                                 new ObjectInfo
+                                     {
+                                         Account = account,
+                                         Container = container,
+                                         Name = "Path1/Path11/Path111/File1",
+                                         Content_Type = "application/octet-stream"
+                                     },
+                                 new ObjectInfo {Account = account, Container = container, Name = "Path1/File2"},
+                                 new ObjectInfo {Account = account, Container = container, Name = "Path1/File3"},
+                                 new ObjectInfo {Account = account, Container = container, Name = "Path1/File"},
+                                 new ObjectInfo {Account = account, Container = container, Name = "Path2/File2"},                                 
+                                 new ObjectInfo {Account = account, Container = container, Name = "Path2/Path21/File2"},
+                                 new ObjectInfo {Account = account, Container = container, Name = "File02"},
+                                 new ObjectInfo {Account = account, Container = container, Name = "File03"}
+                             };
+
+            var tree = source.ToTree();
+            var allNodes = (from DirectoryRecord root in tree
+                            from DirectoryRecord record in root
+                            select record).ToList();
+            Assert.That(allNodes.Count, Is.EqualTo(5));
+        }
+        public void InferFromPath()
+        {
+           
             var account = "someaccount";
             var container = "Root";
-            var source= new[]
-                            {
-                                new ObjectInfo{Account=account,Container=container,Name="Path1",Content_Type="application/directory"},
-                                new ObjectInfo{Account=account,Container=container,Name="Path1/Path11",Content_Type="application/folder"},
-                                new ObjectInfo{Account=account,Container=container,Name="Path1/Path11/Path111"},
-                                new ObjectInfo{Account=account,Container=container,Name="Path1/Path11/Path111/File1",Content_Type="application/octet-stream"},
-                                new ObjectInfo{Account=account,Container=container,Name="Path1/File2"},
-                                new ObjectInfo{Account=account,Container=container,Name="Path2/File2"},
-                                new ObjectInfo{Account=account,Container=container,Name="Path2/Path21/File2"},
-                                new ObjectInfo{Account=account,Container=container,Name="File02"},
-                                new ObjectInfo{Account=account,Container=container,Name="File03"}
-                            };
+            var source = new[]
+                             {                                
+                                 new ObjectInfo {Account = account, Container = container, Name = "Path1/File2"},
+                                 new ObjectInfo {Account = account, Container = container, Name = "Path1/File3"},
+                                 new ObjectInfo {Account = account, Container = container, Name = "Path1/Path11/Path111/File3"},
+                                 new ObjectInfo {Account = account, Container = container, Name = "Path2"},
+                                 new ObjectInfo {Account = account, Container = container, Name = "Path2/Path21",Content_Type="application/directory"},
+                                 new ObjectInfo {Account = account, Container = container, Name = "Path2/Path21/Path01",Content_Type="application/directory"},
+                                 new ObjectInfo {Account = account, Container = container, Name = "Path2/Path21/File211"},
+                                 new ObjectInfo {Account = account, Container = container, Name = "x2",Content_Type="application/directory"},
+                                 new ObjectInfo {Account = account, Container = container, Name = "x2/x21",Content_Type="application/directory"},
+                                 new ObjectInfo {Account = account, Container = container, Name = "x2/x22",Content_Type="application/directory"},
+                             };
 
             var tree = source.ToTree();
             var allNodes = (from DirectoryRecord root in tree
                             from DirectoryRecord record in root
-                            select record).ToList();    
-            Assert.That(allNodes.Count,Is.EqualTo(5));
+                            select record).ToList();
+            //Root Nodes
+            Assert.That(tree.Count, Is.EqualTo(3));
+            //Total nodes
+            Assert.That(allNodes.Count, Is.EqualTo(9));
+
+            Assert.That(allNodes.Any(r=>r.DisplayName=="Path1"));
+            Assert.That(allNodes.Any(r => r.DisplayName == "Path11"));
+            Assert.That(allNodes.Any(r => r.DisplayName == "Path111"));
+            Assert.That(allNodes.Any(r => r.DisplayName == "Path2"));
+            Assert.That(allNodes.Any(r => r.DisplayName == "Path21"));
+        }   
+
+
+        [Test]
+        public void TestShared()
+        {
+            var content = File.ReadAllText("json.txt");
+            var items=JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
+            Assert.That(items.Count,Is.EqualTo(23));
+            var tree = items.ToTree();
+            var allNodes = (from DirectoryRecord root in tree
+                            from DirectoryRecord record in root
+                            select record).ToList();
+            Assert.That(allNodes.Count,Is.EqualTo(10));
+        }
+
+        [Test]
+        public void TestOwn()
+        {
+            var content = File.ReadAllText("json2.txt");
+            var items=JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
+            Assert.That(items.Count,Is.EqualTo(22));
+            var tree = items.ToTree();
+            var allNodes = (from DirectoryRecord root in tree
+                            from DirectoryRecord record in root
+                            select record).ToList();
+            Assert.That(allNodes.Count,Is.EqualTo(9));
         }
     }
 }