Exemple #1
0
        public async Task Image_Test()
        {
            var sanity = new SanityDataContext(Options);

            // Clear existing records
            await sanity.DocumentSet <Post>().Delete().CommitAsync();

            await sanity.DocumentSet <Author>().Delete().CommitAsync();

            // Delete images
            await sanity.Images.Delete().CommitAsync();

            // Upload new image
            var imageUri = new Uri("https://www.sanity.io/static/images/opengraph/social.png");
            var image    = (await sanity.Images.UploadAsync(imageUri)).Document;

            // Link image to new author
            var author = new Author()
            {
                Name  = "Joe Bloggs",
                Image = new SanityImage
                {
                    Asset = new SanityReference <SanityImageAsset> {
                        Ref = image.Id
                    },
                }
            };

            await sanity.DocumentSet <Author>().Create(author).CommitAsync();
        }
        public async Task SanityLocaleT_GetWithLangugeCode_ShouldReturnAT()
        {
            var sanity = new SanityDataContext(Options);

            await ClearAllDataAsync(sanity);

            var page = new Page
            {
                Id      = Guid.NewGuid().ToString(),
                Options = new SanityLocale <PageOptions>()
            };

            page.Options.Set("en", new PageOptions {
                ShowOnFrontPage = false, Subtitle = "Awesome page"
            });
            page.Options.Set("no", new PageOptions {
                ShowOnFrontPage = true, Subtitle = "Heftig bra side!"
            });

            // Create page
            await sanity.DocumentSet <Page>().Create(page).CommitAsync();

            // Retrieve newly created page
            page = await sanity.DocumentSet <Page>().GetAsync(page.Id);

            var enOptions = page.Options.Get("en");
            var noOptions = page.Options.Get("no");

            Assert.NotNull(enOptions);
            Assert.NotNull(noOptions);
            Assert.Equal("Awesome page", enOptions.Subtitle);
            Assert.Equal("Heftig bra side!", noOptions.Subtitle);
            Assert.False(enOptions.ShowOnFrontPage);
            Assert.True(noOptions.ShowOnFrontPage);
        }
        public async Task SanityLocaleString_GetWithLangugeCode_ShouldReturnAValue()
        {
            var sanity = new SanityDataContext(Options);

            await ClearAllDataAsync(sanity);

            var page = new Page
            {
                Id    = Guid.NewGuid().ToString(),
                Title = new SanityLocaleString(),
            };

            page.Title.Set("en", "My Page");
            page.Title.Set("no", "Min side");

            // Create page
            await sanity.DocumentSet <Page>().Create(page).CommitAsync();

            // Retrieve newly created page
            page = await sanity.DocumentSet <Page>().GetAsync(page.Id);

            var enTitle = page.Title.Get("en");
            var noTitle = page.Title.Get("no");

            Assert.NotNull(enTitle);
            Assert.NotNull(noTitle);
            Assert.Equal("My Page", enTitle);
            Assert.Equal("Min side", noTitle);
        }
Exemple #4
0
        public async Task ClearAllDataAsync(SanityDataContext sanity)
        {
            // Clear existing records in single transaction
            sanity.DocumentSet <Post>().Delete();
            sanity.DocumentSet <Author>().Delete();
            sanity.DocumentSet <Category>().Delete();
            await sanity.CommitAsync();

            await sanity.Images.Delete().CommitAsync();
        }
        public async Task <IEnumerable <string> > Get()
        {
            // Returns a list of strings ordered by publish date
            var postTitles = await _context.DocumentSet <Product>()
                             .OrderByDescending(p => p.Title)
                             .ToListAsync();

            return(postTitles.Select(x => x.Title));
        }
 public async Task ConvertTest()
 {
     // this test tries to serialize the Body-element of a Post-document from the Sanity demo-dataset.
     var sanity = new SanityDataContext(Options);
     var posts = await sanity.DocumentSet<Post>().ToListAsync();
     var htmlBuilder = new SanityHtmlBuilder(Options);
     foreach (var post in posts)
     {
         var html = htmlBuilder.BuildAsync(post.Body); // the serialized data
     }
 }
