Example #1
0
        public void ChangeTracking_ChangeResource_Test()
        {
            var resource = new SDataResource {
                { "FirstName", "Joe" }, { "LastName", "Bloggs" }
            };
            var collection = new SDataCollection <SDataResource> {
                resource
            };

            collection.AcceptChanges();
            Assert.That(collection.IsChanged, Is.False);
            Assert.That(collection.GetChanges(), Is.Null);
            resource["FirstName"] = "Jill";
            Assert.That(collection.IsChanged, Is.True);
            var changes = (SDataCollection <object>)collection.GetChanges();

            Assert.That(changes.DeleteMissing, Is.False);
            Assert.That(changes, Is.EqualTo(new List <object> {
                new Dictionary <string, object> {
                    { "FirstName", "Jill" }
                }
            }));
            resource["Age"] = 33;
            Assert.That(collection.IsChanged, Is.True);
            Assert.That(collection.GetChanges(), Is.EqualTo(new List <object> {
                new Dictionary <string, object> {
                    { "FirstName", "Jill" }, { "Age", 33 }
                }
            }));
        }
        public void ChangeTracking_Retain_Protocol_Info_Test()
        {
            var nested = new SDataResource {
                { "Country", "Australia" }, { "City", "Melbourne" }
            };

            nested.Key  = "address1";
            nested.ETag = "aaa1";
            var resource = new SDataResource {
                { "FirstName", "Joe" }, { "Address", nested }
            };

            resource.Key          = "contact1";
            resource.ETag         = "ccc1";
            resource.XmlNamespace = "ns";
            resource.XmlLocalName = "contact";
            resource.AcceptChanges();
            resource["FirstName"] = "Joanne";
            nested["City"]        = "Sydney";
            var changes = (SDataResource)resource.GetChanges();

            Assert.That(changes, Is.Not.EqualTo(resource));
            Assert.That(changes.Key, Is.EqualTo("contact1"));
            Assert.That(changes.ETag, Is.EqualTo("ccc1"));
            Assert.That(changes.XmlNamespace, Is.EqualTo("ns"));
            Assert.That(changes.XmlLocalName, Is.EqualTo("contact"));
            changes = (SDataResource)changes["Address"];
            Assert.That(changes, Is.Not.EqualTo(nested));
            Assert.That(changes.Key, Is.EqualTo("address1"));
            Assert.That(changes.ETag, Is.EqualTo("aaa1"));
        }
Example #3
0
        public void Write_Nested_Poco_Array_Test()
        {
            var resource = new SDataResource("SalesOrder")
            {
                {
                    "OrderLines", new[]
                    {
                        new SalesOrderLine {
                            UnitPrice = 123
                        },
                        new SalesOrderLine {
                            UnitPrice = 456
                        }
                    }
                }
            };
            var nav = Helpers.WriteAtom(resource);
            var mgr = new XmlNamespaceManager(nav.NameTable);

            mgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
            mgr.AddNamespace("sdata", "http://schemas.sage.com/sdata/2008/1");
            var nodes = nav.Select("atom:entry/sdata:payload/SalesOrder/OrderLines/SalesOrderLine", mgr);

            Assert.That(nodes, Is.Not.Null);
            Assert.That(nodes.Count, Is.EqualTo(2));
        }
Example #4
0
        public void Deserialize_Object_With_ReadOnly_Property_Test()
        {
            var value  = new SDataResource();
            var result = ContentHelper.Deserialize <Deserialize_Object_With_ReadOnly_Property_Object>(value);

            Assert.That(result, Is.Not.Null);
        }
Example #5
0
        public void SetProtocolValue_Untyped_Test()
        {
            var resource = new SDataResource();

            ContentHelper.SetProtocolValue(resource, SDataProtocolProperty.ETag, "abc123");
            Assert.That(resource.ETag, Is.EqualTo("abc123"));
        }
 public void Dynamic_Set_Property_Test()
 {
     var resource = new SDataResource();
     dynamic obj = resource;
     obj.Name = "Joe";
     Assert.That(resource["Name"], Is.EqualTo("Joe"));
 }
 private void btnSingleDelete_Click(object sender, EventArgs e)
 {
     Client.Delete(_resource, tbSingleResourceKind.Text);
     _resource = null;
     UpdateGrid();
     StatusLabel.Text = "Delete completed successfully.";
 }
Example #8
0
        public void ChangeTracking_RemoveResource_Test()
        {
            var resource = new SDataResource(new Dictionary <string, object> {
                { "FirstName", "Joe" }
            })
            {
                Key = "abc123"
            };
            var collection = new SDataCollection <SDataResource> {
                resource
            };

            collection.AcceptChanges();
            Assert.That(collection.IsChanged, Is.False);
            Assert.That(collection.GetChanges(), Is.Null);
            collection.RemoveAt(0);
            Assert.That(collection.IsChanged, Is.True);
            var changes = (SDataCollection <object>)collection.GetChanges();

            Assert.That(changes.DeleteMissing, Is.False);
            Assert.That(changes, Is.EqualTo(new List <object> {
                new Dictionary <string, object>()
            }));
            resource = ((SDataResource)changes[0]);
            Assert.That(resource.Key, Is.EqualTo("abc123"));
            Assert.That(resource.IsDeleted, Is.True);
        }
 private void btnSingleDelete_Click(object sender, EventArgs e)
 {
     Client.Delete(_resource, tbSingleResourceKind.Text);
     _resource = null;
     UpdateGrid();
     StatusLabel.Text = "Delete completed successfully.";
 }
        public void Dynamic_Set_Indexer_Test()
        {
            var     resource = new SDataResource();
            dynamic obj      = resource;

            obj["Name"] = "Joe";
            Assert.That(resource["Name"], Is.EqualTo("Joe"));
        }
 public void Set_Primitive_Test()
 {
     var resource = new SDataResource {{"LastName", null}};
     var prop = TypeDescriptor.GetProperties(resource)["LastName"];
     Assert.That(prop, Is.Not.Null);
     prop.SetValue(resource, "Smith");
     Assert.That(resource["LastName"], Is.EqualTo("Smith"));
 }
