Example #1
0
 internal void TrySetHeaderParameter(string parameterName, Dictionary<string, object> parameters)
 {
     if (parameters.ContainsKey(parameterName))
     {
         Headers.Add(parameterName, parameters.String(parameterName));
     }
 }
        public void Should_replace_edge_with_waitForSync()
        {
            Database.ClearTestCollection(Database.TestEdgeCollectionName);
            var db = new ADatabase(Database.Alias);

            var document = new Dictionary<string, object>()
                .String("foo", "some string")
                .Int("bar", 12345);

            var createResult = db.Edge
                .Create(Database.TestEdgeCollectionName, _documents[0].String("_id"), _documents[1].String("_id"), document);

            var newDocument = new Dictionary<string, object>()
                .String("foo", "some other new string")
                .Int("baz", 54321);

            var replaceResult = db.Edge
                .WaitForSync(true)
                .Replace(createResult.Value.String("_id"), newDocument);

            Assert.AreEqual(201, replaceResult.StatusCode);
            Assert.IsTrue(replaceResult.Success);
            Assert.IsTrue(replaceResult.HasValue);
            Assert.AreEqual(replaceResult.Value.String("_id"), createResult.Value.String("_id"));
            Assert.AreEqual(replaceResult.Value.String("_key"), createResult.Value.String("_key"));
            Assert.AreNotEqual(replaceResult.Value.String("_rev"), createResult.Value.String("_rev"));

            var getResult = db.Edge
                .Get(replaceResult.Value.String("_id"));

            Assert.AreEqual(getResult.Value.String("_id"), replaceResult.Value.String("_id"));
            Assert.AreEqual(getResult.Value.String("_key"), replaceResult.Value.String("_key"));
            Assert.AreEqual(getResult.Value.String("_rev"), replaceResult.Value.String("_rev"));

            Assert.AreNotEqual(getResult.Value.String("foo"), document.String("foo"));
            Assert.AreEqual(getResult.Value.String("foo"), newDocument.String("foo"));

            Assert.AreEqual(getResult.Value.Int("baz"), newDocument.Int("baz"));

            Assert.IsFalse(getResult.Value.Has("bar"));
        }
        public void Should_replace_edge_with_ifMatch_and_return_412()
        {
            Database.ClearTestCollection(Database.TestEdgeCollectionName);
            var db = new ADatabase(Database.Alias);

            var document = new Dictionary<string, object>()
                .String("foo", "some string")
                .Int("bar", 12345);

            var createResult = db.Edge
                .Create(Database.TestEdgeCollectionName, _documents[0].String("_id"), _documents[1].String("_id"), document);

            document.Merge(createResult.Value);

            var newDocument = new Dictionary<string, object>()
                .String("foo", "some other new string")
                .Int("baz", 54321);

            var replaceResult = db.Edge
                .IfMatch("123456789")
                .Replace(createResult.Value.String("_id"), newDocument);

            Assert.AreEqual(412, replaceResult.StatusCode);
            Assert.IsFalse(replaceResult.Success);
            Assert.IsTrue(replaceResult.HasValue);
            Assert.AreEqual(replaceResult.Value.String("_id"), document.String("_id"));
            Assert.AreEqual(replaceResult.Value.String("_key"), document.String("_key"));
            Assert.AreEqual(replaceResult.Value.String("_rev"), document.String("_rev"));
        }
        public void Should_get_edge_as_generic_object()
        {
            Database.ClearTestCollection(Database.TestEdgeCollectionName);
            var db = new ADatabase(Database.Alias);

            var document = new Dictionary<string, object>()
                .String("foo", "foo string value")
                .Int("bar", 12345);

            var createResult = db.Edge
                .Create(Database.TestEdgeCollectionName, _documents[0].String("_id"), _documents[1].String("_id"), document);

            var getResult = db.Edge
                .Get<Dummy>(createResult.Value.String("_id"));

            Assert.AreEqual(200, getResult.StatusCode);
            Assert.IsTrue(getResult.Success);
            Assert.IsTrue(getResult.HasValue);
            Assert.AreEqual(document.String("foo"), getResult.Value.Foo);
            Assert.AreEqual(document.Int("bar"), getResult.Value.Bar);
            Assert.AreEqual(0, getResult.Value.Baz);
        }
        public void Should_create_edge()
        {
            Database.ClearTestCollection(Database.TestEdgeCollectionName);
            var db = new ADatabase(Database.Alias);

            var document = new Dictionary<string, object>()
                .String("foo", "foo string value")
                .Int("bar", 12345);

            var createResult = db.Edge
                .Create(Database.TestEdgeCollectionName, _documents[0].String("_id"), _documents[1].String("_id"), document);

            Assert.AreEqual(202, createResult.StatusCode);
            Assert.IsTrue(createResult.Success);
            Assert.IsTrue(createResult.HasValue);
            Assert.IsTrue(createResult.Value.IsString("_id"));
            Assert.IsTrue(createResult.Value.IsString("_key"));
            Assert.IsTrue(createResult.Value.IsString("_rev"));

            var getResult = db.Edge
                .Get(createResult.Value.String("_id"));

            Assert.AreEqual(200, getResult.StatusCode);
            Assert.IsTrue(getResult.Success);
            Assert.IsTrue(getResult.HasValue);
            Assert.AreEqual(getResult.Value.String("_id"), createResult.Value.String("_id"));
            Assert.AreEqual(getResult.Value.String("_key"), createResult.Value.String("_key"));
            Assert.AreEqual(getResult.Value.String("_rev"), createResult.Value.String("_rev"));
            Assert.IsTrue(getResult.Value.IsString("_from"));
            Assert.IsTrue(getResult.Value.IsString("_to"));
            Assert.AreEqual(getResult.Value.String("foo"), document.String("foo"));
            Assert.AreEqual(getResult.Value.Int("bar"), document.Int("bar"));
        }
        public void Should_update_document_with_keepNull()
        {
            var db = new ADatabase(Database.Alias);

            var newDocument = new Dictionary<string, object>()
                .String("foo", "some string")
                .Object("bar", null);

            var createResult = db.Document
                .Create(Database.TestDocumentCollectionName, newDocument);

            newDocument.Merge(createResult.Value);

            var document = new Dictionary<string, object>()
                .String("foo", "some other new string")
                .Object("baz", null);

            var updateResult = db.Document
                .KeepNull(false)
                .Update(newDocument.ID(), document);

            Assert.AreEqual(202, updateResult.StatusCode);
            Assert.IsTrue(updateResult.Success);
            Assert.IsTrue(updateResult.HasValue);
            Assert.AreEqual(updateResult.Value.ID(), newDocument.ID());
            Assert.AreEqual(updateResult.Value.Key(), newDocument.Key());
            Assert.AreNotEqual(updateResult.Value.Rev(), newDocument.Rev());

            var getResult = db.Document
                .Get(updateResult.Value.ID());

            Assert.AreEqual(getResult.Value.ID(), updateResult.Value.ID());
            Assert.AreEqual(getResult.Value.Key(), updateResult.Value.Key());
            Assert.AreEqual(getResult.Value.Rev(), updateResult.Value.Rev());

            Assert.AreNotEqual(getResult.Value.String("foo"), newDocument.String("foo"));
            Assert.AreEqual(getResult.Value.String("foo"), document.String("foo"));

            Assert.IsTrue(getResult.Value.Has("bar"));

            Assert.IsFalse(getResult.Value.Has("baz"));
        }