Exemple #7
0
        public async Task Image_Test()
        {
            var sanity = new SanityDataContext(Options);

            // Clear existing records
            await sanity.DocumentSet <Post>().Delete().CommitAsync();

            await sanity.DocumentSet <Author>().Delete().CommitAsync();

            // Delete images
            await sanity.Images.Delete().CommitAsync();

            // Upload new image
            var imageUri = new Uri("https://www.sanity.io/static/images/opengraph/social.png");
            var image    = (await sanity.Images.UploadAsync(imageUri)).Document;

            // Link image to new author
            var author = new Author()
            {
                Name   = "Joe Bloggs",
                Images = new SanityImage[] {
                    new SanityImage
                    {
                        Asset = new SanityReference <SanityImageAsset> {
                            Ref = image.Id
                        },
                    }
                }
            };

            await sanity.DocumentSet <Author>().Create(author).CommitAsync();

            var retrievedDoc = await sanity.DocumentSet <Author>().Include(a => a.Images).ToListAsync();

            Assert.True(retrievedDoc.FirstOrDefault()?.Images?.FirstOrDefault()?.Asset != null);
        }
Exemple #8
0
        public async Task CRUD_Test()
        {
            var sanity = new SanityDataContext(Options);

            await ClearAllDataAsync(sanity);

            // Create new category
            var categories = sanity.DocumentSet <Category>();

            var category1 = new Category
            {
                CategoryId  = Guid.NewGuid().ToString(),
                Description = "Some of world's greatest conventions!",
                Title       = "Convention"
            };
            var category2 = new Category
            {
                CategoryId  = Guid.NewGuid().ToString(),
                Description = "Excitement, fun and relaxation...",
                Title       = "Resort"
            };
            var category3 = new Category
            {
                CategoryId  = Guid.NewGuid().ToString(),
                Description = "World class hotel rooms, restaurants and facilities...",
                Title       = "Hotel"
            };

            // Chained mutations
            categories.Create(category1).Create(category2).Create(category3);


            // Create new author
            var author = new Author
            {
                Id   = Guid.NewGuid().ToString(),
                Name = "Joe Bloggs",
                Slug = new SanitySlug("joe"),
                FavoriteCategories = new List <SanityReference <Category> >()
                {
                    new SanityReference <Category>
                    {
                        Value = category1
                    }
                }
            };

            sanity.DocumentSet <Author>().Create(author);

            // Create post
            var post = new Post
            {
                Title       = "Welcome to Oslofjord Convention Center!",
                PublishedAt = DateTimeOffset.Now,
                Categories  = new List <SanityReference <Category> >
                {
                    new SanityReference <Category>
                    {
                        Ref = category1.CategoryId
                    },
                    new SanityReference <Category>
                    {
                        Ref = category2.CategoryId
                    },
                    new SanityReference <Category>
                    {
                        Ref = category3.CategoryId
                    }
                },
                Author = new SanityReference <Author>
                {
                    Value = author
                },
                Body = new[]
                {
                    new SanityBlock
                    {
                        Children = new []
                        {
                            new SanitySpan
                            {
                                Text  = "A bold start!",
                                Marks = new[] { "strong" }
                            }
                        }
                    },
                    new SanityBlock
                    {
                        Children = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    }
                }
            };

            sanity.DocumentSet <Post>().Create(post);

            // Save all changes in one transaction
            var result = await sanity.CommitAsync();


            // Retreive all categories (recursive structure)
            var allCategories = await sanity.DocumentSet <Category>().ToListAsync();

            Assert.True(allCategories.Count >= 3);


            // LINQ Query
            var count = (sanity.DocumentSet <Post>().Count());
            var query = sanity.DocumentSet <Post>()
                        .Include(p => p.Author)
                        .Include(p => p.DereferencedAuthor, "author")
                        .Include(p => p.Author.Value.Images)
                        .Include(p => p.Categories)
                        .Include(p => p.Author.Value.FavoriteCategories)
                        .Where(p => p.PublishedAt >= DateTime.Today);

            // Execute Query
            var results = await query.ToListAsync();

            var transformedResult = sanity.DocumentSet <Post>().Where(p => !p.IsDraft()).Select(p => new { p.Title, p.Author.Value.Name }).ToList();

            Assert.True(count > 0);
            Assert.True(results.Count > 0);
            Assert.NotNull(results[0].Author?.Value);
            Assert.NotNull(results[0].DereferencedAuthor);
            Assert.Equal("Joe Bloggs", results[0].Author.Value.Name);

            // Update test
            var postToUpdate = results[0];

            postToUpdate.Title = "New title";

            await sanity.DocumentSet <Post>().Update(postToUpdate).CommitAsync();

            var updatedItem = await sanity.DocumentSet <Post>().GetAsync(postToUpdate.Id);

            Assert.True(updatedItem.Title == "New title");
        }
