Ejemplo n.º 1
0
        private static ZumoTest CreateDeleteTest(string testName, bool useTypedTable, DeleteTestType testType)
        {
            if (useTypedTable && testType == DeleteTestType.NoIdField)
            {
                throw new ArgumentException("Cannot send a delete request without an id field on a typed table.");
            }

            return(new ZumoTest(testName, async delegate(ZumoTest test)
            {
                var client = ZumoTestGlobals.Instance.Client;
                var typedTable = client.GetTable <RoundTripTableItem>();
                var untypedTable = client.GetTable(ZumoTestGlobals.RoundTripTableName);
                RoundTripTableItem itemToInsert = new RoundTripTableItem
                {
                    String1 = "hello",
                    Bool1 = true,
                    Int1 = 123,
                };
                await typedTable.InsertAsync(itemToInsert);
                test.AddLog("Inserted item to be deleted");
                int id = itemToInsert.Id;
                switch (testType)
                {
                case DeleteTestType.ValidDelete:
                    if (useTypedTable)
                    {
                        await typedTable.DeleteAsync(itemToInsert);
                    }
                    else
                    {
                        await untypedTable.DeleteAsync(JsonObject.Parse("{\"id\":" + id + "}"));
                    }

                    test.AddLog("Delete succeeded; verifying that object isn't in the service anymore.");
                    try
                    {
                        var response = await untypedTable.LookupAsync(id);
                        test.AddLog("Error, delete succeeded, but item was returned by the service: {0}", response.Stringify());
                        return false;
                    }
                    catch (MobileServiceInvalidOperationException msioe)
                    {
                        return Validate404Response(test, msioe);
                    }

                case DeleteTestType.NonExistingId:
                    try
                    {
                        if (useTypedTable)
                        {
                            itemToInsert.Id = 1000000000;
                            await typedTable.DeleteAsync(itemToInsert);
                        }
                        else
                        {
                            JsonObject jo = new JsonObject();
                            jo.Add("id", JsonValue.CreateNumberValue(1000000000));
                            await untypedTable.DeleteAsync(jo);
                        }

                        test.AddLog("Error, deleting item with non-existing id should fail, but succeeded");
                        return false;
                    }
                    catch (MobileServiceInvalidOperationException msioe)
                    {
                        return Validate404Response(test, msioe);
                    }

                default:
                    try
                    {
                        JsonObject jo = new JsonObject();
                        jo.Add("Name", JsonValue.CreateStringValue("hello"));
                        await untypedTable.DeleteAsync(jo);

                        test.AddLog("Error, deleting item with non-existing id should fail, but succeeded");
                        return false;
                    }
                    catch (ArgumentException ex)
                    {
                        test.AddLog("Caught expected exception - {0}: {1}", ex.GetType().FullName, ex.Message);
                        return true;
                    }
                }
            }));
        }
        private async Task CreateDeleteTest <TItemType>(string testName, bool useTypedTable, DeleteTestType testType) where TItemType : ICloneableItem <TItemType>
        {
            if (useTypedTable && testType == DeleteTestType.NoIdField)
            {
                throw new ArgumentException("Cannot send a delete request without an id field on a typed table.");
            }

            var       client           = GetClient();
            var       typedTable       = client.GetTable <TItemType>();
            var       useStringIdTable = typeof(TItemType) == typeof(RoundTripTableItem);
            var       untypedTable     = client.GetTable(useStringIdTable ? "RoundTripTable" : "IntIdRoundTripTable");
            TItemType itemToInsert;

            if (useStringIdTable)
            {
                itemToInsert = (TItemType)(object)new RoundTripTableItem {
                    Name = "will be deleted", Number = 123
                };
            }
            else
            {
                itemToInsert = (TItemType)(object)new IntIdRoundTripTableItem {
                    Name = "will be deleted", Number = 123
                };
            }

            await typedTable.InsertAsync(itemToInsert);

            object id = itemToInsert.Id;

            switch (testType)
            {
            case DeleteTestType.ValidDelete:
                if (useTypedTable)
                {
                    await typedTable.DeleteAsync(itemToInsert);
                }
                else
                {
                    await untypedTable.DeleteAsync(new JObject(new JProperty("id", id)));
                }

                var vsioe = await Assert.ThrowsAsync <MobileServiceInvalidOperationException>(() => untypedTable.LookupAsync(id));

                Assert.True(Validate404Response(vsioe));
                return;

            case DeleteTestType.NonExistingId:
                var msioe = await Assert.ThrowsAsync <MobileServiceInvalidOperationException>(async() =>
                {
                    object nonExistingId = useStringIdTable ? (object)Guid.NewGuid().ToString() : (object)1000000000;
                    if (useTypedTable)
                    {
                        itemToInsert.Id = nonExistingId;
                        await typedTable.DeleteAsync(itemToInsert);
                    }
                    else
                    {
                        JObject jo = new JObject(new JProperty("id", nonExistingId));
                        await untypedTable.DeleteAsync(jo);
                    }
                });

                Assert.True(Validate404Response(msioe));
                return;

            default:
                await Assert.ThrowsAsync <ArgumentException>(async() =>
                {
                    JObject jo = new JObject(new JProperty("Name", "hello"));
                    await untypedTable.DeleteAsync(jo);
                });

                return;
            }
        }
