示例#1
0
        public void DeleteDocumentByCommandAndThanItsAttachments_ThisIsNoOpButShouldBeSupported()
        {
            using (var store = GetDocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    session.Store(new User {
                        Name = "Fitzchak"
                    }, "users/1");

                    using (var stream = new MemoryStream(Enumerable.Range(1, 3).Select(x => (byte)x).ToArray()))
                    {
                        session.Advanced.StoreAttachment("users/1", "file", stream, "image/png");
                        session.SaveChanges();
                    }
                }

                AttachmentsCrud.AssertAttachmentCount(store, 1, documentsCount: 1);

                using (var session = store.OpenSession())
                {
                    session.Advanced.Defer(new DeleteCommandData("users/1", null));
                    session.Advanced.DeleteAttachment("users/1", "file");
                    session.Advanced.DeleteAttachment("users/1", "file"); // this should be no-op

                    session.SaveChanges();
                }
                AttachmentsCrud.AssertAttachmentCount(store, 0, documentsCount: 0);
            }
        }
示例#2
0
        public async Task DeleteAttachments()
        {
            using (var store = GetDocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    var user = new User {
                        Name = "Fitzchak"
                    };
                    session.Store(user, "users/1");

                    using (var stream1 = new MemoryStream(Enumerable.Range(1, 3).Select(x => (byte)x).ToArray()))
                        using (var stream2 = new MemoryStream(Enumerable.Range(1, 6).Select(x => (byte)x).ToArray()))
                            using (var stream3 = new MemoryStream(Enumerable.Range(1, 9).Select(x => (byte)x).ToArray()))
                                using (var stream4 = new MemoryStream(Enumerable.Range(1, 12).Select(x => (byte)x).ToArray()))
                                {
                                    session.Advanced.StoreAttachment(user, "file1", stream1, "image/png");
                                    session.Advanced.StoreAttachment(user, "file2", stream2, "image/png");
                                    session.Advanced.StoreAttachment(user, "file3", stream3, "image/png");
                                    session.Advanced.StoreAttachment(user, "file4", stream4, "image/png");

                                    session.SaveChanges();
                                }
                }

                AttachmentsCrud.AssertAttachmentCount(store, 4, documentsCount: 1);

                using (var session = store.OpenSession())
                {
                    var user = session.Load <User>("users/1");

                    session.Advanced.DeleteAttachment("users/1", "file2");
                    session.Advanced.DeleteAttachment(user, "file4");

                    session.SaveChanges();
                }
                AttachmentsCrud.AssertAttachmentCount(store, 2, documentsCount: 1);

                using (var session = store.OpenSession())
                {
                    var user     = session.Load <User>("users/1");
                    var metadata = session.Advanced.GetMetadataFor(user);
                    Assert.Equal(DocumentFlags.HasAttachments.ToString(), metadata[Constants.Documents.Metadata.Flags]);
                    var attachments = metadata.GetObjects(Constants.Documents.Metadata.Attachments);
                    Assert.Equal(2, attachments.Length);
                    Assert.Equal("file1", attachments[0].GetString(nameof(AttachmentName.Name)));
                    Assert.Equal("EcDnm3HDl2zNDALRMQ4lFsCO3J2Lb1fM1oDWOk2Octo=", attachments[0].GetString(nameof(AttachmentName.Hash)));
                    Assert.Equal("file3", attachments[1].GetString(nameof(AttachmentName.Name)));
                    Assert.Equal("NRQuixiqj+xvEokF6MdQq1u+uH1dk/gk2PLChJQ58Vo=", attachments[1].GetString(nameof(AttachmentName.Hash)));
                }

                var dbId1 = new Guid("00000000-48c4-421e-9466-000000000000");
                await SetDatabaseId(store, dbId1);

                using (var session = store.OpenSession())
                {
                    var user = session.Load <User>("users/1");


                    var readBuffer = new byte[16];
                    using (var attachmentStream = new MemoryStream(readBuffer))
                        using (var attachment = session.Advanced.GetAttachment("users/1", "file1"))
                        {
                            attachment.Stream.CopyTo(attachmentStream);
                            Assert.Equal("A:2", attachment.Details.ChangeVector.Substring(0, 3));
                            Assert.Equal("file1", attachment.Details.Name);
                            Assert.Equal("EcDnm3HDl2zNDALRMQ4lFsCO3J2Lb1fM1oDWOk2Octo=", attachment.Details.Hash);
                            Assert.Equal(3, attachmentStream.Position);
                            Assert.Equal(new byte[] { 1, 2, 3 }, readBuffer.Take(3));
                        }
                    using (var attachment = session.Advanced.GetAttachment(user, "file2"))
                    {
                        Assert.Null(attachment);
                    }
                    using (var attachmentStream = new MemoryStream(readBuffer))
                        using (var attachment = session.Advanced.GetAttachment(user, "file3"))
                        {
                            attachment.Stream.CopyTo(attachmentStream);
                            Assert.Equal("A:4", attachment.Details.ChangeVector.Substring(0, 3));
                            Assert.Equal("file3", attachment.Details.Name);
                            Assert.Equal("NRQuixiqj+xvEokF6MdQq1u+uH1dk/gk2PLChJQ58Vo=", attachment.Details.Hash);
                            Assert.Equal(9, attachmentStream.Position);
                            Assert.Equal(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, readBuffer.Take(9));
                        }
                    using (var attachment = session.Advanced.GetAttachment(user, "file4"))
                    {
                        Assert.Null(attachment);
                    }

                    // Delete document should delete all the attachments
                    session.Delete(user);
                    session.SaveChanges();
                }
                AttachmentsCrud.AssertAttachmentCount(store, 0, documentsCount: 0);
            }
        }