Example #12
0
        public void Write_Missing_XmlLocalName_Test()
        {
            var resource = new SDataResource {
                { "LastName", "Smith" }
            };

            Assert.That(() => Helpers.WriteAtom(resource), Throws.TypeOf <SDataClientException>());
        }
 public void Get_NestedResource_Test()
 {
     var resource = new SDataResource {{"Address", new SDataResource()}};
     var prop = TypeDescriptor.GetProperties(resource)["Address"];
     Assert.That(prop, Is.Not.Null);
     Assert.That(prop.PropertyType, Is.EqualTo(typeof (SDataResource)));
     Assert.That(prop.Converter, Is.TypeOf<ExpandableObjectConverter>());
 }
 public void Get_Primitive_Test()
 {
     var resource = new SDataResource {{"LastName", "Smith"}};
     var prop = TypeDescriptor.GetProperties(resource)["LastName"];
     Assert.That(prop, Is.Not.Null);
     Assert.That(prop.PropertyType, Is.EqualTo(typeof (string)));
     Assert.That(prop.GetValue(resource), Is.EqualTo("Smith"));
 }
 private void btnSingleUpdate_Click(object sender, EventArgs e)
 {
     var options = !string.IsNullOrEmpty(tbSingleResourceInclude.Text)
         ? new SDataPayloadOptions {Include = tbSingleResourceInclude.Text}
         : null;
     _resource = Client.Put(_resource, tbSingleResourceKind.Text, options);
     UpdateGrid();
     StatusLabel.Text = "Update completed successfully.";
 }
        public void Dynamic_Get_Indexer_Test()
        {
            var resource = new SDataResource {
                { "Name", "Joe" }
            };
            dynamic obj = resource;

            Assert.That(obj["Name"], Is.EqualTo("Joe"));
        }
        public void Write_Resource_Id_Test()
        {
            var resource = new SDataResource {
                Id = "id"
            };
            var obj = Helpers.WriteJson(resource);

            Assert.That(obj["$id"], Is.EqualTo("id"));
        }
        public void Dynamic_Get_Property_Test()
        {
            var resource = new SDataResource {
                { "Name", "Joe" }
            };
            dynamic obj = resource;

            Assert.That(obj.Name, Is.EqualTo("Joe"));
        }
 public void Get_NestedResourceCollection_Test()
 {
     var resource = new SDataResource {{"OrderLines", new SDataCollection<SDataResource>()}};
     var prop = TypeDescriptor.GetProperties(resource)["OrderLines"];
     Assert.That(prop, Is.Not.Null);
     Assert.That(prop.PropertyType, Is.EqualTo(typeof (SDataCollection<SDataResource>)));
     Assert.That(prop.Converter, Is.Not.Null);
     Assert.That(prop.Converter.GetPropertiesSupported(), Is.True);
 }
Example #20
0
        public void GetProtocolValue_Untyped_Test()
        {
            var resource = new SDataResource {
                ETag = "abc123"
            };
            var eTag = ContentHelper.GetProtocolValue <string>(resource, SDataProtocolProperty.ETag);

            Assert.That(eTag, Is.EqualTo("abc123"));
        }
        public void Write_TimeSpan_Test()
        {
            var resource = new SDataResource {
                { "time", TimeSpan.FromTicks(100000000000) }
            };
            var obj = Helpers.WriteJson(resource);

            Assert.That(obj["time"], Is.EqualTo("02:46:40"));
        }
Example #22
0
        public void Deserialize_Empty_Collection_Inference_Workaround_Test()
        {
            var value = new SDataResource {
                { "Items", new SDataResource() }
            };
            var result = ContentHelper.Deserialize <Deserialize_Empty_Collection_Inference_Workaround_Object>(value);

            Assert.That(result.Items, Is.Not.Null);
        }
        public void Write_Empty_String_Property_Test()
        {
            var resource = new SDataResource {
                { "LastName", "" }
            };
            var obj = Helpers.WriteJson(resource);

            Assert.That(obj["LastName"], Is.Empty);
        }
        public void Write_HttpMethod_Test()
        {
            var resource = new SDataResource {
                HttpMethod = HttpMethod.Post
            };
            var obj = Helpers.WriteJson(resource);

            Assert.That(obj["$httpMethod"], Is.EqualTo("POST"));
        }
        public void Write_DateTimeOffset_Test()
        {
            var resource = new SDataResource {
                Updated = new DateTimeOffset(2013, 6, 15, 8, 0, 0, TimeSpan.FromHours(10))
            };
            var obj     = Helpers.WriteJson(resource);
            var updated = obj["$updated"];

            Assert.That(updated, Is.EqualTo("/Date(1371247200000+1000)/"));
        }
        public void Get_Primitive_Test()
        {
            var resource = new SDataResource {
                { "LastName", "Smith" }
            };
            var prop = TypeDescriptor.GetProperties(resource)["LastName"];

            Assert.That(prop, Is.Not.Null);
            Assert.That(prop.PropertyType, Is.EqualTo(typeof(string)));
            Assert.That(prop.GetValue(resource), Is.EqualTo("Smith"));
        }
        public void Set_Primitive_Test()
        {
            var resource = new SDataResource {
                { "LastName", null }
            };
            var prop = TypeDescriptor.GetProperties(resource)["LastName"];

            Assert.That(prop, Is.Not.Null);
            prop.SetValue(resource, "Smith");
            Assert.That(resource["LastName"], Is.EqualTo("Smith"));
        }
        public void Get_NestedResource_Test()
        {
            var resource = new SDataResource {
                { "Address", new SDataResource() }
            };
            var prop = TypeDescriptor.GetProperties(resource)["Address"];

            Assert.That(prop, Is.Not.Null);
            Assert.That(prop.PropertyType, Is.EqualTo(typeof(SDataResource)));
            Assert.That(prop.Converter, Is.TypeOf <ExpandableObjectConverter>());
        }