Exemple #9
0
        public async Task Contains_Test()
        {
            var sanity = new SanityDataContext(Options);

            await ClearAllDataAsync(sanity);

            var categories = sanity.DocumentSet <Category>();

            // Create categoriues
            var category1 = new Category
            {
                CategoryId  = Guid.NewGuid().ToString(),
                Description = "Some of world's greatest conventions!",
                Tags        = new[] { "One", "Two", "Three" },
                Numbers     = new[] { 1, 2, 3 },
                Title       = "Conventions",
                InternalId  = 1
            };
            var category2 = new Category
            {
                CategoryId  = Guid.NewGuid().ToString(),
                Description = "Some of world's greatest events!",
                Tags        = new[] { "Four", "Five", "Six" },
                Numbers     = new[] { 4, 5, 6 },
                Title       = "Events",
                InternalId  = 2
            };
            var category3 = new Category
            {
                CategoryId  = Guid.NewGuid().ToString(),
                Description = "Some of world's greatest festivals!",
                Tags        = new[] { "Seven", "Eight", "Nine" },
                Numbers     = new[] { 7, 8, 9 },
                Title       = "Festivals",
                InternalId  = 3
            };
            await categories
            .Create(category1)
            .Create(category2)
            .Create(category3)
            .CommitAsync();

            // Test 1
            // *[title in ["Conventions", "Festivals"]]
            // .Where(p => ids.Contains(p.Id))

            var namesToFind = new List <string> {
                "Conventions", "Festivals"
            };
            var result1 = await categories.Where(c => namesToFind.Contains(c.Title)).ToListAsync();

            Assert.True(result1.Count == 2);

            var ïdsToFind = new List <int> {
                1, 2
            };
            var result2 = await categories.Where(c => ïdsToFind.Contains(c.InternalId)).ToListAsync();

            Assert.True(result2.Count == 2);


            // Test 2
            // *["Two" in tags]
            // .Where(p => p.Ids.Contains(ids))
            var result3 = await categories.Where(c => c.Tags.Contains("Two")).ToListAsync();

            Assert.True(result3.Count == 1);

            var result4 = await categories.Where(c => c.Numbers.Contains(3)).ToListAsync();

            Assert.True(result4.Count == 1);

            var result5 = await categories.Where(c => c.Numbers.Contains(c.InternalId)).ToListAsync();

            Assert.True(result5.Count == 1);
        }
