Пример #1
0
        public async Task TestUploadStreamingOverloadAsync()
        {
            var database = Guid.NewGuid().ToString();
            var path     = Path.Combine(Path.GetTempPath(), database);
            var ct       = new CarboniteTool($"Path={path}");
            var stor     = ct.GetStorage();

            var fileSource = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\filesource";

            using (var sourceStream1 = new FileStream($"{Path.Combine(fileSource, "This is a test file.docx")}", FileMode.Open))
                using (var sourceStream2 = new FileStream($"{Path.Combine(fileSource, "broken.pdf")}", FileMode.Open))
                    await stor.UploadAsync(new List <object> {
                        1, 2
                    }, new List <string> {
                        "This is an uploaded test file.docx", "broken.pdf"
                    }, new List <Stream> {
                        sourceStream1, sourceStream2
                    });

            var file = await stor.GetByIdAsync(1);

            Assert.Equal("This is an uploaded test file.docx", file.Filename);

            Directory.Delete(path, true);
        }
Пример #2
0
        public async Task TestSetMetadataAsync()
        {
            var database = Guid.NewGuid().ToString();
            var path     = Path.Combine(Path.GetTempPath(), database);
            var ct       = new CarboniteTool($"Path={path}");
            var stor     = ct.GetStorage();

            var fileSource = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\filesource";

            await stor.UploadAsync(1, $"{Path.Combine(fileSource, "This is a test file.docx")}");

            var oldmeta = await stor.GetByIdAsync(1);

            Assert.Empty(oldmeta.Metadata);

            var metadata = new Dictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" },
                { "key3", "value3" },
                { "key4", "value4" },
            };

            await stor.SetMetadata(1, metadata);

            var newmeta = await stor.GetByIdAsync(1);

            Assert.Equal(metadata, newmeta.Metadata);

            Directory.Delete(path, true);
        }
Пример #3
0
        public async Task TestDeleteMultipleAsync()
        {
            var database = Guid.NewGuid().ToString();
            var path     = Path.Combine(Path.GetTempPath(), database);
            var ct       = new CarboniteTool($"Path={path}");
            var stor     = ct.GetStorage();

            var fileSource = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\filesource";

            await stor.UploadAsync(new List <object> {
                1, 2
            }, new List <string> {
                $"{Path.Combine(fileSource, "This is a test file.docx")}", $"{Path.Combine(fileSource, "broken.pdf")}"
            });

            await stor.DeleteMultipleAsync(new List <object> {
                1, 2
            });

            var files = await stor.GetAllAsync();

            Assert.Empty(files);

            Directory.Delete(path, true);
        }
Пример #4
0
        public async Task TestInsertAsync()
        {
            var database = Guid.NewGuid().ToString();
            var path     = Path.Combine(Path.GetTempPath(), database);
            var ct       = new CarboniteTool($"Path={path}");
            var col      = ct.GetCollection <Order>();

            var customer = new Customer
            {
                Id            = 2,
                Firstname     = "Bill",
                Lastname      = "Masterson",
                Email         = "*****@*****.**",
                Phone         = "555-1234",
                CustomerSince = DateTime.Parse("2008-11-01"),
            };

            var order = new Order
            {
                Id       = new Guid("16db1209-ac9c-472f-bf76-5ba4dcecf2bd"),
                Subtotal = 240.99,
                Total    = 258.25,
                IsFilled = false,
                Customer = customer,
            };

            await col.InsertAsync(order);

            Directory.Delete(path, true);
        }