Example #29
0
        public void Primitive_Values_Formatted_Appropriately()
        {
            var payload = new SDataResource("salesOrder")
            {
                { "byte", byte.MaxValue },
                { "sbyte", sbyte.MaxValue },
                { "short", short.MaxValue },
                { "ushort", ushort.MaxValue },
                { "int", int.MaxValue },
                { "uint", uint.MaxValue },
                { "long", long.MaxValue },
                { "ulong", ulong.MaxValue },
                { "bool", true },
                { "char", 'z' },
                { "float", float.MaxValue },
                { "double", double.MaxValue },
                { "decimal", decimal.MaxValue },
                { "Guid", Guid.NewGuid() },
                { "DateTime", DateTime.Now },
                { "DateTimeOffset", DateTimeOffset.Now },
                { "TimeSpan", DateTime.Now.TimeOfDay }
            };
            var nav = Helpers.WriteAtom(payload);

            nav = nav.SelectSingleNode("*//salesOrder");

            var assertDoesNotThrow = new Action <string, Action <string> >(
                (name, action) =>
            {
                var node = nav.SelectSingleNode(name);
                Assert.That(node, Is.Not.Null);
                Assert.DoesNotThrow(() => action(node.Value));
            });

            assertDoesNotThrow("byte", x => XmlConvert.ToByte(x));
            assertDoesNotThrow("sbyte", x => XmlConvert.ToSByte(x));
            assertDoesNotThrow("short", x => XmlConvert.ToInt16(x));
            assertDoesNotThrow("ushort", x => XmlConvert.ToUInt16(x));
            assertDoesNotThrow("int", x => XmlConvert.ToInt32(x));
            assertDoesNotThrow("uint", x => XmlConvert.ToUInt32(x));
            assertDoesNotThrow("long", x => XmlConvert.ToInt64(x));
            assertDoesNotThrow("ulong", x => XmlConvert.ToUInt64(x));
            assertDoesNotThrow("bool", x => XmlConvert.ToBoolean(x));
            assertDoesNotThrow("char", x => XmlConvert.ToChar(x));
            assertDoesNotThrow("float", x => XmlConvert.ToSingle(x));
            assertDoesNotThrow("double", x => XmlConvert.ToDouble(x));
            assertDoesNotThrow("decimal", x => XmlConvert.ToDecimal(x));
            assertDoesNotThrow("Guid", x => XmlConvert.ToGuid(x));
#if !PCL && !NETFX_CORE
            assertDoesNotThrow("DateTime", x => XmlConvert.ToDateTime(x, XmlDateTimeSerializationMode.RoundtripKind));
#endif
            assertDoesNotThrow("DateTimeOffset", x => XmlConvert.ToDateTimeOffset(x));
            assertDoesNotThrow("TimeSpan", x => XmlConvert.ToTimeSpan(x));
        }
        public void Uri_Property_Should_Be_Escaped_When_Written()
        {
            var payload = new SDataResource
            {
                Url = new Uri("http://localhost/sdata/invoices('`%^ {}<>')")
            };
            var nav = Helpers.WriteJson(payload);
            var url = nav["$url"];

            Assert.That(url, Is.EqualTo("http://localhost/sdata/invoices('%60%25%5E%20%7B%7D%3C%3E')"));
        }
Example #31
0
        public static IDictionary <string, object> AsDictionary(object obj)
        {
            if (obj != null)
            {
                var generic = obj as IDictionary <string, object>;
                if (generic != null)
                {
                    return(generic);
                }

                var nonGeneric = obj as IDictionary;
                if (nonGeneric != null)
                {
                    generic = nonGeneric.Cast <DictionaryEntry>()
                              .ToDictionary(entry => entry.Key.ToString(), entry => entry.Value);
                }
                else
                {
                    var entries = obj as IEnumerable;
                    if (entries != null)
                    {
                        var iface = obj.GetType()
                                    .GetInterfaces()
                                    .FirstOrDefault(item => item.GetTypeInfo().IsGenericType&& item.GetGenericTypeDefinition() == typeof(IDictionary <,>));
                        if (iface != null)
                        {
                            var keyValueType = iface.GetInterfaces()
                                               .First(a => a.GetTypeInfo().IsGenericType&& a.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                                               .GetGenericArguments()[0];
                            var keyProp   = keyValueType.GetProperty("Key");
                            var valueProp = keyValueType.GetProperty("Value");
                            generic = entries.Cast <object>()
                                      .ToDictionary(item => keyProp.GetValue(item, null).ToString(),
                                                    item => valueProp.GetValue(item, null));
                        }
                    }
                }

                if (generic != null)
                {
                    var prot = obj as ISDataProtocolObject;
                    if (prot != null)
                    {
                        generic = new SDataResource(generic);
                        ((ISDataProtocolObject)generic).Info = prot.Info;
                    }

                    return(generic);
                }
            }

            return(null);
        }
        public void Get_NestedResourceCollection_Test()
        {
            var resource = new SDataResource {
                { "OrderLines", new SDataCollection <SDataResource>() }
            };
            var prop = TypeDescriptor.GetProperties(resource)["OrderLines"];

            Assert.That(prop, Is.Not.Null);
            Assert.That(prop.PropertyType, Is.EqualTo(typeof(SDataCollection <SDataResource>)));
            Assert.That(prop.Converter, Is.Not.Null);
            Assert.That(prop.Converter.GetPropertiesSupported(), Is.True);
        }
Example #33
0
        public void Deserialize_SDataProtocolObject_Test()
        {
            var value = new SDataResource {
                XmlLocalName = "contact"
            };
            var result = ContentHelper.Deserialize <SDataProtocolObject_Object>(value);

            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.Not.SameAs(value));
            Assert.That(result, Is.InstanceOf <ISDataProtocolObject>());
            Assert.That(result.Info.XmlLocalName, Is.EqualTo("contact"));
        }
        private void btnSingleUpdate_Click(object sender, EventArgs e)
        {
            var options = !string.IsNullOrEmpty(tbSingleResourceInclude.Text)
                ? new SDataPayloadOptions {
                Include = tbSingleResourceInclude.Text
            }
                : null;

            _resource = Client.Put(_resource, tbSingleResourceKind.Text, options);
            UpdateGrid();
            StatusLabel.Text = "Update completed successfully.";
        }
        public void Write_PostMode_Test()
        {
            var resource = new SDataResource {
                { "LastName", "Smith" }
            };

            resource.Key        = "key";
            resource.HttpMethod = HttpMethod.Post;
            var obj = Helpers.WriteJson(resource);

            Assert.That(obj.ContainsKey("LastName"), Is.False);
        }
 public void ChangeTracking_AddResource_Test()
 {
     var resource = new SDataResource {{"FirstName", "Joe"}, {"LastName", "Bloggs"}};
     var collection = new SDataCollection<SDataResource> {resource};
     collection.AcceptChanges();
     Assert.That(collection.IsChanged, Is.False);
     Assert.That(collection.GetChanges(), Is.Null);
     collection.Add(new SDataResource {{"FirstName", "Jim"}});
     Assert.That(collection.IsChanged, Is.True);
     var changes = (SDataCollection<object>) collection.GetChanges();
     Assert.That(changes.DeleteMissing, Is.False);
     Assert.That(changes, Is.EqualTo(new List<object> {new Dictionary<string, object> {{"FirstName", "Jim"}}}));
 }