Example #7
0
 /// <summary>
 /// Creates index within specified collection in current database context.
 /// </summary>
 public AResult<Dictionary<string, object>> Create(string collectionName)
 {
     var request = new Request(HttpMethod.POST, ApiBaseUri.Index, "");
     var bodyDocument = new Dictionary<string, object>();
     
     // required
     request.QueryString.Add(ParameterName.Collection, collectionName);
     
     // required
     bodyDocument.String(ParameterName.Type, _parameters.String(ParameterName.Type));
     
     switch (_parameters.Enum<AIndexType>(ParameterName.Type))
     {
         case AIndexType.Cap:
             Request.TrySetBodyParameter(ParameterName.ByteSize, _parameters, bodyDocument);
             Request.TrySetBodyParameter(ParameterName.Size, _parameters, bodyDocument);
             break;
         case AIndexType.Fulltext:
             Request.TrySetBodyParameter(ParameterName.Fields, _parameters, bodyDocument);
             Request.TrySetBodyParameter(ParameterName.MinLength, _parameters, bodyDocument);
             break;
         case AIndexType.Geo:
             Request.TrySetBodyParameter(ParameterName.Fields, _parameters, bodyDocument);
             Request.TrySetBodyParameter(ParameterName.GeoJson, _parameters, bodyDocument);
             break;
         case AIndexType.Hash:
             Request.TrySetBodyParameter(ParameterName.Fields, _parameters, bodyDocument);
             Request.TrySetBodyParameter(ParameterName.Sparse, _parameters, bodyDocument);
             Request.TrySetBodyParameter(ParameterName.Unique, _parameters, bodyDocument);
             break;
         case AIndexType.Skiplist:
             Request.TrySetBodyParameter(ParameterName.Fields, _parameters, bodyDocument);
             Request.TrySetBodyParameter(ParameterName.Sparse, _parameters, bodyDocument);
             Request.TrySetBodyParameter(ParameterName.Unique, _parameters, bodyDocument);
             break;
         default:
             break;
     }
     
     request.Body = JSON.ToJSON(bodyDocument, ASettings.JsonParameters);
     
     var response = _connection.Send(request);
     var result = new AResult<Dictionary<string, object>>(response);
     
     switch (response.StatusCode)
     {
         case 200:
         case 201:
             if (response.DataType == DataType.Document)
             {
                 result.Value = (response.Data as Dictionary<string, object>);
                 result.Success = (result.Value != null);
             }
             break;
         case 400:
         case 404:
         default:
             // Arango error
             break;
     }
     
     _parameters.Clear();
     
     return result;
 }
        public void Should_update_document_with_mergeArrays_set_to_false()
        {
            var db = new ADatabase(Database.Alias);

            var newDocument = new Dictionary<string, object>()
                .String("foo", "some string")
                .Document("bar", new Dictionary<string, object>().String("foo", "string value"));

            var createResult = db.Document
                .Create(Database.TestDocumentCollectionName, newDocument);

            newDocument.Merge(createResult.Value);

            var document = new Dictionary<string, object>()
                .String("foo", "some other new string")
                .Document("bar", new Dictionary<string, object>().String("bar", "other string value"));

            var updateResult = db.Document
                .MergeObjects(false)
                .Update(newDocument.String("_id"), document);

            Assert.AreEqual(202, updateResult.StatusCode);
            Assert.IsTrue(updateResult.Success);
            Assert.IsTrue(updateResult.HasValue);
            Assert.AreEqual(updateResult.Value.String("_id"), newDocument.String("_id"));
            Assert.AreEqual(updateResult.Value.String("_key"), newDocument.String("_key"));
            Assert.AreNotEqual(updateResult.Value.String("_rev"), newDocument.String("_rev"));

            var getResult = db.Document
                .Get(updateResult.Value.String("_id"));

            Assert.AreEqual(getResult.Value.String("_id"), updateResult.Value.String("_id"));
            Assert.AreEqual(getResult.Value.String("_key"), updateResult.Value.String("_key"));
            Assert.AreEqual(getResult.Value.String("_rev"), updateResult.Value.String("_rev"));

            Assert.AreNotEqual(getResult.Value.String("foo"), newDocument.String("foo"));
            Assert.AreEqual(getResult.Value.String("foo"), document.String("foo"));

            Assert.IsFalse(getResult.Value.Has("bar.foo"));

            Assert.IsTrue(getResult.Value.Has("bar.bar"));
        }
        public void Should_create_document_with_custom_ID()
        {
            Database.ClearTestCollection(Database.TestDocumentCollectionName);
            var db = new ADatabase(Database.Alias);

            var document = new Dictionary<string, object>()
                .String("_key", "1234-5678")
                .String("foo", "foo string value")
                .Int("bar", 12345);

            var createResult = db.Document
                .Create(Database.TestDocumentCollectionName, document);

            Assert.AreEqual(202, createResult.StatusCode);
            Assert.IsTrue(createResult.Success);
            Assert.IsTrue(createResult.HasValue);
            Assert.AreEqual(Database.TestDocumentCollectionName + "/" + document.String("_key"), createResult.Value.String("_id"));
            Assert.AreEqual(document.String("_key"), createResult.Value.String("_key"));
            Assert.IsTrue(createResult.Value.IsString("_rev"));
        }
        public void Should_replace_document_with_ifMatch_and_lastUpdatePolicy()
        {
            var documents = Database.ClearCollectionAndFetchTestDocumentData(Database.TestDocumentCollectionName);
            var db = new ADatabase(Database.Alias);

            var document = new Dictionary<string, object>()
                .String("foo", "some other new string")
                .Int("baz", 54321);

            var replaceResult = db.Document
                .IfMatch("123456789", AUpdatePolicy.Last)
                .Replace(documents[0].String("_id"), document);

            Assert.AreEqual(202, replaceResult.StatusCode);
            Assert.IsTrue(replaceResult.Success);
            Assert.IsTrue(replaceResult.HasValue);
            Assert.AreEqual(replaceResult.Value.String("_id"), documents[0].String("_id"));
            Assert.AreEqual(replaceResult.Value.String("_key"), documents[0].String("_key"));
            Assert.AreNotEqual(replaceResult.Value.String("_rev"), documents[0].String("_rev"));

            var getResult = db.Document
                .Get(replaceResult.Value.String("_id"));

            Assert.AreEqual(getResult.Value.String("_id"), replaceResult.Value.String("_id"));
            Assert.AreEqual(getResult.Value.String("_key"), replaceResult.Value.String("_key"));
            Assert.AreEqual(getResult.Value.String("_rev"), replaceResult.Value.String("_rev"));

            Assert.AreNotEqual(getResult.Value.String("foo"), documents[0].String("foo"));
            Assert.AreEqual(getResult.Value.String("foo"), document.String("foo"));

            Assert.AreEqual(getResult.Value.Int("baz"), document.Int("baz"));

            Assert.IsFalse(getResult.Value.Has("bar"));
        }
        public void Should_replace_edge_with_ifMatch()
        {
            Database.ClearTestCollection(Database.TestEdgeCollectionName);
            var db = new ADatabase(Database.Alias);

            var document = new Dictionary<string, object>()
                .String("foo", "some string")
                .Int("bar", 12345);

            var createResult = db
                .Document
                .CreateEdge(Database.TestEdgeCollectionName, _documents[0].ID(), _documents[1].ID(), document);

            document.Merge(createResult.Value);

            var newDocument = new Dictionary<string, object>()
                .From(_documents[0].ID())
                .To(_documents[1].ID())
                .String("foo", "some other new string")
                .Int("baz", 54321);

            var replaceResult = db
                .Document
                .IfMatch(document.Rev())
                .Replace(createResult.Value.ID(), newDocument);

            Assert.AreEqual(202, replaceResult.StatusCode);
            Assert.IsTrue(replaceResult.Success);
            Assert.IsTrue(replaceResult.HasValue);
            Assert.AreEqual(replaceResult.Value.ID(), document.ID());
            Assert.AreEqual(replaceResult.Value.Key(), document.Key());
            Assert.AreNotEqual(replaceResult.Value.Rev(), document.Rev());

            var getResult = db
                .Document
                .Get(replaceResult.Value.ID());

            Assert.AreEqual(getResult.Value.ID(), replaceResult.Value.ID());
            Assert.AreEqual(getResult.Value.Key(), replaceResult.Value.Key());
            Assert.AreEqual(getResult.Value.Rev(), replaceResult.Value.Rev());

            Assert.AreNotEqual(getResult.Value.String("foo"), document.String("foo"));
            Assert.AreEqual(getResult.Value.String("foo"), newDocument.String("foo"));

            Assert.AreEqual(getResult.Value.Int("baz"), newDocument.Int("baz"));

            Assert.IsFalse(getResult.Value.Has("bar"));
        }
        public void Should_replace_edge_with_generic_object()
        {
            Database.ClearTestCollection(Database.TestEdgeCollectionName);
            var db = new ADatabase(Database.Alias);

            var document = new Dictionary<string, object>()
                .String("foo", "some string")
                .Int("bar", 12345);

            var createResult = db
                .Document
                .CreateEdge(Database.TestEdgeCollectionName, _documents[0].ID(), _documents[1].ID(), document);

            var dummy = new Dummy();
            dummy.Foo = "some other new string";
            dummy.Baz = 54321;

            var replaceResult = db
                .Document
                .ReplaceEdge(createResult.Value.ID(), _documents[0].ID(), _documents[1].ID(), dummy);

            Assert.AreEqual(202, replaceResult.StatusCode);
            Assert.IsTrue(replaceResult.Success);
            Assert.IsTrue(replaceResult.HasValue);
            Assert.AreEqual(replaceResult.Value.ID(), createResult.Value.ID());
            Assert.AreEqual(replaceResult.Value.Key(), createResult.Value.Key());
            Assert.AreNotEqual(replaceResult.Value.Rev(), createResult.Value.Rev());

            var getResult = db
                .Document
                .Get(replaceResult.Value.ID());

            Assert.AreEqual(getResult.Value.ID(), replaceResult.Value.ID());
            Assert.AreEqual(getResult.Value.Key(), replaceResult.Value.Key());
            Assert.AreEqual(getResult.Value.Rev(), replaceResult.Value.Rev());

            Assert.AreNotEqual(getResult.Value.String("foo"), document.String("foo"));
            Assert.AreEqual(getResult.Value.String("foo"), dummy.Foo);

            Assert.AreEqual(getResult.Value.Int("baz"), dummy.Baz);

            Assert.AreEqual(0, getResult.Value.Int("bar"));
        }
        public void Should_create_edge_with_returnNew_parameter()
        {
            Database.ClearTestCollection(Database.TestEdgeCollectionName);
            var db = new ADatabase(Database.Alias);

            var document = new Dictionary<string, object>()
                .From(_documents[0].ID())
                .To(_documents[1].ID())
                .String("foo", "foo string value")
                .Int("bar", 12345);

            var createResult = db
                .Document
                .ReturnNew()
                .CreateEdge(Database.TestEdgeCollectionName, document);

            Assert.AreEqual(202, createResult.StatusCode);
            Assert.IsTrue(createResult.Success);
            Assert.IsTrue(createResult.HasValue);
            Assert.IsTrue(createResult.Value.IsString("_id"));
            Assert.IsTrue(createResult.Value.IsString("_key"));
            Assert.IsTrue(createResult.Value.IsString("_rev"));
            Assert.IsTrue(createResult.Value.Has("new"));
            Assert.AreEqual(createResult.Value.ID(), createResult.Value.String("new._id"));
            Assert.AreEqual(createResult.Value.Key(), createResult.Value.String("new._key"));
            Assert.AreEqual(createResult.Value.Rev(), createResult.Value.String("new._rev"));
            Assert.AreEqual(document.String("_from"), createResult.Value.String("new._from"));
            Assert.AreEqual(document.String("_to"), createResult.Value.String("new._to"));
            Assert.AreEqual(document.String("foo"), createResult.Value.String("new.foo"));
            Assert.AreEqual(document.Int("bar"), createResult.Value.Int("new.bar"));
        }
        public void Should_update_edge_with_generic_object()
        {
            Database.ClearTestCollection(Database.TestEdgeCollectionName);
            var db = new ADatabase(Database.Alias);

            var document = new Dictionary<string, object>()
                .String("foo", "some string")
                .Int("bar", 12345);

            var createResult = db.Edge
                .Create(Database.TestEdgeCollectionName, _documents[0].String("_id"), _documents[1].String("_id"), document);

            var dummy = new Dummy();
            dummy.Foo = "some other new string";
            dummy.Bar = 54321;
            dummy.Baz = 12345;

            var updateResult = db.Edge
                .Update(createResult.Value.String("_id"), dummy);

            Assert.AreEqual(202, updateResult.StatusCode);
            Assert.IsTrue(updateResult.Success);
            Assert.IsTrue(updateResult.HasValue);
            Assert.AreEqual(updateResult.Value.String("_id"), createResult.Value.String("_id"));
            Assert.AreEqual(updateResult.Value.String("_key"), createResult.Value.String("_key"));
            Assert.AreNotEqual(updateResult.Value.String("_rev"), createResult.Value.String("_rev"));

            var getResult = db.Edge
                .Get(updateResult.Value.String("_id"));

            Assert.AreEqual(getResult.Value.String("_id"), updateResult.Value.String("_id"));
            Assert.AreEqual(getResult.Value.String("_key"), updateResult.Value.String("_key"));
            Assert.AreEqual(getResult.Value.String("_rev"), updateResult.Value.String("_rev"));

            Assert.AreNotEqual(getResult.Value.String("foo"), document.String("foo"));
            Assert.AreEqual(getResult.Value.String("foo"), dummy.Foo);

            Assert.AreNotEqual(getResult.Value.Int("bar"), document.Int("bar"));
            Assert.AreEqual(getResult.Value.Int("bar"), dummy.Bar);
            Assert.AreEqual(getResult.Value.Int("baz"), dummy.Baz);
        }
        public void Should_update_document_with_waitForSync()
        {
            var documents = Database.ClearCollectionAndFetchTestDocumentData(Database.TestDocumentCollectionName);
            var db = new ADatabase(Database.Alias);

            var document = new Dictionary<string, object>()
                .String("foo", "some other new string")
                .Int("bar", 54321)
                .Int("baz", 12345);

            var updateResult = db.Document
                .WaitForSync(true)
                .Update(documents[0].String("_id"), document);

            Assert.AreEqual(201, updateResult.StatusCode);
            Assert.IsTrue(updateResult.Success);
            Assert.IsTrue(updateResult.HasValue);
            Assert.AreEqual(updateResult.Value.String("_id"), documents[0].String("_id"));
            Assert.AreEqual(updateResult.Value.String("_key"), documents[0].String("_key"));
            Assert.AreNotEqual(updateResult.Value.String("_rev"), documents[0].String("_rev"));

            var getResult = db.Document
                .Get(updateResult.Value.String("_id"));

            Assert.AreEqual(getResult.Value.String("_id"), updateResult.Value.String("_id"));
            Assert.AreEqual(getResult.Value.String("_key"), updateResult.Value.String("_key"));
            Assert.AreEqual(getResult.Value.String("_rev"), updateResult.Value.String("_rev"));

            Assert.AreNotEqual(getResult.Value.String("foo"), documents[0].String("foo"));
            Assert.AreEqual(getResult.Value.String("foo"), document.String("foo"));

            Assert.AreNotEqual(getResult.Value.Int("bar"), documents[0].Int("bar"));
            Assert.AreEqual(getResult.Value.Int("bar"), document.Int("bar"));
            Assert.AreEqual(getResult.Value.Int("baz"), document.Int("baz"));
        }
        public void Should_update_edge_with_ifMatch()
        {
            Database.ClearTestCollection(Database.TestEdgeCollectionName);
            var db = new ADatabase(Database.Alias);

            var document = new Dictionary<string, object>()
                .String("foo", "some string")
                .Int("bar", 12345);

            var createResult = db.Edge
                .Create(Database.TestEdgeCollectionName, _documents[0].String("_id"), _documents[1].String("_id"), document);

            var newDocument = new Dictionary<string, object>()
                .String("foo", "some other new string")
                .Int("bar", 54321)
                .Int("baz", 12345);

            var updateResult = db.Edge
                .IfMatch(createResult.Value.String("_rev"))
                .Update(createResult.Value.String("_id"), newDocument);

            Assert.AreEqual(202, updateResult.StatusCode);
            Assert.IsTrue(updateResult.Success);
            Assert.IsTrue(updateResult.HasValue);
            Assert.AreEqual(updateResult.Value.String("_id"), createResult.Value.String("_id"));
            Assert.AreEqual(updateResult.Value.String("_key"), createResult.Value.String("_key"));
            Assert.AreNotEqual(updateResult.Value.String("_rev"), createResult.Value.String("_rev"));

            var getResult = db.Edge
                .Get(updateResult.Value.String("_id"));

            Assert.AreEqual(getResult.Value.String("_id"), updateResult.Value.String("_id"));
            Assert.AreEqual(getResult.Value.String("_key"), updateResult.Value.String("_key"));
            Assert.AreEqual(getResult.Value.String("_rev"), updateResult.Value.String("_rev"));

            Assert.AreNotEqual(getResult.Value.String("foo"), document.String("foo"));
            Assert.AreEqual(getResult.Value.String("foo"), newDocument.String("foo"));

            Assert.AreNotEqual(getResult.Value.Int("bar"), document.Int("bar"));
            Assert.AreEqual(getResult.Value.Int("bar"), newDocument.Int("bar"));

            // by default JSON integers are deserialized to long type
            Assert.IsTrue(getResult.Value.IsLong("baz"));
        }
        public void Should_update_edge_with_keepNull()
        {
            Database.ClearTestCollection(Database.TestEdgeCollectionName);
            var db = new ADatabase(Database.Alias);

            var document = new Dictionary<string, object>()
                .String("foo", "some string")
                .Object("bar", null);

            var createResult = db.Edge
                .Create(Database.TestEdgeCollectionName, _documents[0].String("_id"), _documents[1].String("_id"), document);

            document.Merge(createResult.Value);

            var newDocument = new Dictionary<string, object>()
                .String("foo", "some other new string")
                .Object("baz", null);

            var updateResult = db.Edge
                .KeepNull(false)
                .Update(createResult.Value.String("_id"), newDocument);

            Assert.AreEqual(202, updateResult.StatusCode);
            Assert.IsTrue(updateResult.Success);
            Assert.IsTrue(updateResult.HasValue);
            Assert.AreEqual(updateResult.Value.String("_id"), document.String("_id"));
            Assert.AreEqual(updateResult.Value.String("_key"), document.String("_key"));
            Assert.AreNotEqual(updateResult.Value.String("_rev"), document.String("_rev"));

            var getResult = db.Document
                .Get(updateResult.Value.String("_id"));

            Assert.AreEqual(getResult.Value.String("_id"), updateResult.Value.String("_id"));
            Assert.AreEqual(getResult.Value.String("_key"), updateResult.Value.String("_key"));
            Assert.AreEqual(getResult.Value.String("_rev"), updateResult.Value.String("_rev"));

            Assert.AreNotEqual(getResult.Value.String("foo"), document.String("foo"));
            Assert.AreEqual(getResult.Value.String("foo"), newDocument.String("foo"));

            Assert.IsTrue(getResult.Value.Has("bar"));

            Assert.IsFalse(getResult.Value.Has("baz"));
        }
