Exemplo n.º 1
0
        private void TestExtremum(T value, int id, ExtremumType extremumType)
        {
            Collection <DataFieldInfo> dataFields = DataObjectInfo <T> .GetDataFields(DataFieldMask.Data);

            // Set the value
            foreach (DataFieldInfo field in dataFields)
            {
                Type fieldType = field.Field.FieldType;

                if (fieldType.IsValueType && fieldType.IsPrimitive)
                {
                    DataObjectInfo <T> .SetProperty(field, value, Extremum(fieldType, extremumType));
                }
            }

            // Save the object
            DataObjectFactory <T> .WriteObject(value);

            value = DataObjectFactory <T> .CreateObject(id);

            // Verify the value
            foreach (DataFieldInfo field in dataFields)
            {
                Type fieldType = field.Field.FieldType;

                if (fieldType.IsValueType && fieldType.IsPrimitive)
                {
                    object storedValue = field.Field.GetValue(value);
                    Assert.AreEqual(Extremum(fieldType, extremumType), storedValue, string.Format("#17 - {0}: Maximum value should store correctly.", FormatFieldName(field)));
                }
            }
        }
Exemplo n.º 2
0
        private void TestTableType()
        {
            // Create a new object
            T t = DataObjectFactory <T> .NewObject();

            Assert.IsNotNull(t, "#1 (Object should not be null)");
            Assert.IsTrue(t.IsNew, "#5 (IsNew should be set)");
            Assert.IsFalse(t.IsDeleted, "#6 (IsDeleted should be false)");
            Assert.IsTrue(t.IsDirty, "#7 (IsDirty should be true)");

            // Fill the data
            Collection <DataFieldInfo> dataFields = DataObjectInfo <T> .GetDataFields(DataFieldMask.Data);

            foreach (DataFieldInfo dataField in dataFields)
            {
                DataObjectInfo <T> .SetProperty(dataField, t, Random(dataField.Field.FieldType));
            }

            // Save the object to the database
            DataObjectFactory <T> .WriteObject(t);

            if (!DataObjectInfo <T> .HasPrimaryKeyWithAutoNumber())
            {
                throw new NotSupportedException();
            }

            int id = DataObjectInfo <T> .GetPrimaryKey(t);

            Assert.AreNotEqual(0, id, "#2 (ID should not be zero)");
            Assert.IsFalse(t.IsDirty, "#8 (IsDirty should be false)");
            Assert.IsFalse(t.IsDeleted, "#9 (IsDeleted should be false)");
            Assert.IsFalse(t.IsNew, "#16 (IsNew should be false)");

            // Load a new object
            T database = DataObjectFactory <T> .CreateObject(id);

            foreach (DataFieldInfo dataField in dataFields)
            {
                object localValue    = dataField.Field.GetValue(t);
                object databaseValue = dataField.Field.GetValue(database);

                // Text strings may be trimmed by the database. For now,
                // ignore that.
                if (dataField.Field.FieldType == typeof(string))
                {
                    string localText    = (string)localValue;
                    string databaseText = (string)databaseValue;

                    if (databaseText.Length < localText.Length)
                    {
                        localValue = localText.Substring(0, databaseText.Length);
                    }
                }

                if (dataField.Field.FieldType == typeof(double))
                {
                    Assert.AreEqual((double)localValue, (double)databaseValue, 0.01, string.Format("#3a - {0} (Value retrieved from database should equal stored value)", FormatFieldName(dataField)));
                }
                else if (dataField.Field.FieldType == typeof(float))
                {
                    Assert.AreEqual((float)localValue, (float)databaseValue, 0.01, string.Format("#3b - {0} (Value retrieved from database should equal stored value)", FormatFieldName(dataField)));
                }
                else
                {
                    Assert.AreEqual(localValue, databaseValue, string.Format("#3x - {0} (Value retrieved from database should equal stored value)", FormatFieldName(dataField)));
                }
            }

            // Modify the object
            foreach (DataFieldInfo dataField in dataFields)
            {
                DataObjectInfo <T> .SetProperty(dataField, t, Random(dataField.Field.FieldType));

                Assert.IsTrue(DataObjectInfo <T> .HasChanged(dataField, t), string.Format("#11 - {0} (Field should be marked as dirty)", FormatFieldName(dataField)));
            }

            Assert.IsTrue(t.IsDirty, "#12 (Object should be dirty)");

            DataObjectFactory <T> .WriteObject(t);

            Assert.AreEqual(id, DataObjectInfo <T> .GetPrimaryKey(t), "#13 (Id should not change after saving)");
            Assert.IsFalse(t.IsNew, "#14 (IsDirty should not be set");
            Assert.IsFalse(t.IsNew, "#15 (IsNew should not be set");

            database = DataObjectFactory <T> .CreateObject(id);

            foreach (DataFieldInfo dataField in dataFields)
            {
                object localValue    = dataField.Field.GetValue(t);
                object databaseValue = dataField.Field.GetValue(database);

                // Text strings may be trimmed by the database. For now,
                // ignore that.
                if (dataField.Field.FieldType == typeof(string))
                {
                    string localText    = (string)localValue;
                    string databaseText = (string)databaseValue;

                    if (databaseText.Length < localText.Length)
                    {
                        localValue = localText.Substring(0, databaseText.Length);
                    }
                }

                if (dataField.Field.FieldType == typeof(double))
                {
                    Assert.AreEqual((double)localValue, (double)databaseValue, 0.01, string.Format("#4a - {0}", FormatFieldName(dataField)));
                }
                else if (dataField.Field.FieldType == typeof(float))
                {
                    Assert.AreEqual((float)localValue, (float)databaseValue, 0.01, string.Format("#4b - {0}", FormatFieldName(dataField)));
                }
                else
                {
                    Assert.AreEqual(localValue, databaseValue, string.Format("#4x - {0}", FormatFieldName(dataField)));
                }
            }

            // For all fields of value types, try to store the maximum and minimum value, to make sure
            // the type in the database can hold the values given by the program.
            TestExtremum(t, id, ExtremumType.Maximum);
            TestExtremum(t, id, ExtremumType.Minimum);

            // Delete the object
            DataObjectFactory <T> .DeleteObject(t);
        }