Пример #5
0
        public async Task TestGetAllAsync()
        {
            var path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\CarboniteTest";
            var ct   = new CarboniteTool($"Path={path}");
            var col  = ct.GetCollection <Order>();

            var orders = await col.GetAllAsync();

            Assert.Equal(2, orders.Length);

            Assert.Equal(new Guid("06a1bac6-b534-421d-a130-1441fe0ef5c6"), orders[0].Id);
            Assert.Equal(55.23, orders[0].Subtotal);
            Assert.Equal(60.91, orders[0].Total);
            Assert.True(orders[0].IsFilled);

            Assert.Equal(2, orders[0].Customer.Id);
            Assert.Equal("Bill", orders[0].Customer.Firstname);
            Assert.Equal("Masterson", orders[0].Customer.Lastname);
            Assert.Equal("*****@*****.**", orders[0].Customer.Email);
            Assert.Equal("555-1234", orders[0].Customer.Phone);
            Assert.Equal(DateTime.Parse("2008-11-01"), orders[0].Customer.CustomerSince);

            Assert.Equal(new Guid("16db1209-ac9c-472f-bf76-5ba4dcecf2bd"), orders[1].Id);
            Assert.Equal(240.99, orders[1].Subtotal);
            Assert.Equal(258.25, orders[1].Total);
            Assert.False(orders[1].IsFilled);

            Assert.Equal(4, orders[1].Customer.Id);
            Assert.Equal("Armin", orders[1].Customer.Firstname);
            Assert.Equal("Slate", orders[1].Customer.Lastname);
            Assert.Equal("*****@*****.**", orders[1].Customer.Email);
            Assert.Equal("555-1956", orders[1].Customer.Phone);
            Assert.Equal(DateTime.Parse("2011-12-21"), orders[1].Customer.CustomerSince);
        }
Пример #6
0
        public async Task TestDeleteMultipleAsync()
        {
            var database = Guid.NewGuid().ToString();
            var path     = Path.Combine(Path.GetTempPath(), database);
            var ct       = new CarboniteTool($"Path={path}");
            var col      = ct.GetCollection <Order>();

            var customerA = new Customer
            {
                Id            = 2,
                Firstname     = "Bill",
                Lastname      = "Masterson",
                Email         = "*****@*****.**",
                Phone         = "555-1234",
                CustomerSince = DateTime.Parse("2008-11-01"),
            };

            var customerB = new Customer
            {
                Id            = 3,
                Firstname     = "Jill",
                Lastname      = "James",
                Email         = "*****@*****.**",
                Phone         = "555-9456",
                CustomerSince = DateTime.Parse("2012-03-11"),
            };

            var orderA = new Order
            {
                Id       = new Guid("06a1bac6-b534-421d-a130-1441fe0ef5c6"),
                Subtotal = 55.23,
                Total    = 60.91,
                IsFilled = true,
                Customer = customerA,
            };

            var orderB = new Order
            {
                Id       = new Guid("16db1209-ac9c-472f-bf76-5ba4dcecf2bd"),
                Subtotal = 240.99,
                Total    = 258.25,
                IsFilled = false,
                Customer = customerB,
            };

            await col.InsertAsync(new List <Order> {
                orderA, orderB
            });

            await col.DeleteMultipleAsync(new List <object> {
                "06a1bac6-b534-421d-a130-1441fe0ef5c6", "16db1209-ac9c-472f-bf76-5ba4dcecf2bd"
            });

            var orders = await col.GetAllAsync();

            Assert.Empty(orders);

            Directory.Delete(path, true);
        }
Пример #7
0
        public async Task TestGetByIdAsync()
        {
            var path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\CarboniteTest";
            var ct   = new CarboniteTool($"Path={path}");
            var stor = ct.GetStorage();

            var file = await stor.GetByIdAsync(1);

            Assert.Equal(1, file.Id);
            Assert.Equal("This is a test file.docx", file.Filename);
        }
Пример #8
0
        public async Task TestDownloadAsync()
        {
            var path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\CarboniteTest";
            var ct   = new CarboniteTool($"Path={path}");
            var stor = ct.GetStorage();

            var randomName  = Guid.NewGuid().ToString();
            var destination = Path.Combine(Path.GetTempPath(), randomName);

            await stor.DownloadAsync(1, destination);

            File.Delete(destination);
        }
Пример #9
0
        public async Task TestCopyToStreamAsync()
        {
            var path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\CarboniteTest";
            var ct   = new CarboniteTool($"Path={path}");
            var stor = ct.GetStorage();

            var randomName  = Guid.NewGuid().ToString();
            var destination = Path.Combine(Path.GetTempPath(), randomName);

            using (var destinationStream = new FileStream(destination, FileMode.Create))
                await stor.CopyFileToStreamAsync(1, destinationStream);

            File.Delete(destination);
        }