Example #18
0
        /// <summary>
        /// Creates new database with given name and user list.
        /// </summary>
        public AResult<bool> Create(string databaseName, List<AUser> users)
        {
            var request = new Request(HttpMethod.POST, ApiBaseUri.Database, "");
            var bodyDocument = new Dictionary<string, object>();

            // required: database name
            bodyDocument.String("name", databaseName);

            // optional: list of users
            if ((users != null) && (users.Count > 0))
            {
                var userList = new List<Dictionary<string, object>>();

                foreach (var user in users)
                {
                    var userItem = new Dictionary<string, object>();

                    if (!string.IsNullOrEmpty(user.Username))
                    {
                        userItem.String("username", user.Username);
                    }

                    if (!string.IsNullOrEmpty(user.Password))
                    {
                        userItem.String("passwd", user.Password);
                    }

                    userItem.Bool("active", user.Active);

                    if (user.Extra != null)
                    {
                        userItem.Document("extra", user.Extra);
                    }

                    userList.Add(userItem);
                }

                bodyDocument.List("users", userList);
            }

            request.Body = JSON.ToJSON(bodyDocument, ASettings.JsonParameters);

            var response = _connection.Send(request);
            var result = new AResult<bool>(response);

            switch (response.StatusCode)
            {
                case 201:
                    var body = response.ParseBody<Body<bool>>();

                    result.Success = (body != null);
                    result.Value = body.Result;
                    break;
                case 400:
                case 403:
                case 404:
                default:
                    // Arango error
                    break;
            }

            _parameters.Clear();

            return result;
        }
        public void Should_update_edge_with_mergeArrays_set_to_true()
        {
            Database.ClearTestCollection(Database.TestEdgeCollectionName);
            var db = new ADatabase(Database.Alias);

            var document = new Dictionary<string, object>()
                .String("foo", "some string")
                .Document("bar", new Dictionary<string, object>().String("foo", "string value"));

            var createResult = db.Edge
                .Create(Database.TestEdgeCollectionName, _documents[0].String("_id"), _documents[1].String("_id"), document);

            document.Merge(createResult.Value);

            var newDocument = new Dictionary<string, object>()
                .String("foo", "some other new string")
                .Document("bar", new Dictionary<string, object>().String("bar", "other string value"));

            var updateResult = db.Edge
                .MergeObjects(true) // this is also default behavior
                .Update(createResult.Value.String("_id"), newDocument);

            Assert.AreEqual(202, updateResult.StatusCode);
            Assert.IsTrue(updateResult.Success);
            Assert.IsTrue(updateResult.HasValue);
            Assert.AreEqual(updateResult.Value.String("_id"), document.String("_id"));
            Assert.AreEqual(updateResult.Value.String("_key"), document.String("_key"));
            Assert.AreNotEqual(updateResult.Value.String("_rev"), document.String("_rev"));

            var getResult = db.Document
                .Get(updateResult.Value.String("_id"));

            Assert.AreEqual(getResult.Value.String("_id"), updateResult.Value.String("_id"));
            Assert.AreEqual(getResult.Value.String("_key"), updateResult.Value.String("_key"));
            Assert.AreEqual(getResult.Value.String("_rev"), updateResult.Value.String("_rev"));

            Assert.AreNotEqual(getResult.Value.String("foo"), document.String("foo"));
            Assert.AreEqual(getResult.Value.String("foo"), newDocument.String("foo"));

            Assert.IsTrue(getResult.Value.Has("bar.foo"));

            Assert.IsTrue(getResult.Value.Has("bar.bar"));
        }
