Exemplo n.º 1
0
        public async Task TestCreateFolderHierarchy()
        {
            Assert.NotNull(context);

            // create a folder-hierarchy /A/B/C
            await repo.Create(new BookmarkEntity {
                Id          = NewId,
                ChildCount  = 0,
                Created     = DateTime.UtcNow,
                DisplayName = "A",
                Path        = "/",
                SortOrder   = 0,
                Type        = ItemType.Folder,
                Url         = "http://url",
                UserName    = Username
            });

            await repo.Create(new BookmarkEntity {
                Id          = NewId,
                ChildCount  = 0,
                Created     = DateTime.UtcNow,
                DisplayName = "B",
                Path        = "/A",
                SortOrder   = 0,
                Type        = ItemType.Folder,
                Url         = "http://url",
                UserName    = Username
            });

            await repo.Create(new BookmarkEntity {
                ChildCount  = 0,
                Created     = DateTime.UtcNow,
                DisplayName = "C",
                Path        = "/A/B",
                SortOrder   = 0,
                Type        = ItemType.Folder,
                Url         = "http://url",
                UserName    = Username
            });

            var bms = await repo.GetBookmarksByPathStart("/A", Username);

            Assert.NotNull(bms);
            Assert.Equal(2, bms.Count);

            bms = await repo.GetAllBookmarks(Username);

            Assert.NotNull(bms);
            Assert.Equal(3, bms.Count);
            Assert.Equal("A", bms[0].DisplayName);
            Assert.Equal("B", bms[1].DisplayName);
            Assert.Equal("C", bms[2].DisplayName);

            bms = await repo.GetBookmarksByName("B", Username);

            Assert.NotNull(bms);
            Assert.Single(bms);
            Assert.Equal("/A", bms[0].Path);

            bms = await repo.GetBookmarksByPath("/A/B", Username);

            Assert.NotNull(bms);
            Assert.Single(bms);
            Assert.Equal("C", bms[0].DisplayName);

            var bm = await repo.GetFolderByPath("/A/B", Username);

            Assert.NotNull(bm);
            Assert.Equal("B", bm.DisplayName);
            Assert.Equal("/A", bm.Path);

            // invalid folder
            bm = await repo.GetFolderByPath("|A|B", Username);

            Assert.Null(bm);

            // use the structure from above and get the child-count of nodes
            // this is just a folder-structure of /A/B/C
            var nodes = await repo.GetChildCountOfPath("/A", Username);

            Assert.NotNull(nodes);
            Assert.Single(nodes);
            Assert.Equal(1, nodes[0].Count);
            Assert.Equal("/A", nodes[0].Path);

            nodes = await repo.GetChildCountOfPath("", Username);

            Assert.NotNull(nodes);
            Assert.Equal(3, nodes.Count);
            Assert.Equal(1, nodes[0].Count);
            Assert.Equal("/", nodes[0].Path);

            // create a node to an existing path
            var nodeID = NewId;

            bm = await repo.Create(new BookmarkEntity {
                Id          = nodeID,
                ChildCount  = 0,
                Created     = DateTime.UtcNow,
                DisplayName = "URL",
                Path        = "/A/B",
                SortOrder   = 0,
                Type        = ItemType.Node,
                Url         = "http://url",
                UserName    = Username
            });

            Assert.NotNull(bm);

            // get the folder
            bm = await repo.GetFolderByPath("/A/B", Username);

            Assert.NotNull(bm);
            Assert.Equal("B", bm.DisplayName);
            Assert.Equal("/A", bm.Path);
            Assert.Equal(2, bm.ChildCount); // sub-folder and the newly created node

            nodes = await repo.GetChildCountOfPath("/A/B", Username);

            Assert.NotNull(nodes);
            Assert.Single(nodes);
            Assert.Equal(2, nodes[0].Count);
            Assert.Equal("/A/B", nodes[0].Path);

            // unknown path
            nodes = await repo.GetChildCountOfPath("/A/B/C/D/E", Username);

            Assert.Empty(nodes);

            // remove a node
            Assert.True(await repo.Delete(new BookmarkEntity {
                Id = nodeID, UserName = Username
            }));

            bm = await repo.GetFolderByPath("/A/B", Username);

            Assert.NotNull(bm);
            Assert.Equal("B", bm.DisplayName);
            Assert.Equal("/A", bm.Path);
            Assert.Equal(1, bm.ChildCount); // sub-folder only

            Assert.False(await repo.Delete(new BookmarkEntity {
                Id = "-1", UserName = Username
            }));

            // we have the path /A/B/C
            // if we delete /A/B the only thing left will be /A
            Assert.True(await repo.DeletePath("/A/B", Username));

            bms = await repo.GetAllBookmarks(Username);

            Assert.NotNull(bms);
            Assert.Single(bms);

            await Assert.ThrowsAsync <ArgumentException>(() => {
                return(repo.DeletePath("", Username));
            });

            await Assert.ThrowsAsync <ArgumentException>(() => {
                return(repo.DeletePath("/", Username));
            });

            Assert.False(await repo.DeletePath("/D/E", Username));
        }