示例#3
0
        public async Task PutAttachments()
        {
            using (var store = GetDocumentStore())
            {
                var names = new[]
                {
                    "profile.png",
                    "background-photo.jpg",
                    "fileNAME_#$1^%_בעברית.txt"
                };

                using (var session = store.OpenSession())
                    using (var profileStream = new MemoryStream(new byte[] { 1, 2, 3 }))
                        using (var backgroundStream = new MemoryStream(new byte[] { 10, 20, 30, 40, 50 }))
                            using (var fileStream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }))
                            {
                                var user = new User {
                                    Name = "Fitzchak"
                                };
                                session.Store(user, "users/1");

                                session.Advanced.StoreAttachment("users/1", names[0], profileStream, "image/png");
                                session.Advanced.StoreAttachment(user, names[1], backgroundStream, "ImGgE/jPeG");
                                session.Advanced.StoreAttachment(user, names[2], fileStream);

                                session.SaveChanges();
                            }

                using (var session = store.OpenSession())
                {
                    var user     = session.Load <User>("users/1");
                    var metadata = session.Advanced.GetMetadataFor(user);
                    Assert.Equal(DocumentFlags.HasAttachments.ToString(), metadata[Constants.Documents.Metadata.Flags]);
                    var attachments = metadata.GetObjects(Constants.Documents.Metadata.Attachments);
                    Assert.Equal(3, attachments.Length);
                    var orderedNames = names.OrderBy(x => x).ToArray();
                    for (var i = 0; i < names.Length; i++)
                    {
                        var name       = orderedNames[i];
                        var attachment = attachments[i];
                        Assert.Equal(name, attachment.GetString(nameof(AttachmentName.Name)));
                        var hash = attachment.GetString(nameof(AttachmentName.Hash));
                        if (i == 0)
                        {
                            Assert.Equal("igkD5aEdkdAsAB/VpYm1uFlfZIP9M2LSUsD6f6RVW9U=", hash);
                            Assert.Equal(5, attachment.GetLong(nameof(AttachmentName.Size)));
                        }
                        else if (i == 1)
                        {
                            Assert.Equal("Arg5SgIJzdjSTeY6LYtQHlyNiTPmvBLHbr/Cypggeco=", hash);
                            Assert.Equal(5, attachment.GetLong(nameof(AttachmentName.Size)));
                        }
                        else if (i == 2)
                        {
                            Assert.Equal("EcDnm3HDl2zNDALRMQ4lFsCO3J2Lb1fM1oDWOk2Octo=", hash);
                            Assert.Equal(3, attachment.GetLong(nameof(AttachmentName.Size)));
                        }
                    }

                    AttachmentsCrud.AssertAttachmentCount(store, 3, 3);

                    var dbId1 = new Guid("00000000-48c4-421e-9466-000000000000");
                    await SetDatabaseId(store, dbId1);

                    var readBuffer = new byte[8];
                    for (var i = 0; i < names.Length; i++)
                    {
                        var name = names[i];
                        using (var attachmentStream = new MemoryStream(readBuffer))
                            using (var attachment = session.Advanced.GetAttachment(user, name))
                            {
                                attachment.Stream.CopyTo(attachmentStream);
                                var expected = "A:" + (2 + i);
                                Assert.Equal(expected, attachment.Details.ChangeVector.Substring(0, expected.Length));
                                Assert.Equal(name, attachment.Details.Name);
                                Assert.Equal(i == 0 ? 3 : 5, attachmentStream.Position);
                                if (i == 0)
                                {
                                    Assert.Equal(new byte[] { 1, 2, 3 }, readBuffer.Take(3));
                                    Assert.Equal("image/png", attachment.Details.ContentType);
                                    Assert.Equal("EcDnm3HDl2zNDALRMQ4lFsCO3J2Lb1fM1oDWOk2Octo=", attachment.Details.Hash);
                                    Assert.Equal(3, attachment.Details.Size);
                                }
                                else if (i == 1)
                                {
                                    Assert.Equal(new byte[] { 10, 20, 30, 40, 50 }, readBuffer.Take(5));
                                    Assert.Equal("ImGgE/jPeG", attachment.Details.ContentType);
                                    Assert.Equal("igkD5aEdkdAsAB/VpYm1uFlfZIP9M2LSUsD6f6RVW9U=", attachment.Details.Hash);
                                    Assert.Equal(5, attachment.Details.Size);
                                }
                                else if (i == 2)
                                {
                                    Assert.Equal(new byte[] { 1, 2, 3, 4, 5 }, readBuffer.Take(5));
                                    Assert.Equal("", attachment.Details.ContentType);
                                    Assert.Equal("Arg5SgIJzdjSTeY6LYtQHlyNiTPmvBLHbr/Cypggeco=", attachment.Details.Hash);
                                    Assert.Equal(5, attachment.Details.Size);
                                }
                            }
                    }

                    using (var notExistsAttachment = session.Advanced.GetAttachment("users/1", "not-there"))
                    {
                        Assert.Null(notExistsAttachment);
                    }
                }
            }
        }