Exemplo n.º 1
0
        public async Task Test_Can_Insert_And_Retrieve_ProtoBuf_Documents()
        {
            using (var db = await OpenTestPartitionAsync())
            {
                var location = db.Root["Books"]["ProtoBuf"];
                await CleanLocation(db, location);

                // quickly define the metatype for Books, because I'm too lazy to write a .proto for this, or add [ProtoMember] attributes everywhere
                var metaType = ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(Book), false);
                metaType.Add("Id", "Title", "Author", "Published", "Pages");
                metaType.CompileInPlace();

                var docs = new FdbDocumentCollection <Book, int>(
                    location,
                    (book) => book.Id,
                    new ProtobufCodec <Book>()
                    );

                var books = GetBooks();

                // store a document
                var book1 = books[0];
                await db.WriteAsync((tr) => docs.InsertAsync(tr, book1), this.Cancellation);

#if DEBUG
                await DumpSubspace(db, location);
#endif

                // retrieve the document
                var copy = await db.ReadAsync((tr) => docs.LoadAsync(tr, 42), this.Cancellation);

                Assert.That(copy, Is.Not.Null);
                Assert.That(copy.Id, Is.EqualTo(book1.Id));
                Assert.That(copy.Title, Is.EqualTo(book1.Title));
                Assert.That(copy.Author, Is.EqualTo(book1.Author));
                Assert.That(copy.Published, Is.EqualTo(book1.Published));
                Assert.That(copy.Pages, Is.EqualTo(book1.Pages));

                // store another document
                var book2 = books[1];
                await db.WriteAsync((tr) => docs.InsertAsync(tr, book2), this.Cancellation);

#if DEBUG
                await DumpSubspace(db, location);
#endif
            }
        }
Exemplo n.º 2
0
        public async Task Test_Can_Insert_And_Retrieve_Json_Documents()
        {
            using (var db = await OpenTestPartitionAsync())
            {
                var location = db.Root["Books"]["JSON"];
                await CleanLocation(db, location);

                var docs = new FdbDocumentCollection <Book, int>(
                    location,
                    (book) => book.Id,
                    new JsonNetCodec <Book>()
                    );

                var books = GetBooks();

                // store a document
                var book1 = books[0];
                await db.WriteAsync((tr) => docs.InsertAsync(tr, book1), this.Cancellation);

#if DEBUG
                await DumpSubspace(db, location);
#endif

                // retrieve the document
                var copy = await db.ReadAsync((tr) => docs.LoadAsync(tr, book1.Id), this.Cancellation);

                Assert.That(copy, Is.Not.Null);
                Assert.That(copy.Id, Is.EqualTo(book1.Id));
                Assert.That(copy.Title, Is.EqualTo(book1.Title));
                Assert.That(copy.Author, Is.EqualTo(book1.Author));
                Assert.That(copy.Published, Is.EqualTo(book1.Published));
                Assert.That(copy.Pages, Is.EqualTo(book1.Pages));

                // store another document
                var book2 = books[1];
                await db.WriteAsync((tr) => docs.InsertAsync(tr, book2), this.Cancellation);

#if DEBUG
                await DumpSubspace(db, location);
#endif
            }
        }
		public async Task Test_Can_Insert_And_Retrieve_Json_Documents()
		{
			using (var db = await OpenTestPartitionAsync())
			{
				var location = await GetCleanDirectory(db, "Books", "JSON");

				var docs = new FdbDocumentCollection<Book, int>(
					location,
					(book) => book.Id,
					new JsonNetCodec<Book>()
				);

				var books = GetBooks();

				// store a document
				var book1 = books[0];
				await db.WriteAsync((tr) => docs.Insert(tr, book1), this.Cancellation);
#if DEBUG
				await DumpSubspace(db, location);
#endif

				// retrieve the document
				var copy = await db.ReadAsync((tr) =>docs.LoadAsync(tr, book1.Id), this.Cancellation);

				Assert.That(copy, Is.Not.Null);
				Assert.That(copy.Id, Is.EqualTo(book1.Id));
				Assert.That(copy.Title, Is.EqualTo(book1.Title));
				Assert.That(copy.Author, Is.EqualTo(book1.Author));
				Assert.That(copy.Published, Is.EqualTo(book1.Published));
				Assert.That(copy.Pages, Is.EqualTo(book1.Pages));

				// store another document
				var book2 = books[1];
				await db.WriteAsync((tr) => docs.Insert(tr, book2), this.Cancellation);
#if DEBUG
				await DumpSubspace(db, location);
#endif
			}
		}
		public async Task Test_Can_Insert_And_Retrieve_ProtoBuf_Documents()
		{
			using (var db = await OpenTestPartitionAsync())
			{
				var location = await GetCleanDirectory(db, "Books", "ProtoBuf");

				// quickly define the metatype for Books, because I'm too lazy to write a .proto for this, or add [ProtoMember] attributes everywhere
				var metaType = ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(Book), false);
				metaType.Add("Id", "Title", "Author", "Published", "Pages");
				metaType.CompileInPlace();

				var docs = new FdbDocumentCollection<Book, int>(
					location,
					(book) => book.Id,
					new ProtobufCodec<Book>()
				);

				var books = GetBooks();

				// store a document
				var book1 = books[0];
				await db.WriteAsync((tr) => docs.Insert(tr, book1), this.Cancellation);
#if DEBUG
				await DumpSubspace(db, location);
#endif

				// retrieve the document
				var copy = await db.ReadAsync((tr) => docs.LoadAsync(tr, 42), this.Cancellation);

				Assert.That(copy, Is.Not.Null);
				Assert.That(copy.Id, Is.EqualTo(book1.Id));
				Assert.That(copy.Title, Is.EqualTo(book1.Title));
				Assert.That(copy.Author, Is.EqualTo(book1.Author));
				Assert.That(copy.Published, Is.EqualTo(book1.Published));
				Assert.That(copy.Pages, Is.EqualTo(book1.Pages));

				// store another document
				var book2 = books[1];
				await db.WriteAsync((tr) => docs.Insert(tr, book2), this.Cancellation);
#if DEBUG
				await DumpSubspace(db, location);
#endif
			}
		}