Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="QueryOperation"/> class.
        /// </summary>
        /// <param name="doc">The document representing the operation.</param>
        public QueryOperation(Document doc)
        {
            if (doc == null)
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "The value under 'query' is not a valid document.");
                return;
            }

            int foundFields = 0;

            if (doc.ContainsKey("fields") && doc["fields"].ValueType == DocumentEntryType.Document)
            {
                ++foundFields;
                _fields = doc["fields"].ValueAsDocument;
            }
            else if (doc.ContainsKey("fields"))
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "The 'fields' field is present, but is not a valid document.");
                return;
            }
            else
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "The 'fields' field is required for the query operation.");
                return;
            }

            if (foundFields != doc.Count)
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "The number of found fields in the 'query' document does not match the number of valid fields.");
                return;
            }

            _valid = true;
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AddOperation"/> class.
        /// </summary>
        /// <param name="doc">The document representing the operation.</param>
        public AddOperation(Document doc)
        {
            if (doc == null)
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "The value under 'add' is not a valid document.");
                return;
            }

            if (!doc.ContainsKey("document"))
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "The 'document' field is required for the add operation.");
                return;
            }

            if (doc["document"].ValueType != DocumentEntryType.Document)
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "The 'document' field is not a valid document.");
                return;
            }

            if (doc.Count != 1)
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "The number of found fields in the 'add' document does not match the number of valid fields.");
                return;
            }

            _document = doc["document"].ValueAsDocument;

            if (!_document.ContainsKey("id"))
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidId, "Document does not contain an id field.");
                return;
            }

            if (_document["id"].ValueType != DocumentEntryType.String)
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidId, "Document contains an id field that is not an ObjectId.");
                return;
            }

            try
            {
                _id = new ObjectId(_document["id"].ValueAsString);
            }
            catch (Exception)
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidId, "Document contains an id field that is not an ObjectId.");
                return;
            }

            _valid = true;
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RemoveOperation"/> class.
        /// </summary>
        /// <param name="doc">The document representing the operation.</param>
        public RemoveOperation(Document doc)
        {
            if (doc == null)
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "The value under 'remove' is not a valid document.");
                return;
            }

            if (doc.ContainsKey("documentId") && doc["documentId"].ValueType == DocumentEntryType.String)
            {
                try
                {
                    _documentId = new ObjectId(doc["documentId"].ValueAsString);
                }
                catch (Exception)
                {
                    _errorMessage = new DataOperationResult(ErrorCodes.InvalidId, "The 'documentId' field is not a valid ObjectId.");
                    return;
                }
            }
            else if (doc.ContainsKey("documentId"))
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidId, "The 'documentId' field is not a string value.");
                return;
            }
            else
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidId, "The 'documentId' field is required for the update operation.");
                return;
            }

            if (doc.Count != 1)
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "The number of found fields in the 'remove' document does not match the number of valid fields.");
            }

            _valid = true;
        }