Example #37
0
        public void Deserialize_Simple_Types_From_Null_Test()
        {
            var value = new SDataResource
            {
                { "SByteProp", null },
                { "ShortProp", null },
                { "IntProp", null },
                { "LongProp", null },
                { "ByteProp", null },
                { "UShortProp", null },
                { "UIntProp", null },
                { "ULongProp", null },
                { "FloatProp", null },
                { "DoubleProp", null },
                { "BoolProp", null },
                { "CharProp", null },
                { "DecimalProp", null },
                { "DateTimeProp", null },
                { "EnumProp", null },
                { "StringProp", null },
                { "DateTimeOffsetProp", null },
                { "GuidProp", null },
                { "TimeSpanProp", null },
                { "VersionProp", null },
                { "UriProp", null },
                { "ByteArrayProp", null }
            };
            var result = ContentHelper.Deserialize <Simple_Types_Object>(value);

            Assert.That(result.SByteProp, Is.EqualTo(0));
            Assert.That(result.ShortProp, Is.EqualTo(0));
            Assert.That(result.IntProp, Is.EqualTo(0));
            Assert.That(result.LongProp, Is.EqualTo(0));
            Assert.That(result.ByteProp, Is.EqualTo(0));
            Assert.That(result.UShortProp, Is.EqualTo(0));
            Assert.That(result.UIntProp, Is.EqualTo(0));
            Assert.That(result.ULongProp, Is.EqualTo(0));
            Assert.That(result.FloatProp, Is.EqualTo(0));
            Assert.That(result.DoubleProp, Is.EqualTo(0));
            Assert.That(result.BoolProp, Is.False);
            Assert.That(result.CharProp, Is.EqualTo('\0'));
            Assert.That(result.DecimalProp, Is.EqualTo(0));
            Assert.That(result.DateTimeProp, Is.EqualTo(DateTime.MinValue));
            Assert.That(result.EnumProp, Is.EqualTo(DayOfWeek.Sunday));
            Assert.That(result.StringProp, Is.EqualTo(null));
            Assert.That(result.DateTimeOffsetProp, Is.EqualTo(DateTimeOffset.MinValue));
            Assert.That(result.GuidProp, Is.EqualTo(Guid.Empty));
            Assert.That(result.TimeSpanProp, Is.EqualTo(TimeSpan.Zero));
            Assert.That(result.VersionProp, Is.EqualTo(null));
            Assert.That(result.UriProp, Is.EqualTo(null));
        }
