Click here to Skip to main content
15,881,588 members
Articles / Database Development / NoSQL

RavenDB - An Introduction

,
Rate me:
Please Sign up or sign in to vote.
4.87/5 (38 votes)
28 Apr 2010CPOL7 min read 261.4K   2.7K   112  
An introduction to RavenDB - a new open source .NET document database using .NET 4.0 and VS 2010
using System;
using Newtonsoft.Json.Linq;
using Raven.Database;
using Xunit;

namespace Raven.Tests.Storage
{
	public class CreateUpdateDeleteDocuments : AbstractDocumentStorageTest
	{
		private readonly DocumentDatabase db;

		public CreateUpdateDeleteDocuments()
		{
			db = new DocumentDatabase(new RavenConfiguration {DataDirectory = "raven.db.test.esent"});
		}

		#region IDisposable Members

		public override void Dispose()
		{
			db.Dispose();
			base.Dispose();
		}

		#endregion

		[Fact]
		public void When_creating_document_with_id_specified_will_return_specified_id()
		{
			var documentId = db.Put("1", Guid.Empty, JObject.Parse("{ first_name: 'ayende', last_name: 'rahien'}"),
			                        new JObject(), null);
			Assert.Equal("1", documentId.Key);
		}

		[Fact]
		public void Can_get_id_from_document_metadata()
		{
			db.Put("1", Guid.Empty, JObject.Parse("{ first_name: 'ayende', last_name: 'rahien'}"),
			       new JObject(), null);
			Assert.Equal("1", db.Get("1", null).Metadata["@id"].Value<string>());
		}

		[Fact]
		public void When_creating_document_with_no_id_specified_will_return_guid_as_id()
		{
			var documentId = db.Put(null, Guid.Empty, JObject.Parse("{ first_name: 'ayende', last_name: 'rahien'}"),
                                    new JObject(), null);
			Assert.DoesNotThrow(() => new Guid(documentId.Key));
		}

		[Fact]
		public void When_creating_documents_with_no_id_specified_will_return_guids_in_sequencal_order()
		{
			var documentId1 = db.Put(null, Guid.Empty, JObject.Parse("{ first_name: 'ayende', last_name: 'rahien'}"),
                                     new JObject(), null);
			var documentId2 = db.Put(null, Guid.Empty, JObject.Parse("{ first_name: 'ayende', last_name: 'rahien'}"),
                                     new JObject(), null);
            Assert.Equal(1, new Guid(documentId2.Key).CompareTo(new Guid(documentId1.Key)));
		}

		[Fact]
		public void Can_create_and_read_document()
		{
            db.Put("1", Guid.Empty, JObject.Parse("{  first_name: 'ayende', last_name: 'rahien'}"), new JObject(), null);
            var document = db.Get("1", null).ToJson();

			Assert.Equal("ayende", document.Value<string>("first_name"));
			Assert.Equal("rahien", document.Value<string>("last_name"));
		}

		[Fact]
		public void Can_edit_document()
		{
            db.Put("1", Guid.Empty, JObject.Parse("{ first_name: 'ayende', last_name: 'rahien'}"), new JObject(), null);

            db.Put("1", db.Get("1", null).Etag, JObject.Parse("{ first_name: 'ayende2', last_name: 'rahien2'}"), new JObject(), null);
            var document = db.Get("1", null).ToJson();

			Assert.Equal("ayende2", document.Value<string>("first_name"));
			Assert.Equal("rahien2", document.Value<string>("last_name"));
		}

		[Fact]
		public void Can_delete_document()
		{
            db.Put("1", Guid.Empty, JObject.Parse("{ first_name: 'ayende', last_name: 'rahien'}"), new JObject(), null);
            var document = db.Get("1", null);
            db.Delete("1", document.Etag, null);

            Assert.Null(db.Get("1", null));
		}

		[Fact]
		public void Can_query_document_by_id_when_having_multiple_documents()
		{
            db.Put("1", Guid.Empty, JObject.Parse("{ first_name: 'ayende', last_name: 'rahien'}"), new JObject(), null);
            db.Put("21", Guid.Empty, JObject.Parse("{ first_name: 'ayende2', last_name: 'rahien2'}"), new JObject(), null);
            var document = db.Get("21", null).ToJson();

			Assert.Equal("ayende2", document.Value<string>("first_name"));
			Assert.Equal("rahien2", document.Value<string>("last_name"));
		}

		[Fact]
		public void Querying_by_non_existant_document_returns_null()
		{
            Assert.Null(db.Get("1", null));
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.

Written By
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions