Fix for missing directories
[pithos-ms-client] / trunk / Pithos.Client.WPF.Test / NodeTest.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using NUnit.Framework;
6 using Pithos.Client.WPF.SelectiveSynch;
7 using Pithos.Client.WPF.Utils;
8 using Pithos.Interfaces;
9
10 namespace Pithos.Client.WPF.Test
11 {
12     [TestFixture]
13     class NodeTest
14     {
15         [Test]
16         public void TestIteration()
17         {
18             var root = new Node<int>{Path = "Root",
19                                 Children =
20                                     {
21                                         new Node<int> {Path = "Root/Path1",
22                                             Children =
23                                                 {
24                                                     new Node<int>{Path="Root/Path1/Path11",
25                                                         Children=
26                                                             {
27                                                                 new Node<int>{Path="Root/Path1/Path11/Path111",
28                                                                 Children=
29                                                                     {
30                                                                         new Node<int>{Path="Root/Path1/Path11/Path111/File1"}        
31                                                                     }}
32                                                             }
33                                                     },
34                                                     new Node<int>{Path="Root/Path1/File2"}
35                                                 }
36                                         },                                        
37                                     }
38                             };
39             Assert.That(root.Count(), Is.EqualTo(6));
40         }
41         
42         [Test]
43         public void TestEquals()
44         {
45             var target = new Node<int>{Path = "Root",
46                                 Children =
47                                     {
48                                         new Node<int> {Path = "Root/Path1",
49                                             Children =
50                                                 {
51                                                     new Node<int>{Path="Root/Path1/Path11",
52                                                         Children=
53                                                             {
54                                                                 new Node<int>{Path="Root/Path1/Path11/Path111",
55                                                                 Children=
56                                                                     {
57                                                                         new Node<int>{Path="Root/Path1/Path11/Path111/File1"}        
58                                                                     }}
59                                                             }
60                                                     },
61                                                     new Node<int>{Path="Root/Path1/File2"}
62                                                 }
63                                         },                                        
64                                     }
65                             };
66             var source= new Node<int>{Path = "Root",
67                                 Children =
68                                     {
69                                         new Node<int>{Path = "Root/Path1",
70                                             Children =
71                                                 {
72                                                     new Node<int>{Path="Root/Path1/Path11",
73                                                         Children=
74                                                             {
75                                                                 new Node<int>{Path="Root/Path1/Path11/Path111",
76                                                                 Children=
77                                                                     {
78                                                                         new Node<int>{Path="Root/Path1/Path11/Path111/File1"}        
79                                                                     }}
80                                                             }
81                                                     },
82                                                     new Node<int>{Path="Root/Path1/File2"}
83                                                 }
84                                         },                                        
85                                     }
86                             };
87             Assert.That(source.Equals(target), Is.True);            
88         }
89
90         [Test]
91         public void TestToTree()
92         {
93             var target = new Node<int>{Path = "Root",
94                                 Children =
95                                     {
96                                         new Node<int>{Path = "Root/Path1",
97                                             Children =
98                                                 {
99                                                     new Node<int>{Path="Root/Path1/File2"},
100                                                     new Node<int>{Path="Root/Path1/Path11",
101                                                         Children=
102                                                             {
103                                                                 new Node<int>{Path="Root/Path1/Path11/Path111",
104                                                                 Children=
105                                                                     {
106                                                                         new Node<int>{Path="Root/Path1/Path11/Path111/File1"}        
107                                                                     }}
108                                                             }
109                                                     },
110                                                 }
111                                         },                                        
112                                     }
113                             };
114             var source= new[]
115                             {
116                                 Tuple.Create("Root",0),
117                                 Tuple.Create("Root/Path1",0),
118                                 Tuple.Create("Root/Path1/Path11",0),
119                                 Tuple.Create("Root/Path1/Path11/Path111",0),
120                                 Tuple.Create("Root/Path1/Path11/Path111/File1",0),
121                                 Tuple.Create("Root/Path1/File2",0)
122                             };
123
124             Assert.That(source.ToTree(s=>s.Item1,s=>s.Item2).First().Equals(target), Is.True);
125         } [Test]
126         
127         public void TestObjectInfoToTree()
128         {
129             var target = new DirectoryRecord{ DisplayName= "Root",
130                                 Directories =
131                                     {
132                                         new DirectoryRecord{DisplayName = "Root/DisplayName1",
133                                             Directories =
134                                                 {
135                                                     new DirectoryRecord{DisplayName="Root/DisplayName1/File2"},
136                                                     new DirectoryRecord{DisplayName="Root/DisplayName1/DisplayName11",
137                                                         Directories=
138                                                             {
139                                                                 new DirectoryRecord{DisplayName="Root/DisplayName1/DisplayName11/DisplayName111",
140                                                                 Directories=
141                                                                     {
142                                                                         new DirectoryRecord{DisplayName="Root/DisplayName1/DisplayName11/DisplayName111/File1"}        
143                                                                     }}
144                                                             }
145                                                     },
146                                                 }
147                                         },                                        
148                                     }
149                             };
150             var account = "someaccount";
151             var container = "Root";
152             var source= new[]
153                             {
154                                 new ObjectInfo{Account=account,Container=container,Name="Path1",Content_Type="application/directory"},
155                                 new ObjectInfo{Account=account,Container=container,Name="Path1/Path11",Content_Type="application/folder"},
156                                 new ObjectInfo{Account=account,Container=container,Name="Path1/Path11/Path111"},
157                                 new ObjectInfo{Account=account,Container=container,Name="Path1/Path11/Path111/File1",Content_Type="application/octet-stream"},
158                                 new ObjectInfo{Account=account,Container=container,Name="Path1/File2"},
159                                 new ObjectInfo{Account=account,Container=container,Name="Path2/File2"},
160                                 new ObjectInfo{Account=account,Container=container,Name="Path2/Path21/File2"},
161                                 new ObjectInfo{Account=account,Container=container,Name="File02"},
162                                 new ObjectInfo{Account=account,Container=container,Name="File03"}
163                             };
164
165             var tree = source.ToTree();
166             var allNodes = (from DirectoryRecord root in tree
167                             from DirectoryRecord record in root
168                             select record).ToList();    
169             Assert.That(allNodes.Count,Is.EqualTo(5));
170         }
171     }
172 }