Пример #10
0
        public async Task TestUploadAsync()
        {
            var database = Guid.NewGuid().ToString();
            var path     = Path.Combine(Path.GetTempPath(), database);
            var ct       = new CarboniteTool($"Path={path}");
            var stor     = ct.GetStorage();

            var fileSource = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\filesource";

            await stor.UploadAsync(1, $"{Path.Combine(fileSource, "This is a test file.docx")}");

            await stor.UploadAsync(2, $"{Path.Combine(fileSource, "broken.pdf")}");

            Directory.Delete(path, true);
        }
Пример #11
0
        public async Task TestGetByIdsAsync()
        {
            var path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\CarboniteTest";
            var ct   = new CarboniteTool($"Path={path}");
            var stor = ct.GetStorage();

            var files = await stor.GetByIdsAsync(new List <object> {
                1, 2
            });

            Assert.Equal(2, files.Length);

            Assert.Equal(1, files[0].Id);
            Assert.Equal("This is a test file.docx", files[0].Filename);

            Assert.Equal(2, files[1].Id);
            Assert.Equal("broken.pdf", files[1].Filename);
        }
Пример #12
0
        public async Task TestDownloadOverloadAsync()
        {
            var path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\CarboniteTest";
            var ct   = new CarboniteTool($"Path={path}");
            var stor = ct.GetStorage();

            var randomName1  = Guid.NewGuid().ToString();
            var randomName2  = Guid.NewGuid().ToString();
            var destination1 = Path.Combine(Path.GetTempPath(), randomName1);
            var destination2 = Path.Combine(Path.GetTempPath(), randomName2);

            await stor.DownloadAsync(new List <object> {
                1, 2
            }, new List <string> {
                destination1, destination2
            });

            File.Delete(destination1);
            File.Delete(destination2);
        }
Пример #13
0
        public async Task TestGetByIdAsync()
        {
            var path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + "\\CarboniteTest";
            var ct   = new CarboniteTool($"Path={path}");
            var col  = ct.GetCollection <Order>();

            var order = await col.GetByIdAsync("06a1bac6-b534-421d-a130-1441fe0ef5c6");

            Assert.Equal(new Guid("06a1bac6-b534-421d-a130-1441fe0ef5c6"), order.Id);
            Assert.Equal(55.23, order.Subtotal);
            Assert.Equal(60.91, order.Total);
            Assert.True(order.IsFilled);

            Assert.Equal(2, order.Customer.Id);
            Assert.Equal("Bill", order.Customer.Firstname);
            Assert.Equal("Masterson", order.Customer.Lastname);
            Assert.Equal("*****@*****.**", order.Customer.Email);
            Assert.Equal("555-1234", order.Customer.Phone);
            Assert.Equal(DateTime.Parse("2008-11-01"), order.Customer.CustomerSince);
        }
Пример #14
0
        public async Task TestCopyToStreamOverloadAsync()
        {
            var path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\CarboniteTest";
            var ct   = new CarboniteTool($"Path={path}");
            var stor = ct.GetStorage();

            var randomName1  = Guid.NewGuid().ToString();
            var randomName2  = Guid.NewGuid().ToString();
            var destination1 = Path.Combine(Path.GetTempPath(), randomName1);
            var destination2 = Path.Combine(Path.GetTempPath(), randomName2);

            using (var destinationStream1 = new FileStream(destination1, FileMode.Create))
                using (var destinationStream2 = new FileStream(destination2, FileMode.Create))
                    await stor.CopyFileToStreamAsync(new List <object> {
                        1, 2
                    }, new List <Stream> {
                        destinationStream1, destinationStream2
                    });

            File.Delete(destination1);
            File.Delete(destination2);
        }
Пример #15
0
        public async Task TestSetFilenameAsync()
        {
            var database = Guid.NewGuid().ToString();
            var path     = Path.Combine(Path.GetTempPath(), database);
            var ct       = new CarboniteTool($"Path={path}");
            var stor     = ct.GetStorage();

            var fileSource = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + @"\filesource";

            await stor.UploadAsync(1, $"{Path.Combine(fileSource, "This is a test file.docx")}");

            var oldmeta = await stor.GetByIdAsync(1);

            Assert.Equal("This is a test file.docx", oldmeta.Filename);

            await stor.SetFilename(1, "NewFilename.docx");

            var newmeta = await stor.GetByIdAsync(1);

            Assert.Equal("NewFilename.docx", newmeta.Filename);

            Directory.Delete(path, true);
        }