Exemple #10
0
        public async Task Image_Test()
        {
            var sanity = new SanityDataContext(Options);

            // Clear existing records
            await sanity.DocumentSet <Post>().Delete().CommitAsync();

            await sanity.DocumentSet <Author>().Delete().CommitAsync();

            await sanity.DocumentSet <Category>().Delete().CommitAsync();

            // Delete images
            await sanity.Images.Delete().CommitAsync();

            // Upload new image
            var imageUri = new Uri("https://www.sanity.io/static/images/opengraph/social.png");
            var image    = (await sanity.Images.UploadAsync(imageUri)).Document;

            var category = new Category
            {
                CategoryId  = "AUTHORS",
                Description = "Category for popular authors",
                Title       = "Popular Authors",
                MainImage   = new SanityImage
                {
                    Asset = new SanityReference <SanityImageAsset> {
                        Ref = image.Id
                    },
                }
            };
            await sanity.DocumentSet <Category>().Create(category).CommitAsync();

            // Link image to new author
            var author = new Author()
            {
                Name   = "Joe Bloggs",
                Images = new SanityImage[] {
                    new SanityImage
                    {
                        Asset = new SanityReference <SanityImageAsset> {
                            Ref = image.Id
                        },
                    }
                },
                FavoriteCategories = new List <SanityReference <Category> >
                {
                    new SanityReference <Category>
                    {
                        Value = category
                    }
                }
            };

            await sanity.DocumentSet <Author>().Create(author).CommitAsync();

            var retrievedDoc = await sanity.DocumentSet <Author>().ToListAsync();

            Assert.True(retrievedDoc.FirstOrDefault()?.Images?.FirstOrDefault()?.Asset?.Value?.Extension != null);

            Assert.True(retrievedDoc.FirstOrDefault()?.FavoriteCategories?.FirstOrDefault().Value?.MainImage?.Asset?.Value?.Extension != null);
        }
Exemple #11
0
        public async Task BlockContent_Extensions_Test()
        {
            var sanity = new SanityDataContext(Options);

            // Clear existing records in single transaction
            await ClearAllDataAsync(sanity);

            // Uplooad image
            var imageUri = new Uri("https://www.sanity.io/static/images/opengraph/social.png");
            var image    = (await sanity.Images.UploadAsync(imageUri)).Document;

            // Create post
            var post = new Post
            {
                MainImage = new SanityImage
                {
                    Asset = new SanityReference <SanityImageAsset> {
                        Ref = image.Id
                    },
                },

                Body = new SanityObject[]
                {
                    new SanityBlock
                    {
                        Children = new []
                        {
                            new SanitySpan
                            {
                                Text  = "A bold start!",
                                Marks = new[] { "strong" }
                            }
                        }
                    },
                    new SanityBlock
                    {
                        Children = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    },
                    new SanityImage
                    {
                        Asset = new SanityReference <SanityImageAsset> {
                            Ref = image.Id
                        },
                    },
                    new SanityBlock
                    {
                        SanityType = "block",
                        Level      = 1,
                        ListItem   = "bullet",
                        Children   = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    },
                    new SanityBlock
                    {
                        SanityType = "block",
                        Level      = 1,
                        ListItem   = "bullet",
                        Children   = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    },
                    new SanityBlock
                    {
                        SanityType = "block",
                        Level      = 2,
                        ListItem   = "bullet",
                        Children   = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    },
                    new SanityBlock
                    {
                        SanityType = "block",
                        Level      = 3,
                        ListItem   = "bullet",
                        Children   = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    },
                    new SanityBlock
                    {
                        SanityType = "block",
                        Level      = 1,
                        ListItem   = "number",
                        Children   = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    }
                    ,
                    new SanityBlock
                    {
                        SanityType = "block",
                        Level      = 2,
                        ListItem   = "number",
                        Children   = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    }
                }
            };
            var result = (await sanity.DocumentSet <Post>().Create(post).CommitAsync()).Results[0].Document;

            // Serialize block content
            var html = await result.Body.ToHtmlAsync(sanity);

            // Serialize single object
            var imageTag = await result.MainImage.ToHtmlAsync(sanity);

            //test whole content
            Assert.NotNull(html);
            Assert.True(html.IndexOf("<strong>A bold start!</strong>") != -1);
            Assert.True(html.IndexOf("<img") != -1);
            Assert.True(html.IndexOf(image.AssetId) != -1);

            //test image
            Assert.True(imageTag.IndexOf("<img") != -1);
            Assert.True(imageTag.IndexOf(image.AssetId) != -1);
        }