Esempio n. 4
0
        public void AllJsonTypes()
        {
            string json = "{\"int\":10,\"string\":\"test\",\"bool\":true,\"float\":5.345,\"array\":[10,\"test\",{\"key\":\"value\"}],\"document\":{\"key\":\"value\"}}";

            DataOp.Document doc = new DataOp.Document(json);
            Assert.AreEqual(true, doc.Valid, "The document is not valid.");
            Assert.AreEqual(6, doc.Count, "The document does not contain the right number of items.");
            Assert.AreEqual(false, doc.CheckForSubkeys(), "The document had sub-keys.");
            Assert.AreEqual(json, doc.ToJson(), "The json is incorrect.");

            Assert.AreEqual(true, doc.ContainsKey("int"), "The document did not contain the key \"int\".");
            Assert.AreEqual(DataOp.DocumentEntryType.Integer, doc["int"].ValueType, "The value at key \"int\" was not an integer.");
            Assert.AreEqual(10, doc["int"].ValueAsInteger, "The value at key \"int\" was not 10.");

            Assert.AreEqual(true, doc.ContainsKey("string"), "The document did not contain the key \"string\".");
            Assert.AreEqual(DataOp.DocumentEntryType.String, doc["string"].ValueType, "The value at key \"string\" was not a string.");
            Assert.AreEqual("test", doc["string"].ValueAsString, "The value at key \"string\" was not \"test\".");

            Assert.AreEqual(true, doc.ContainsKey("bool"), "The document did not contain the key \"bool\".");
            Assert.AreEqual(DataOp.DocumentEntryType.Boolean, doc["bool"].ValueType, "The value at key \"bool\" was not a bool.");
            Assert.AreEqual(true, doc["bool"].ValueAsBoolean, "The value at key \"bool\" was not true.");

            Assert.AreEqual(true, doc.ContainsKey("float"), "The document did not contain the key \"float\".");
            Assert.AreEqual(DataOp.DocumentEntryType.Float, doc["float"].ValueType, "The value at key \"float\" was not a float.");
            Assert.That(doc["float"].ValueAsFloat, Is.EqualTo(5.345).Within(0.00001), "The value at key \"float\" was not 5.345.");

            Assert.AreEqual(true, doc.ContainsKey("array"), "The document did not contain the key \"array\".");
            Assert.AreEqual(DataOp.DocumentEntryType.Array, doc["array"].ValueType, "The value at key \"array\" was not an array.");
            var array = doc["array"].ValueAsArray;

            Assert.AreEqual(3, array.Count, "The array does not contain the right number of items.");
            Assert.AreEqual(DataOp.DocumentEntryType.Integer, array[0].ValueType, "The first value of the array is not an integer.");
            Assert.AreEqual(10, array[0].ValueAsInteger, "The first value of the array is not 10.");
            Assert.AreEqual(DataOp.DocumentEntryType.String, array[1].ValueType, "The second value of the array is not a string.");
            Assert.AreEqual("test", array[1].ValueAsString, "The second value of the array is not \"test\".");
            Assert.AreEqual(DataOp.DocumentEntryType.Document, array[2].ValueType, "The third value of the array is not a document.");
            Assert.True(array[2].ValueAsDocument.Equals(new DataOp.Document("{\"key\":\"value\"}")), "The third value of the array is not \"{\"key\":\"value\"}\".");

            Assert.AreEqual(true, doc.ContainsKey("document"), "The document did not contain the key \"document\".");
            Assert.AreEqual(DataOp.DocumentEntryType.Document, doc["document"].ValueType, "The value at key \"document\" was not a document.");
            Assert.True(doc["document"].ValueAsDocument.Equals(new DataOp.Document("{\"key\":\"value\"}")), "The value at key \"document\" was not \"{\"key\":\"value\"}\".");
            Assert.AreEqual(1, doc["document"].ValueAsDocument.Count, "The number of items in the document at key \"document\" was not 1.");
            Assert.AreEqual(DataOp.DocumentEntryType.String, doc["document"].ValueAsDocument["key"].ValueType, "The value at key \"key\" in the document at key \"document\" was not a string.");
            Assert.AreEqual("value", doc["document"].ValueAsDocument["key"].ValueAsString, "The value at key \"key\" in the document at key \"document\" was not \"value\".");
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="UpdateOperation"/> class.
        /// </summary>
        /// <param name="doc">The document representing the operation.</param>
        public UpdateOperation(Document doc)
        {
            if (doc == null)
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "The value under 'update' is not a valid document.");
                return;
            }

            int foundFields = 0;

            if (doc.ContainsKey("documentId") && doc["documentId"].ValueType == DocumentEntryType.String)
            {
                ++foundFields;
                try
                {
                    _documentId = new ObjectId(doc["documentId"].ValueAsString);
                }
                catch (Exception)
                {
                    _errorMessage = new DataOperationResult(ErrorCodes.InvalidId, "The 'documentId' field is not a valid ObjectId.");
                    return;
                }
            }
            else if (doc.ContainsKey("documentId"))
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidId, "The 'documentId' field is not a string value.");
                return;
            }
            else
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidId, "The 'documentId' field is required for the update operation.");
                return;
            }

            if (doc.ContainsKey("updateFields") && doc["updateFields"].ValueType == DocumentEntryType.Document)
            {
                ++foundFields;
                _updateFields = doc["updateFields"].ValueAsDocument;
            }
            else if (doc.ContainsKey("updateFields"))
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "The 'updateFields' field is present, but is not a valid document.");
                return;
            }

            if (doc.ContainsKey("removeFields") && doc["removeFields"].ValueType == DocumentEntryType.Array)
            {
                ++foundFields;
                _removeFields = new List <string>();
                foreach (var item in doc["removeFields"].ValueAsArray)
                {
                    if (item.ValueType != DocumentEntryType.String)
                    {
                        _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "Not all items in the 'removeFields' array is a string.");
                        return;
                    }

                    _removeFields.Add(item.ValueAsString);
                }
            }
            else if (doc.ContainsKey("removeFields"))
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "The 'removeFields' field is present, but is not a valid array.");
                return;
            }

            if (foundFields != doc.Count)
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "The number of found fields in the 'update' document does not match the number of valid fields.");
            }

            if (_updateFields.ContainsKey("id") || _removeFields.Contains("id"))
            {
                _errorMessage = new DataOperationResult(ErrorCodes.InvalidDocument, "Cannot modify the ObjectId of a document after it is created.");
                return;
            }

            _valid = true;
        }
Esempio n. 6
0
 /// <summary>
 /// Checks to see if the document matches the current query.
 /// </summary>
 /// <param name="doc">The document to test.</param>
 /// <returns>True if the document matches the query, otherwise false.</returns>
 public bool Match(Document doc)
 {
     return(doc.ContainsKey(_key) && _parts.All(e => MatchItem(doc, e)));
 }