public void ValidatePatchOperationSerialization()
        {
            int toCount   = 0;
            int fromCount = 0;

            CosmosSerializerHelper serializerHelper = new CosmosSerializerHelper(
                null,
                (input) => fromCount++,
                (input) => toCount++);

            CosmosSerializerCore  serializerCore = new CosmosSerializerCore(serializerHelper);
            List <PatchOperation> patch          = new List <PatchOperation>()
            {
                PatchOperation.Remove("/removePath")
            };

            Assert.AreEqual(0, toCount);

            // custom serializer is not used since operation type is Remove, which doesnt have "value" param to serialize
            using (Stream stream = serializerCore.ToStream(patch)) { }
            Assert.AreEqual(0, toCount);

            patch.Add(PatchOperation.Add("/addPath", "addValue"));
            // custom serializer is used since there is Add operation type also
            using (Stream stream = serializerCore.ToStream(patch)) { }
            Assert.AreEqual(1, toCount);
        }
        public void ValidatePatchOperationSerialization()
        {
            int toCount   = 0;
            int fromCount = 0;

            CosmosSerializerHelper serializerHelper = new CosmosSerializerHelper(
                null,
                (input) => fromCount++,
                (input) => toCount++);

            CosmosSerializerCore  serializerCore = new CosmosSerializerCore(serializerHelper);
            List <PatchOperation> patch          = new List <PatchOperation>()
            {
                PatchOperation.Remove("/removePath")
            };

            Assert.AreEqual(0, toCount);

            PatchItemRequestOptions patchRequestOptions = new PatchItemRequestOptions();

            // custom serializer is not used since operation type is Remove, which doesnt have "value" param to serialize
            using (Stream stream = serializerCore.ToStream(new PatchSpec(patch, patchRequestOptions))) { }
            Assert.AreEqual(0, toCount);

            patch.Add(PatchOperation.Add("/addPath", "addValue"));
            // custom serializer is used since there is Add operation type also
            using (Stream stream = serializerCore.ToStream(new PatchSpec(patch, patchRequestOptions))) { }
            Assert.AreEqual(1, toCount);

            patch.Clear();
            toCount = 0;
            patch.Add(PatchOperation.Add("/addPath", new CosmosJsonDotNetSerializer().ToStream("addValue")));
            // custom serializer is not used since the input value is of type stream
            using (Stream stream = serializerCore.ToStream(new PatchSpec(patch, patchRequestOptions))) { }
            Assert.AreEqual(0, toCount);
        }
        public void ValidateSqlQuerySpecSerialization()
        {
            int toCount   = 0;
            int fromCount = 0;

            CosmosSerializerHelper serializerHelper = new CosmosSerializerHelper(
                null,
                (input) => fromCount++,
                (input) => toCount++);

            CosmosSerializerCore serializerCore = new CosmosSerializerCore(serializerHelper);
            SqlQuerySpec         querySpec      = new SqlQuerySpec("select * from T where T.id = @id");

            querySpec.Parameters = new SqlParameterCollection()
            {
                new SqlParameter("@id", "testValue")
            };

            try
            {
                serializerCore.ToStream <SqlQuerySpec>(querySpec);
                Assert.Fail("ToStream should throw exception");
            }
            catch (ArgumentException e)
            {
                Assert.IsNotNull(e);
            }

            try
            {
                serializerCore.FromStream <SqlQuerySpec>(new MemoryStream());
                Assert.Fail("FromStream should throw exception");
            }
            catch (ArgumentException e)
            {
                Assert.IsNotNull(e);
            }

            Assert.AreEqual(0, toCount);
            Assert.AreEqual(0, fromCount);

            using (Stream stream = serializerCore.ToStreamSqlQuerySpec(querySpec, ResourceType.Offer)) { }

            Assert.AreEqual(0, toCount);
            Assert.AreEqual(0, fromCount);

            List <ResourceType> publicQuerySupportedTypes = new List <ResourceType>()
            {
                ResourceType.Database,
                ResourceType.Collection,
                ResourceType.Document,
                ResourceType.Trigger,
                ResourceType.UserDefinedFunction,
                ResourceType.StoredProcedure,
                ResourceType.Permission,
                ResourceType.User,
                ResourceType.Conflict
            };

            foreach (ResourceType resourceType in publicQuerySupportedTypes)
            {
                toCount = 0;

                using (Stream stream = serializerCore.ToStreamSqlQuerySpec(querySpec, resourceType))
                {
                    Assert.AreEqual(1, toCount);
                    Assert.AreEqual(0, fromCount);
                }
            }
        }