Пример #16
0
        static async Task Main(string[] args)
        {
            var customerA = new Customer
            {
                Id            = 2,
                Firstname     = "Bill",
                Lastname      = "Masterson",
                Email         = "*****@*****.**",
                Phone         = "555-1234",
                CustomerSince = DateTime.Parse("2008-11-01"),
            };

            var customerB = new Customer
            {
                Id            = 3,
                Firstname     = "Jill",
                Lastname      = "James",
                Email         = "*****@*****.**",
                Phone         = "555-9456",
                CustomerSince = DateTime.Parse("2012-03-11"),
            };

            var customerC = new Customer
            {
                Id            = 4,
                Firstname     = "Armin",
                Lastname      = "Slate",
                Email         = "*****@*****.**",
                Phone         = "555-1956",
                CustomerSince = DateTime.Parse("2011-12-21"),
            };

            var newcustomers = new List <Customer> {
                customerA, customerB
            };

            var ct = new CarboniteTool(@"Path=C:\Temp\Test");
            var customerCollection = ct.GetCollection <Customer>();

            await customerCollection.InsertAsync(newcustomers);

            await customerCollection.InsertAsync(customerC);

            var customers = await customerCollection.GetAllAsync();

            foreach (Customer c in customers)
            {
                Console.WriteLine($"Customer {c.Firstname} {c.Lastname} has ID {c.Id}");
            }

            var id = new Guid("06a1bac6-b534-421d-a130-1441fe0ef5c6");

            var orderA = new Order
            {
                Id       = id,
                Subtotal = 55.23,
                Total    = 60.91,
                IsFilled = true,
                Customer = customerA,
            };

            var orderB = new Order
            {
                Id       = new Guid("16db1209-ac9c-472f-bf76-5ba4dcecf2bd"),
                Subtotal = 240.99,
                Total    = 258.25,
                IsFilled = false,
                Customer = customerC,
            };

            var orderCollection = ct.GetCollection <Order>("OrderNameOverride");

            await orderCollection.InsertAsync(orderA);

            await orderCollection.InsertAsync(orderB);

            var pullorder = await orderCollection.GetByIdAsync(id);

            Console.WriteLine($"Pulled order {pullorder.Id} for customer {pullorder.Customer.Firstname} {pullorder.Customer.Lastname} came to a total of {pullorder.Total}");

            foreach (Order o in await orderCollection.GetAllAsync())
            {
                Console.WriteLine($"Order {o.Id} for customer {o.Customer.Firstname} {o.Customer.Lastname} came to a total of {o.Total}");
            }

            var fileStore = ct.GetStorage();
            await fileStore.UploadAsync(1, @"C:\Temp\filesource\This is a test file.docx");

            await fileStore.UploadAsync(2, @"C:\Temp\filesource\broken.pdf");

            await fileStore.UploadAsync(3, @"C:\Temp\filesource\letter.txt");

            var dict = new Dictionary <string, string>
            {
                { "Description", "This is a description" },
                { "key2", "value2" },
                { "key3", "value3" },
                { "key4", "value4" },
            };

            await fileStore.SetMetadata(2, dict);

            var meta = await fileStore.GetByIdAsync(2);

            var metadata = await fileStore.GetAllAsync();

            foreach (FileStorageMetadata file in metadata)
            {
                if (file.Metadata.Count > 0)
                {
                    Console.WriteLine($"There is a file called \"{file.Filename}\" with ID {file.Id} in storage. It has metadata:");
                    foreach (KeyValuePair <string, string> pair in file.Metadata)
                    {
                        Console.WriteLine($"    Key: {pair.Key}, Value: {pair.Value}");
                    }
                }
                else
                {
                    Console.WriteLine($"There is a file called \"{file.Filename}\" with ID {file.Id} in storage. It has no metadata.");
                }
            }

            await fileStore.DownloadAsync(metadata[0].Id, $"C:\\Temp\\filedest\\{metadata[0].Filename}", true);
        }