Ejemplo n.º 3
0
        private static ZumoTest CreateDeleteTest <TItemType>(string testName, bool useTypedTable, DeleteTestType testType) where TItemType : ICloneableItem <TItemType>
        {
            if (useTypedTable && testType == DeleteTestType.NoIdField)
            {
                throw new ArgumentException("Cannot send a delete request without an id field on a typed table.");
            }

            return(new ZumoTest(testName, async delegate(ZumoTest test)
            {
                var client = ZumoTestGlobals.Instance.Client;
                var typedTable = client.GetTable <TItemType>();
                var useStringIdTable = typeof(TItemType) == typeof(StringIdRoundTripTableItem);
                var untypedTable = client.GetTable(
                    useStringIdTable ?
                    ZumoTestGlobals.StringIdRoundTripTableName :
                    ZumoTestGlobals.RoundTripTableName);
                TItemType itemToInsert;
                if (useStringIdTable)
                {
                    itemToInsert = (TItemType)(object)new StringIdRoundTripTableItem {
                        Name = "will be deleted", Number = 123
                    };
                }
                else
                {
                    itemToInsert = (TItemType)(object)new RoundTripTableItem {
                        String1 = "will be deleted", Int1 = 123
                    };
                }

                await typedTable.InsertAsync(itemToInsert);
                test.AddLog("Inserted item to be deleted");
                object id = itemToInsert.Id;
                switch (testType)
                {
                case DeleteTestType.ValidDelete:
                    if (useTypedTable)
                    {
                        await typedTable.DeleteAsync(itemToInsert);
                    }
                    else
                    {
                        await untypedTable.DeleteAsync(new JObject(new JProperty("id", id)));
                    }

                    test.AddLog("Delete succeeded; verifying that object isn't in the service anymore.");
                    try
                    {
                        var response = await untypedTable.LookupAsync(id);
                        test.AddLog("Error, delete succeeded, but item was returned by the service: {0}", response);
                        return false;
                    }
                    catch (MobileServiceInvalidOperationException msioe)
                    {
                        return Validate404Response(test, msioe);
                    }

                case DeleteTestType.NonExistingId:
                    try
                    {
                        object nonExistingId = useStringIdTable ? (object)Guid.NewGuid().ToString() : (object)1000000000;
                        if (useTypedTable)
                        {
                            itemToInsert.Id = nonExistingId;
                            await typedTable.DeleteAsync(itemToInsert);
                        }
                        else
                        {
                            JObject jo = new JObject(new JProperty("id", nonExistingId));
                            await untypedTable.DeleteAsync(jo);
                        }

                        test.AddLog("Error, deleting item with non-existing id should fail, but succeeded");
                        return false;
                    }
                    catch (MobileServiceInvalidOperationException msioe)
                    {
                        return Validate404Response(test, msioe);
                    }

                default:
                    try
                    {
                        JObject jo = new JObject(new JProperty("Name", "hello"));
                        await untypedTable.DeleteAsync(jo);

                        test.AddLog("Error, deleting item without an id should fail, but succeeded");
                        return false;
                    }
                    catch (ArgumentException ex)
                    {
                        test.AddLog("Caught expected exception - {0}: {1}", ex.GetType().FullName, ex.Message);
                        return true;
                    }
                }
            }));
        }
        private async Task CreateDeleteTest <TItemType>(string testName, bool useTypedTable, DeleteTestType testType) where TItemType : ICloneableItem <TItemType>
        {
            if (useTypedTable && testType == DeleteTestType.NoIdField)
            {
                throw new ArgumentException("Cannot send a delete request without an id field on a typed table.");
            }

            var       client           = GetClient();
            var       typedTable       = client.GetTable <TItemType>();
            var       useStringIdTable = typeof(TItemType) == typeof(RoundTripTableItem);
            var       untypedTable     = client.GetTable(useStringIdTable ? "RoundTripTable" : "IntIdRoundTripTable");
            TItemType itemToInsert;

            if (useStringIdTable)
            {
                itemToInsert = (TItemType)(object)new RoundTripTableItem {
                    Name = "will be deleted", Number = 123
                };
            }
            else
            {
                itemToInsert = (TItemType)(object)new IntIdRoundTripTableItem {
                    Name = "will be deleted", Number = 123
                };
            }

            await typedTable.InsertAsync(itemToInsert);

            Log("Inserted item to be deleted");
            object id = itemToInsert.Id;

            switch (testType)
            {
            case DeleteTestType.ValidDelete:
                if (useTypedTable)
                {
                    await typedTable.DeleteAsync(itemToInsert);
                }
                else
                {
                    await untypedTable.DeleteAsync(new JObject(new JProperty("id", id)));
                }

                Log("Delete succeeded; verifying that object isn't in the service anymore.");
                try
                {
                    var response = await untypedTable.LookupAsync(id);

                    Assert.Fail("Error, delete succeeded, but item was returned by the service: " + response);
                }
                catch (MobileServiceInvalidOperationException msioe)
                {
                    Assert.IsTrue(Validate404Response(msioe));
                }
                return;

            case DeleteTestType.NonExistingId:
                try
                {
                    object nonExistingId = useStringIdTable ? (object)Guid.NewGuid().ToString() : (object)1000000000;
                    if (useTypedTable)
                    {
                        itemToInsert.Id = nonExistingId;
                        await typedTable.DeleteAsync(itemToInsert);
                    }
                    else
                    {
                        JObject jo = new JObject(new JProperty("id", nonExistingId));
                        await untypedTable.DeleteAsync(jo);
                    }
                    Assert.Fail("Error, deleting item with non-existing id should fail, but succeeded");
                }
                catch (MobileServiceInvalidOperationException msioe)
                {
                    Assert.IsTrue(Validate404Response(msioe));
                }
                return;

            default:
                try
                {
                    JObject jo = new JObject(new JProperty("Name", "hello"));
                    await untypedTable.DeleteAsync(jo);

                    Assert.Fail("Error, deleting item without an id should fail, but succeeded");
                }
                catch (ArgumentException ex)
                {
                    Log("Caught expected exception - {0}: {1}", ex.GetType().FullName, ex.Message);
                }
                return;
            }
        }