-
Notifications
You must be signed in to change notification settings - Fork 820
/
CreateUpdateDeleteDocuments.cs
106 lines (88 loc) · 3.72 KB
/
CreateUpdateDeleteDocuments.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//-----------------------------------------------------------------------
// <copyright file="CreateUpdateDeleteDocuments.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using Raven.Json.Linq;
using Raven.Database;
using Raven.Database.Config;
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", RunInUnreliableYetFastModeThatIsNotSuitableForProduction = true});
}
#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, RavenJObject.Parse("{ first_name: 'ayende', last_name: 'rahien'}"),
new RavenJObject(), null);
Assert.Equal("1", documentId.Key);
}
[Fact]
public void Can_get_id_from_document_metadata()
{
db.Put("1", Guid.Empty, RavenJObject.Parse("{ first_name: 'ayende', last_name: 'rahien'}"),
new RavenJObject(), 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, RavenJObject.Parse("{ first_name: 'ayende', last_name: 'rahien'}"),
new RavenJObject(), null);
Assert.DoesNotThrow(() => new Guid(documentId.Key));
}
[Fact]
public void Can_create_and_read_document()
{
db.Put("1", Guid.Empty, RavenJObject.Parse("{ first_name: 'ayende', last_name: 'rahien'}"), new RavenJObject(), 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, RavenJObject.Parse("{ first_name: 'ayende', last_name: 'rahien'}"), new RavenJObject(), null);
db.Put("1", db.Get("1", null).Etag, RavenJObject.Parse("{ first_name: 'ayende2', last_name: 'rahien2'}"), new RavenJObject(), 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, RavenJObject.Parse("{ first_name: 'ayende', last_name: 'rahien'}"), new RavenJObject(), 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, RavenJObject.Parse("{ first_name: 'ayende', last_name: 'rahien'}"), new RavenJObject(), null);
db.Put("21", Guid.Empty, RavenJObject.Parse("{ first_name: 'ayende2', last_name: 'rahien2'}"), new RavenJObject(), 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));
}
}
}