Example #20
0
        /// <summary>
        /// Creates new collection in current database context.
        /// </summary>
        public AResult<Dictionary<string, object>> Create(string collectionName)
        {
            var request = new Request(HttpMethod.POST, ApiBaseUri.Collection, "");
            var bodyDocument = new Dictionary<string, object>();

            // required
            bodyDocument.String(ParameterName.Name, collectionName);
            // optional
            Request.TrySetBodyParameter(ParameterName.Type, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.WaitForSync, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.JournalSize, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.DoCompact, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.IsSystem, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.IsVolatile, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.KeyOptionsType, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.KeyOptionsAllowUserKeys, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.KeyOptionsIncrement, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.KeyOptionsOffset, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.NumberOfShards, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.ShardKeys, _parameters, bodyDocument);

            request.Body = JSON.ToJSON(bodyDocument, ASettings.JsonParameters);

            var response = _connection.Send(request);
            var result = new AResult<Dictionary<string, object>>(response);

            switch (response.StatusCode)
            {
                case 200:
                    var body = response.ParseBody<Dictionary<string, object>>();

                    result.Success = (body != null);
                    result.Value = body;
                    break;
                default:
                    // Arango error
                    break;
            }

            _parameters.Clear();

            return result;
        }
        public void Should_replace_document_with_waitForSync()
        {
            var documents = Database.ClearCollectionAndFetchTestDocumentData(Database.TestDocumentCollectionName);
            var db = new ADatabase(Database.Alias);

            var document = new Dictionary<string, object>()
                .String("foo", "some other new string")
                .Int("baz", 54321);

            var replaceResult = db.Document
                .WaitForSync(true)
                .Replace(documents[0].ID(), document);

            Assert.AreEqual(201, replaceResult.StatusCode);
            Assert.IsTrue(replaceResult.Success);
            Assert.IsTrue(replaceResult.HasValue);
            Assert.AreEqual(replaceResult.Value.ID(), documents[0].ID());
            Assert.AreEqual(replaceResult.Value.Key(), documents[0].Key());
            Assert.AreNotEqual(replaceResult.Value.Rev(), documents[0].Rev());

            var getResult = db.Document
                .Get(replaceResult.Value.ID());

            Assert.AreEqual(getResult.Value.ID(), replaceResult.Value.ID());
            Assert.AreEqual(getResult.Value.Key(), replaceResult.Value.Key());
            Assert.AreEqual(getResult.Value.Rev(), replaceResult.Value.Rev());

            Assert.AreNotEqual(getResult.Value.String("foo"), documents[0].String("foo"));
            Assert.AreEqual(getResult.Value.String("foo"), document.String("foo"));

            Assert.AreEqual(getResult.Value.Int("baz"), document.Int("baz"));

            Assert.IsFalse(getResult.Value.Has("bar"));
        }