Example #38
0
        public void Deserialize_Simple_Types_From_String_Test()
        {
            var value = new SDataResource
            {
                { "SByteProp", "1" },
                { "ShortProp", "1" },
                { "IntProp", "1" },
                { "LongProp", "1" },
                { "ByteProp", "1" },
                { "UShortProp", "1" },
                { "UIntProp", "1" },
                { "ULongProp", "1" },
                { "FloatProp", "1.1" },
                { "DoubleProp", "1.1" },
                { "BoolProp", "true" },
                { "CharProp", "1" },
                { "DecimalProp", "1.1" },
                { "DateTimeProp", "2001-01-01T01:01:01Z" },
                { "EnumProp", "1" },
                { "StringProp", "1" },
                { "DateTimeOffsetProp", "2001-01-01T01:01:01Z" },
                { "GuidProp", "11111111-1111-1111-1111-111111111111" },
                { "TimeSpanProp", "01:01:01" },
                { "VersionProp", "1.1" },
                { "UriProp", "http://1.com" },
                { "ByteArrayProp", "1" }
            };
            var result = ContentHelper.Deserialize <Simple_Types_Object>(value);

            Assert.That(result.SByteProp, Is.EqualTo(1));
            Assert.That(result.ShortProp, Is.EqualTo(1));
            Assert.That(result.IntProp, Is.EqualTo(1));
            Assert.That(result.LongProp, Is.EqualTo(1));
            Assert.That(result.ByteProp, Is.EqualTo(1));
            Assert.That(result.UShortProp, Is.EqualTo(1));
            Assert.That(result.UIntProp, Is.EqualTo(1));
            Assert.That(result.ULongProp, Is.EqualTo(1));
            Assert.That(result.FloatProp, Is.EqualTo(1.1F));
            Assert.That(result.DoubleProp, Is.EqualTo(1.1));
            Assert.That(result.BoolProp, Is.True);
            Assert.That(result.CharProp, Is.EqualTo('1'));
            Assert.That(result.DecimalProp, Is.EqualTo(1.1));
            Assert.That(result.DateTimeProp, Is.EqualTo(new DateTime(2001, 1, 1, 1, 1, 1)));
            Assert.That(result.EnumProp, Is.EqualTo(DayOfWeek.Monday));
            Assert.That(result.StringProp, Is.EqualTo("1"));
            Assert.That(result.DateTimeOffsetProp, Is.EqualTo(new DateTimeOffset(2001, 1, 1, 1, 1, 1, TimeSpan.Zero)));
            Assert.That(result.GuidProp, Is.EqualTo(new Guid("11111111-1111-1111-1111-111111111111")));
            Assert.That(result.TimeSpanProp, Is.EqualTo(new TimeSpan(1, 1, 1)));
            Assert.That(result.VersionProp, Is.EqualTo(new Version(1, 1)));
            Assert.That(result.UriProp, Is.EqualTo(new Uri("http://1.com")));
        }
 public void ChangeTracking_RejectChanges_Test()
 {
     var resource = new SDataResource {{"FirstName", "Joe"}, {"LastName", "Bloggs"}};
     resource.AcceptChanges();
     Assert.That(resource.IsChanged, Is.False);
     Assert.That(resource.GetChanges(), Is.Null);
     resource.Remove("LastName");
     resource["FirstName"] = "Jill";
     resource.Add("Age", 33);
     Assert.That(resource.IsChanged, Is.True);
     Assert.That(resource.GetChanges(), Is.Not.Null);
     resource.RejectChanges();
     Assert.That(resource.IsChanged, Is.False);
     Assert.That(resource.GetChanges(), Is.Null);
 }
 public void ChangeTracking_RemoveResource_Test()
 {
     var resource = new SDataResource(new Dictionary<string, object> {{"FirstName", "Joe"}}) {Key = "abc123"};
     var collection = new SDataCollection<SDataResource> {resource};
     collection.AcceptChanges();
     Assert.That(collection.IsChanged, Is.False);
     Assert.That(collection.GetChanges(), Is.Null);
     collection.RemoveAt(0);
     Assert.That(collection.IsChanged, Is.True);
     var changes = (SDataCollection<object>) collection.GetChanges();
     Assert.That(changes.DeleteMissing, Is.False);
     Assert.That(changes, Is.EqualTo(new List<object> {new Dictionary<string, object>()}));
     resource = ((SDataResource) changes[0]);
     Assert.That(resource.Key, Is.EqualTo("abc123"));
     Assert.That(resource.IsDeleted, Is.True);
 }
 public void Collection_Items_Can_Be_In_Different_Namespace()
 {
     var payload = new SDataResource("tradingAccount", "http://gcrm.com")
                       {
                           {
                               "emails", new SDataCollection<SDataResource>("email")
                                             {
                                                 new SDataResource
                                                     {
                                                         XmlNamespace = "http://common.com"
                                                     }
                                             }
                           }
                       };
     var nav = Helpers.WriteAtom(payload);
     var mgr = new XmlNamespaceManager(nav.NameTable);
     mgr.AddNamespace("g", "http://gcrm.com");
     mgr.AddNamespace("c", "http://common.com");
     var node = nav.SelectSingleNode("*//g:tradingAccount/g:emails/c:email", mgr);
     Assert.That(node, Is.Not.Null);
 }
 public void ChangeTracking_Nested_Test()
 {
     var nested = new SDataResource {{"Country", "Australia"}, {"City", "Melbourne"}};
     var resource = new SDataResource {{"Address", nested}};
     resource.AcceptChanges();
     Assert.That(nested.IsChanged, Is.False);
     Assert.That(nested.GetChanges(), Is.Null);
     Assert.That(resource.IsChanged, Is.False);
     Assert.That(resource.GetChanges(), Is.Null);
     nested["City"] = "Sydney";
     Assert.That(nested.IsChanged, Is.True);
     Assert.That(nested.GetChanges(), Is.EqualTo(new Dictionary<string, object> {{"City", "Sydney"}}));
     Assert.That(resource.IsChanged, Is.True);
     var changes = (IDictionary<string, object>) resource.GetChanges();
     Assert.That(changes.Count, Is.EqualTo(1));
     Assert.That(changes["Address"], Is.EqualTo(new Dictionary<string, object> {{"City", "Sydney"}}));
     nested.Add("State", "VIC");
     Assert.That(nested.IsChanged, Is.True);
     Assert.That(nested.GetChanges(), Is.EqualTo(new Dictionary<string, object> {{"City", "Sydney"}, {"State", "VIC"}}));
     Assert.That(resource.IsChanged, Is.True);
     changes = (IDictionary<string, object>) resource.GetChanges();
     Assert.That(changes.Count, Is.EqualTo(1));
     Assert.That(changes["Address"], Is.EqualTo(new Dictionary<string, object> {{"City", "Sydney"}, {"State", "VIC"}}));
 }
 public void Write_Resource_Id_Test()
 {
     var resource = new SDataResource {Id = "id"};
     var obj = Helpers.WriteJson(resource);
     Assert.That(obj["$id"], Is.EqualTo("id"));
 }
 public void Dynamic_Get_Property_Test()
 {
     var resource = new SDataResource {{"Name", "Joe"}};
     dynamic obj = resource;
     Assert.That(obj.Name, Is.EqualTo("Joe"));
 }
 public void Write_Empty_String_Property_Test()
 {
     var resource = new SDataResource {{"LastName", ""}};
     var obj = Helpers.WriteJson(resource);
     Assert.That(obj["LastName"], Is.Empty);
 }
 public void ChangeTracking_Test()
 {
     var resource = new SDataResource {{"FirstName", "Joe"}, {"LastName", "Bloggs"}};
     resource.AcceptChanges();
     Assert.That(resource.IsChanged, Is.False);
     Assert.That(resource.GetChanges(), Is.Null);
     resource["FirstName"] = "Jill";
     Assert.That(resource.IsChanged, Is.True);
     Assert.That(resource.GetChanges(), Is.EqualTo(new Dictionary<string, object> {{"FirstName", "Jill"}}));
     resource.Add("Age", 33);
     Assert.That(resource.IsChanged, Is.True);
     Assert.That(resource.GetChanges(), Is.EqualTo(new Dictionary<string, object> {{"FirstName", "Jill"}, {"Age", 33}}));
 }
 public void Dynamic_Get_Indexer_Test()
 {
     var resource = new SDataResource {{"Name", "Joe"}};
     dynamic obj = resource;
     Assert.That(obj["Name"], Is.EqualTo("Joe"));
 }
 public void Write_Nested_Poco_Array_Test()
 {
     var resource = new SDataResource("SalesOrder")
                        {
                            {
                                "OrderLines", new[]
                                                  {
                                                      new SalesOrderLine {UnitPrice = 123},
                                                      new SalesOrderLine {UnitPrice = 456}
                                                  }
                            }
                        };
     var obj = Helpers.WriteJson(resource);
     Assert.That(obj["OrderLines"], Is.InstanceOf<ICollection<object>>());
     Assert.That(obj["OrderLines"], Has.Count.EqualTo(2));
 }
        public void Primitive_Values_Formatted_Appropriately()
        {
            var payload = new SDataResource("salesOrder")
                              {
                                  {"byte", byte.MaxValue},
                                  {"sbyte", sbyte.MaxValue},
                                  {"short", short.MaxValue},
                                  {"ushort", ushort.MaxValue},
                                  {"int", int.MaxValue},
                                  {"uint", uint.MaxValue},
                                  {"long", long.MaxValue},
                                  {"ulong", ulong.MaxValue},
                                  {"bool", true},
                                  {"char", 'z'},
                                  {"float", float.MaxValue},
                                  {"double", double.MaxValue},
                                  {"decimal", decimal.MaxValue},
                                  {"Guid", Guid.NewGuid()},
                                  {"DateTime", DateTime.Now},
                                  {"DateTimeOffset", DateTimeOffset.Now},
                                  {"TimeSpan", DateTime.Now.TimeOfDay}
                              };
            var nav = Helpers.WriteAtom(payload);
            nav = nav.SelectSingleNode("*//salesOrder");

            var assertDoesNotThrow = new Action<string, Action<string>>(
                (name, action) =>
                    {
                        var node = nav.SelectSingleNode(name);
                        Assert.That(node, Is.Not.Null);
                        Assert.DoesNotThrow(() => action(node.Value));
                    });
            assertDoesNotThrow("byte", x => XmlConvert.ToByte(x));
            assertDoesNotThrow("sbyte", x => XmlConvert.ToSByte(x));
            assertDoesNotThrow("short", x => XmlConvert.ToInt16(x));
            assertDoesNotThrow("ushort", x => XmlConvert.ToUInt16(x));
            assertDoesNotThrow("int", x => XmlConvert.ToInt32(x));
            assertDoesNotThrow("uint", x => XmlConvert.ToUInt32(x));
            assertDoesNotThrow("long", x => XmlConvert.ToInt64(x));
            assertDoesNotThrow("ulong", x => XmlConvert.ToUInt64(x));
            assertDoesNotThrow("bool", x => XmlConvert.ToBoolean(x));
            assertDoesNotThrow("char", x => XmlConvert.ToChar(x));
            assertDoesNotThrow("float", x => XmlConvert.ToSingle(x));
            assertDoesNotThrow("double", x => XmlConvert.ToDouble(x));
            assertDoesNotThrow("decimal", x => XmlConvert.ToDecimal(x));
            assertDoesNotThrow("Guid", x => XmlConvert.ToGuid(x));
            #if !PCL && !NETFX_CORE
            assertDoesNotThrow("DateTime", x => XmlConvert.ToDateTime(x, XmlDateTimeSerializationMode.RoundtripKind));
            #endif
            assertDoesNotThrow("DateTimeOffset", x => XmlConvert.ToDateTimeOffset(x));
            assertDoesNotThrow("TimeSpan", x => XmlConvert.ToTimeSpan(x));
        }
 public void Write_Resource_Diagnoses_Test()
 {
     var resource = new SDataResource
                         {
                             Diagnoses =
                                 {
                                     new Diagnosis {SDataCode = DiagnosisCode.BadUrlSyntax}
                                 }
                         };
     var obj = Helpers.WriteJson(resource);
     Assert.That(obj["$diagnoses"], Is.InstanceOf<IList<object>>());
     var diagnoses = (IList<object>) obj["$diagnoses"];
     Assert.That(diagnoses, Has.Count.EqualTo(1));
     Assert.That(diagnoses[0], Is.InstanceOf<IDictionary<string, object>>());
     var diagnosis = (IDictionary<string, object>) diagnoses[0];
     Assert.That(diagnosis["sdataCode"], Is.EqualTo("BadUrlSyntax"));
 }
 public void Write_Nested_Dictionary_Test()
 {
     var resource = new SDataResource
                        {
                            {"LastName", "Smith"},
                            {"Address", new Dictionary<string, object> {{"City", "Melbourne"}}}
                        };
     var obj = Helpers.WriteJson(resource);
     Assert.That(obj["LastName"], Is.EqualTo("Smith"));
     Assert.That(obj["Address"], Is.InstanceOf<IDictionary<string, object>>());
     var address = (IDictionary<string, object>) obj["Address"];
     Assert.That(address["City"], Is.EqualTo("Melbourne"));
 }
 public void Uri_Property_Should_Be_Escaped_When_Written()
 {
     var payload = new SDataResource
                       {
                           Url = new Uri("http://localhost/sdata/invoices('`%^ {}<>')")
                       };
     var nav = Helpers.WriteJson(payload);
     var url = nav["$url"];
     Assert.That(url, Is.EqualTo("http://localhost/sdata/invoices('%60%25%5E%20%7B%7D%3C%3E')"));
 }
 public void Write_Links_Test()
 {
     var resource = new SDataResource
         {
             Links = new List<SDataLink>
                 {
                     new SDataLink {Uri = new Uri("http://dummy/$schema"), Relation = "schema"},
                     new SDataLink {Uri = new Uri("http://dummy/$template"), Relation = "template"},
                     new SDataLink {Uri = new Uri("http://dummy/$service"), Relation = "service"}
                 }
         };
     var obj = Helpers.WriteJson(resource);
     Assert.That(obj["$schema"], Is.EqualTo("http://dummy/$schema"));
     Assert.That(obj["$template"], Is.EqualTo("http://dummy/$template"));
     Assert.That(obj["$service"], Is.EqualTo("http://dummy/$service"));
 }
        public void Primitive_Values_Formatted_Appropriately()
        {
            var payload = new SDataResource
                              {
                                  {"byte", byte.MaxValue},
                                  {"sbyte", sbyte.MaxValue},
                                  {"short", short.MaxValue},
                                  {"ushort", ushort.MaxValue},
                                  {"int", int.MaxValue},
                                  {"uint", uint.MaxValue},
                                  {"long", long.MaxValue},
                                  {"ulong", ulong.MaxValue},
                                  {"bool", true},
                                  {"char", 'z'},
                                  {"float", float.MaxValue},
                                  {"double", double.MaxValue},
                                  {"decimal", decimal.MaxValue},
                                  {"Guid", Guid.NewGuid()},
                                  {"DateTime", DateTime.Now},
                                  {"DateTimeOffset", DateTimeOffset.Now}
                              };
            var nav = Helpers.WriteJson(payload);

            var assertDoesNotThrow = new Action<string, Func<object, object>>(
                (name, action) =>
                    {
                        var value = nav[name];
                        Assert.That(value, Is.Not.Null);
                        Assert.DoesNotThrow(() => action(value));
                    });
            assertDoesNotThrow("byte", x => Convert.ToByte(x));
            assertDoesNotThrow("sbyte", x => Convert.ToSByte(x));
            assertDoesNotThrow("short", x => Convert.ToInt16(x));
            assertDoesNotThrow("ushort", x => Convert.ToUInt16(x));
            assertDoesNotThrow("int", x => Convert.ToInt32(x));
            assertDoesNotThrow("uint", x => Convert.ToUInt32(x));
            assertDoesNotThrow("long", x => Convert.ToInt64(x));
            assertDoesNotThrow("ulong", x => Convert.ToUInt64(x));
            assertDoesNotThrow("bool", x => Convert.ToBoolean(x));
            assertDoesNotThrow("char", x => Convert.ToChar(x));
            assertDoesNotThrow("float", x => Convert.ToSingle(x));
            assertDoesNotThrow("double", x => Convert.ToDouble(x));
            assertDoesNotThrow("decimal", x => Convert.ToDecimal(x));
            assertDoesNotThrow("Guid", x => new Guid(x.ToString()));
            assertDoesNotThrow("DateTime", x => (string) x);
            assertDoesNotThrow("DateTimeOffset", x => (string) x);
        }
 public void ChangeTracking_Retain_Protocol_Info_Test()
 {
     var nested = new SDataResource {{"Country", "Australia"}, {"City", "Melbourne"}};
     nested.Key = "address1";
     nested.ETag = "aaa1";
     var resource = new SDataResource {{"FirstName", "Joe"}, {"Address", nested}};
     resource.Key = "contact1";
     resource.ETag = "ccc1";
     resource.XmlNamespace = "ns";
     resource.XmlLocalName = "contact";
     resource.AcceptChanges();
     resource["FirstName"] = "Joanne";
     nested["City"] = "Sydney";
     var changes = (SDataResource) resource.GetChanges();
     Assert.That(changes, Is.Not.EqualTo(resource));
     Assert.That(changes.Key, Is.EqualTo("contact1"));
     Assert.That(changes.ETag, Is.EqualTo("ccc1"));
     Assert.That(changes.XmlNamespace, Is.EqualTo("ns"));
     Assert.That(changes.XmlLocalName, Is.EqualTo("contact"));
     changes = (SDataResource) changes["Address"];
     Assert.That(changes, Is.Not.EqualTo(nested));
     Assert.That(changes.Key, Is.EqualTo("address1"));
     Assert.That(changes.ETag, Is.EqualTo("aaa1"));
 }
 public void Write_Resource_SyncState_Test()
 {
     var resource = new SDataResource
                         {
                             SyncState = new SyncState
                                             {
                                                 Tick = 123
                                             }
                         };
     var obj = Helpers.WriteJson(resource);
     Assert.That(obj["$syncState"], Is.InstanceOf<IDictionary<string, object>>());
     var syncState = (IDictionary<string, object>) obj["$syncState"];
     Assert.That(syncState["tick"], Is.EqualTo(123));
 }
        public void Execute_Batch_Test()
        {
            var requestMock = new Mock<SDataRequest>(null, null, null);
            requestMock.Setup(x => x.GetResponse()).Returns(new SDataResponse(HttpStatusCode.OK, null, null, null, null, null, null, null, null));
            SDataUri requestUri = null;
            var requestFactory = new Func<string, SDataRequest>(uri =>
            {
                requestUri = new SDataUri(uri);
                return requestMock.Object;
            });
            var client = new SDataClient("test://dummy", requestFactory);
            var file1 = new AttachedFile(null, null, null);
            var params1 = new SDataParameters
                {
                    Include = "include1",
                    Select = "select1",
                    Precedence = 1,
                    Format = MediaType.ImagePng,
                    Language = "language",
                    Version = "version",
                    Path = "path",
                    ExtensionArgs = {{"foo1", "bar1"}},
                    Method = HttpMethod.Post,
                    Content = new object(),
                    ContentType = MediaType.ImageTiff,
                    ETag = "etag1",
                    Form = {{"hello1", "world1"}},
                    Files = {file1},
                    Accept = new[] {MediaType.ImageJpeg}
                };
            var file2 = new AttachedFile(null, null, null);
            var resource2 = new SDataResource {Key = "key2"};
            resource2["foo"] = "bar";
            var params2 = new SDataParameters
                {
                    Include = "include2",
                    Select = "select2",
                    Precedence = 2,
                    Format = MediaType.ImagePng,
                    Language = "language",
                    Version = "version",
                    Path = "path",
                    ExtensionArgs = {{"foo2", "bar2"}},
                    Method = HttpMethod.Put,
                    Content = resource2,
                    ContentType = MediaType.ImageTiff,
                    ETag = "etag2",
                    Form = {{"hello2", "world2"}},
                    Files = {file2},
                    Accept = new[] {MediaType.Css}
                };
            client.ExecuteBatch<SDataResource>(new[] {params1, params2});

            var resources = requestMock.Object.Content as IList<SDataResource>;
            Assert.That(resources, Is.Not.Null);
            Assert.That(requestUri, Is.Not.Null);
            Assert.That(requestUri.Include, Is.EqualTo("include1,include2"));
            Assert.That(requestUri.Select, Is.EqualTo("select1,select2"));
            Assert.That(requestUri.Precedence, Is.EqualTo(2));
            Assert.That(requestUri.Format, Is.EqualTo(MediaType.ImagePng));
            Assert.That(requestUri.Language, Is.EqualTo("language"));
            Assert.That(requestUri.Version, Is.EqualTo("version"));
            Assert.That(requestUri.Path, Is.EqualTo("path/$batch"));
            Assert.That(requestUri["_foo1"], Is.EqualTo("bar1"));
            Assert.That(requestUri["_foo2"], Is.EqualTo("bar2"));
            Assert.That(requestMock.Object.Method, Is.EqualTo(HttpMethod.Post));
            Assert.That(resources[0].HttpMethod, Is.EqualTo(HttpMethod.Post));
            Assert.That(resources[1].HttpMethod, Is.EqualTo(HttpMethod.Put));
            Assert.That(resources[0].Url, Is.Null);
            Assert.That(resources[1].Url, Is.EqualTo(new Uri("test://dummy/path('key2')?include=include2&select=select2&precedence=2")));
            Assert.That(requestMock.Object.ContentType, Is.EqualTo(MediaType.ImageTiff));
            Assert.That(resources[0].IfMatch, Is.EqualTo("etag1"));
            Assert.That(resources[1].IfMatch, Is.EqualTo("etag2"));
            Assert.That(requestMock.Object.Form["hello1"], Is.EqualTo("world1"));
            Assert.That(requestMock.Object.Form["hello2"], Is.EqualTo("world2"));
            Assert.That(requestMock.Object.Files[0], Is.EqualTo(file1));
            Assert.That(requestMock.Object.Files[1], Is.EqualTo(file2));
            Assert.That(requestMock.Object.Accept, Is.EqualTo(new[] {MediaType.ImageJpeg, MediaType.Css}));
            Assert.That(requestMock.Object.AcceptLanguage, Is.EqualTo("language"));
        }
 public void Written_Collection_Uses_Item_Resource_Name()
 {
     var payload = new SDataResource("salesOrder")
                       {
                           {
                               "orderLines", new SDataCollection<SDataResource>("salesOrderLine")
                                                 {
                                                     new SDataResource {Key = "43660-1"}
                                                 }
                           }
                       };
     var nav = Helpers.WriteAtom(payload);
     var node = nav.SelectSingleNode("*//salesOrder/orderLines/salesOrderLine");
     Assert.That(node, Is.Not.Null);
 }
 public void Write_TimeSpan_Test()
 {
     var resource = new SDataResource {{"time", TimeSpan.FromTicks(100000000000)}};
     var obj = Helpers.WriteJson(resource);
     Assert.That(obj["time"], Is.EqualTo("02:46:40"));
 }
 public void Uri_Property_Should_Be_Escaped_When_Written()
 {
     var payload = new SDataResource
                       {
                           XmlLocalName = "person",
                           XmlNamespace = "http://test.com",
                           Url = new Uri("http://localhost/sdata/invoices('`%^ {}<>')")
                       };
     var nav = Helpers.WriteAtom(payload);
     var mgr = new XmlNamespaceManager(nav.NameTable);
     mgr.AddNamespace("sdata", Common.SData.Namespace);
     mgr.AddNamespace("test", "http://test.com");
     var node = nav.SelectSingleNode("*//test:person/@sdata:uri", mgr);
     Assert.That(node, Is.Not.Null);
     Assert.That(node.Value, Is.EqualTo("http://localhost/sdata/invoices('%60%25%5E%20%7B%7D%3C%3E')"));
 }