Exemple #1
0
        public void Can_Map_Null_Values_To_Nullable_Enum_Properties()
        {
            // Arrange
            const int     id            = 1;
            const string  firstName     = "Jimbo";
            const string  lastName      = "Smith";
            const Gender  gender        = Gender.Male;
            MaritalStatus?maritalStatus = null;

            dynamic person = new ExpandoObject();

            person.Id            = id;
            person.FirstName     = firstName;
            person.LastName      = lastName;
            person.Gender        = gender;
            person.MaritalStatus = maritalStatus;

            // Act
            PersonWithProperties customer = Slapper.AutoMapper.MapDynamic <PersonWithProperties>(person);

            // Assert
            Assert.NotNull(customer);
            Assert.That(customer.Id == id);
            Assert.That(customer.FirstName == firstName);
            Assert.That(customer.LastName == lastName);
            Assert.That(customer.Gender == gender);
            Assert.That(customer.MaritalStatus == maritalStatus);
        }
Exemple #2
0
        public void Can_Map_Guid_Values_To_Guid_Properties()
        {
            // Arrange
            const int    id        = 1;
            const string firstName = "Bob";
            const string lastName  = "Smith";
            Guid         uniqueId  = Guid.NewGuid();

            dynamic dynamicPerson = new ExpandoObject();

            dynamicPerson.Id        = id;
            dynamicPerson.FirstName = firstName;
            dynamicPerson.LastName  = lastName;
            dynamicPerson.UniqueId  = uniqueId;

            // Act
            PersonWithProperties customer = Slapper.AutoMapper.MapDynamic <PersonWithProperties>(dynamicPerson);

            // Assert
            Assert.NotNull(customer);
            Assert.That(customer.Id == id);
            Assert.That(customer.FirstName == firstName);
            Assert.That(customer.LastName == lastName);
            Assert.That(customer.UniqueId == uniqueId);
        }
Exemple #3
0
        public void Can_Map_Guid_String_Values_To_Guid_Fields()
        {
            // Arrange
            const int    id        = 1;
            const string firstName = "Bob";
            const string lastName  = "Smith";
            Guid         uniqueId  = Guid.NewGuid();

            dynamic dynamicPerson = new ExpandoObject();

            dynamicPerson.Id        = id;
            dynamicPerson.FirstName = firstName;
            dynamicPerson.LastName  = lastName;
            dynamicPerson.UniqueId  = uniqueId.ToString(); // This is what we are testing

            // Act
            PersonWithFields customer = Slapper.AutoMapper.MapDynamic <PersonWithFields>(dynamicPerson);

            // Assert
            Assert.NotNull(customer);
            Assert.That(customer.Id == id);
            Assert.That(customer.FirstName == firstName);
            Assert.That(customer.LastName == lastName);
            Assert.That(Equals(customer.UniqueId, uniqueId));     // This is what we are testing
        }
Exemple #4
0
        public void Can_Map_Null_Values_To_Nullable_DateTime_Fields()
        {
            // Arrange
            const int    id        = 1;
            const string firstName = "Bob";
            const string lastName  = "Smith";
            DateTime     startDate = DateTime.Now.AddDays(-2);
            DateTime?    endDate   = null; // This is what we are testing

            dynamic dynamicPerson = new ExpandoObject();

            dynamicPerson.Id        = id;
            dynamicPerson.FirstName = firstName;
            dynamicPerson.LastName  = lastName;
            dynamicPerson.StartDate = startDate;
            dynamicPerson.EndDate   = endDate;

            // Act
            PersonWithFields customer = Slapper.AutoMapper.MapDynamic <PersonWithFields>(dynamicPerson);

            // Assert
            Assert.NotNull(customer);
            Assert.That(customer.Id == id);
            Assert.That(customer.FirstName == firstName);
            Assert.That(customer.LastName == lastName);
            Assert.That(customer.StartDate == startDate);
            Assert.That(customer.EndDate == endDate);   // This is what we are testing
        }
Exemple #5
0
        public void Can_Map_Enum_Values_To_Enum_Properties()
        {
            // Arrange
            const int    id        = 1;
            const string firstName = "Jimbo";
            const string lastName  = "Smith";
            const Gender gender    = Gender.Male;

            var dictionary = new Dictionary <string, object>
            {
                { "Id", id },
                { "FirstName", firstName },
                { "LastName", lastName },
                { "Gender", gender }
            };

            // Act
            var customer = Slapper.AutoMapper.Map <PersonWithProperties>(dictionary);

            // Assert
            Assert.NotNull(customer);
            Assert.That(customer.Id == id);
            Assert.That(customer.FirstName == firstName);
            Assert.That(customer.LastName == lastName);
            Assert.That(customer.Gender == gender);
        }
Exemple #6
0
        public void Can_Map_Complex_Nested_Members()
        {
            // Arrange
            const int     id         = 1;
            const string  firstName  = "Bob";
            const string  lastName   = "Smith";
            const int     orderId    = 1;
            const decimal orderTotal = 50.50m;

            var dictionary = new Dictionary <string, object>
            {
                { "Id", id },
                { "FirstName", firstName },
                { "LastName", lastName },
                { "Orders_Id", orderId },
                { "Orders_OrderTotal", orderTotal }
            };

            // Act
            var customer = Slapper.AutoMapper.Map <MapTestModels.CustomerWithOrdersList>(dictionary);

            // Assert
            Assert.NotNull(customer);
            Assert.That(customer.Id == id);
            Assert.That(customer.FirstName == firstName);
            Assert.That(customer.LastName == lastName);
            Assert.NotNull(customer.Orders);
            Assert.That(customer.Orders.Count == 1);
            Assert.That(customer.Orders.First().Id == orderId);
            Assert.That(customer.Orders.First().OrderTotal == orderTotal);
        }
Exemple #7
0
        public void Can_Map_DateTime_String_Values_To_Nullable_DateTime_Properties()
        {
            // Arrange
            const int    id        = 1;
            const string firstName = "Bob";
            const string lastName  = "Smith";
            DateTime     startDate = DateTime.Now.AddDays(-2);
            DateTime?    endDate   = DateTime.Now;

            dynamic dynamicPerson = new ExpandoObject();

            dynamicPerson.Id        = id;
            dynamicPerson.FirstName = firstName;
            dynamicPerson.LastName  = lastName;
            dynamicPerson.StartDate = startDate.ToString();
            dynamicPerson.EndDate   = endDate.ToString();

            // Act
            PersonWithProperties customer = Slapper.AutoMapper.MapDynamic <PersonWithProperties>(dynamicPerson);

            // Assert
            Assert.NotNull(customer);
            Assert.That(customer.Id == id);
            Assert.That(customer.FirstName == firstName);
            Assert.That(customer.LastName == lastName);
            Assert.That(customer.StartDate.ToString() == startDate.ToString());
            Assert.That(customer.EndDate.ToString() == endDate.ToString());
        }
Exemple #8
0
        public void Can_Use_Registered_TypeActivators_WithInterfaces()
        {
            // Arrange
            const string id    = "1";
            const int    count = 2;
            const Enum   enu   = Enum.Two;

            var dictionary = new Dictionary <string, object>
            {
                { "Id", id },
                { "Nested_Count", count },
                { "Nested_Enum", enu }
            };

            AutoMapper.Configuration.TypeActivators.Add(new TypeActivator());

            // Act
            var result = Slapper.AutoMapper.Map <IClass>(dictionary);

            // Assert
            Assert.NotNull(result);
            Assert.That(result.Id == id);
            Assert.That(result.Nested.Enum == enu);
            Assert.That(result.Nested.Count == count);
        }
Exemple #9
0
        public void TypeActivator_Order_Is_Adhered_To()
        {
            // Arrange
            const string id    = "1";
            const int    count = 2;
            const Enum   enu   = Enum.Two;

            var dictionary = new Dictionary <string, object>
            {
                { "Id", id },
                { "Nested_Count", count },
                { "Nested_Enum", enu }
            };

            AutoMapper.Configuration.TypeActivators.Add(new Class2TypeActivator());
            AutoMapper.Configuration.TypeActivators.Add(new Class2CopyTypeActivator());

            // Act
            var result = Slapper.AutoMapper.Map <IClass2>(dictionary);

            // Assert
            Assert.NotNull(result);
            Assert.That(result.Id == id);
            Assert.That(result.GetType() == typeof(Class2Copy));
        }
        public void Can_Map_To_Types_With_No_Identifiers()
        {
            // Arrange
            const string person1FirstName = "Bob";
            const string person1LastName  = "Smith";
            const string person2FirstName = "Nancy";
            const string person2LastName  = "Sue";

            dynamic person1 = new ExpandoObject();

            person1.FirstName = person1FirstName;
            person1.LastName  = person1LastName;

            dynamic person2 = new ExpandoObject();

            person2.FirstName = person2FirstName;
            person2.LastName  = person2LastName;

            var list = new List <dynamic> {
                person1, person2
            };

            // Act
            var persons = Slapper.AutoMapper.MapDynamic <PersonWithFields>(list).ToList();

            // Assert
            Assert.NotNull(persons);
            Assert.That(persons.Count == 2);
            Assert.That(persons[0].FirstName == person1FirstName);
            Assert.That(persons[0].LastName == person1LastName);
            Assert.That(persons[1].FirstName == person2FirstName);
            Assert.That(persons[1].LastName == person2LastName);
        }
        public void TestEqualList()
        {
            var v1 = new LogicVar();
            var v2 = new LogicVar();

            var goal  = MicroKanren.Equal(new [] { 5, 6 }, new[] { v1, v2 });
            var subst = goal(MicroKanren.GetEmptySubst()).SingleOrDefault();

            Assert.NotNull(subst);
            Assert.AreEqual(5, subst.GetValue(v1));
            Assert.AreEqual(6, subst.GetValue(v2));

            goal  = MicroKanren.Equal(new object[] { 5, 5, v1 }, new[] { v1, v1, v2 });
            subst = goal(MicroKanren.GetEmptySubst()).SingleOrDefault();

            Assert.NotNull(subst);
            Assert.AreEqual(5, subst.GetValue(v1));
            Assert.AreEqual(5, subst.GetValue(v2));

            goal = MicroKanren.Equal(new[] { 5, 6, 7 }, new[] { v1, v2 });
            Assert.IsNull(goal(MicroKanren.GetEmptySubst()));

            var v3 = new LogicVar();

            goal = MicroKanren.Equal(new[] { 5, 6 }, new[] { v1, v2, v3 });
            Assert.IsNull(goal(MicroKanren.GetEmptySubst()));
        }
Exemple #12
0
        public void Can_Map_DifferentsRows_to_Same_object()
        {
            dynamic dynamicCustomer = new ExpandoObject();

            dynamicCustomer.Id          = 1;
            dynamicCustomer.Name        = "Clark";
            dynamicCustomer.Phones_Id   = 1;
            dynamicCustomer.Phones_Name = "88888";
            dynamicCustomer.Emails_Id   = "1";
            dynamicCustomer.Emails_Name = "*****@*****.**";

            dynamic dynamicCustomer2 = new ExpandoObject();

            dynamicCustomer2.Id          = 1;
            dynamicCustomer2.Name        = "Clark";
            dynamicCustomer2.Phones_Id   = 2;
            dynamicCustomer2.Phones_Name = "99999";
            dynamicCustomer2.Emails_Id   = "2";
            dynamicCustomer2.Emails_Name = "*****@*****.**";

            var list = new List <dynamic> {
                dynamicCustomer, dynamicCustomer2
            };
            var customer = Slapper.AutoMapper.MapDynamic <NameValue>(list).FirstOrDefault();

            Assert.NotNull(customer);
            Assert.AreNotEqual(customer.Emails.FirstOrDefault(e => e.Id == 1).Name, customer.Phones.FirstOrDefault(p => p.Id == 1).Name);
        }
        public static void NotNull(object value)
        {
#if XUNIT
            FrameworkAssert.NotNull(value);
#else
            FrameworkAssert.IsNotNull(value);
#endif
        }
Exemple #14
0
        public void GetUserById()
        {
            //Arrange
            var databaseFactory = new DatabaseFactory();
            var userRepository  = new Repository <User>(databaseFactory);

            //Act
            var user = userRepository.GetById(1);

            //Asset
            Assert.NotNull(user);
        }
        public void GetEmployeeById()
        {
            //Arrange
            var databaseFactory    = new DatabaseFactory();
            var employeeRepository = new Repository <Employee>(databaseFactory);

            //Act
            var employee = employeeRepository.GetById(1001);

            //Asset
            Assert.NotNull(employee);
        }
Exemple #16
0
        public void Can_Detect_Duplicate_Parent_Members_And_Properly_Instantiate_The_Object_Only_Once()
        {
            // Arrange
            const int     id         = 1;
            const string  firstName  = "Bob";
            const string  lastName   = "Smith";
            const int     orderId    = 1;
            const decimal orderTotal = 50.50m;

            var dictionary = new Dictionary <string, object>
            {
                { "Id", id },
                { "FirstName", firstName },
                { "LastName", lastName },
                { "Orders_Id", orderId },
                { "Orders_OrderTotal", orderTotal }
            };

            var dictionary2 = new Dictionary <string, object>
            {
                { "Id", id },
                { "FirstName", firstName },
                { "LastName", lastName },
                { "Orders_Id", orderId + 1 },
                { "Orders_OrderTotal", orderTotal + 1 }
            };

            var listOfDictionaries = new List <Dictionary <string, object> > {
                dictionary, dictionary2
            };

            // Act
            var customers = Slapper.AutoMapper.Map <MapTestModels.CustomerWithOrdersList>(listOfDictionaries);

            var customer = customers.FirstOrDefault();

            // Assert
            Assert.That(customers.Count() == 1);
            Assert.NotNull(customer);
            Assert.That(customer.Id == id);
            Assert.That(customer.FirstName == firstName);
            Assert.That(customer.LastName == lastName);
            Assert.NotNull(customer.Orders);
            Assert.That(customer.Orders.Count == 2);
            Assert.That(customer.Orders[0].Id == orderId);
            Assert.That(customer.Orders[0].OrderTotal == orderTotal);
            Assert.That(customer.Orders[1].Id == orderId + 1);
            Assert.That(customer.Orders[1].OrderTotal == orderTotal + 1);
        }
Exemple #17
0
        public void Can_map_grandchild_with_parts_of_same_name_as_child()
        {
            var data = new List <Dictionary <string, object> > {
                new Dictionary <string, object> {
                    { "Id", 1 },
                    { "Members_Id", 1 },
                    { "Members_SubMembers_Id", 1 }
                }
            };

            var club = AutoMapper.MapDynamic <Club>(data).Single();

            Assert.That(club.Members.Count == 1);
            Assert.NotNull(club.Members[0].SubMembers);
            Assert.That(club.Members[0].SubMembers.Count == 1);
        }
        public void TestEqualSame()
        {
            var v1 = new LogicVar();
            var v2 = new LogicVar();

            var goal1 = MicroKanren.Equal(v1, 5);
            var goal2 = MicroKanren.Equal(v2, 5);
            var goal3 = MicroKanren.Equal(v2, v1);

            var subst1 = goal1(MicroKanren.GetEmptySubst()).Single();
            var subst2 = goal2(subst1).Single();
            var subst3 = goal3(subst2).SingleOrDefault();

            Assert.NotNull(subst3);
            Assert.AreEqual(5, subst3.GetValue(v1));
            Assert.AreEqual(5, subst3.GetValue(v2));
        }
Exemple #19
0
        public void Can_Map_Int32_Values_To_NUllable_Enum_Properties()
        {
            dynamic person = new ExpandoObject();

            person.Id            = 1;
            person.FirstName     = "FirstName";
            person.LastName      = "LastName";
            person.MaritalStatus = 2;
            person.Gender        = Gender.Male;

            // Act
            var customer = Slapper.AutoMapper.MapDynamic <PersonWithProperties>(person);

            // Assert
            Assert.NotNull(customer);
            Assert.That(customer.MaritalStatus == MaritalStatus.Single);
        }
        public void Can_Handle_Mapping_A_Single_Dynamic_Object()
        {
            // Arrange
            dynamic dynamicCustomer = new ExpandoObject();

            dynamicCustomer.Id                = 1;
            dynamicCustomer.FirstName         = "Bob";
            dynamicCustomer.LastName          = "Smith";
            dynamicCustomer.Orders_Id         = 1;
            dynamicCustomer.Orders_OrderTotal = 50.50m;

            // Act
            var customer = Slapper.AutoMapper.MapDynamic <Customer>(dynamicCustomer) as Customer;

            // Assert
            Assert.NotNull(customer);
            Assert.That(customer.Orders.Count == 1);
        }
Exemple #21
0
        public void Can_Map_Matching_Field_Names_Using_Dynamic()
        {
            // Arrange
            dynamic dynamicPerson = new ExpandoObject();

            dynamicPerson.Id        = 1;
            dynamicPerson.FirstName = "Clark";
            dynamicPerson.LastName  = "Kent";

            // Act
            var person = Slapper.AutoMapper.MapDynamic <Person>(dynamicPerson) as Person;

            // Assert
            Assert.NotNull(person);
            Assert.That(person.Id == 1);
            Assert.That(person.FirstName == "Clark");
            Assert.That(person.LastName == "Kent");
        }
Exemple #22
0
        public void Can_Map_Matching_Field_Names_With_Ease()
        {
            // Arrange
            var dictionary = new Dictionary <string, object>
            {
                { "Id", 1 },
                { "FirstName", "Clark" },
                { "LastName", "Kent" }
            };

            // Act
            var person = Slapper.AutoMapper.Map <Person>(dictionary);

            // Assert
            Assert.NotNull(person);
            Assert.That(person.Id == 1);
            Assert.That(person.FirstName == "Clark");
            Assert.That(person.LastName == "Kent");
        }
        public void TestEqualDifferent()
        {
            var v1 = new LogicVar();
            var v2 = new LogicVar();

            var goal1 = MicroKanren.Equal(v1, 5);
            var goal2 = MicroKanren.Equal(v2, "5");

            var subst1 = goal1(MicroKanren.GetEmptySubst()).SingleOrDefault();

            Assert.IsNull(MicroKanren.Equal(6, v1)(subst1));

            var subst2 = goal2(subst1).SingleOrDefault();

            Assert.NotNull(subst2);
            Assert.AreEqual(5, subst2.GetValue(v1));
            Assert.AreEqual("5", subst2.GetValue(v2));

            Assert.IsNull(MicroKanren.Equal(v2, v1)(subst2));
        }
        public void TestRecursiveConjunction()
        {
            var v1 = new LogicVar();
            var v2 = new LogicVar();
            var v3 = new LogicVar();

            var goal1 = MicroKanren.Equal(v1, 5);
            var goal2 = MicroKanren.Equal(v2, v1);
            var goal3 = MicroKanren.Equal(v3, v2);

            var conjGoal = MicroKanren.Conjunction(goal3, MicroKanren.Conjunction(goal1, goal2));

            var subst = conjGoal(MicroKanren.GetEmptySubst()).SingleOrDefault();

            Assert.NotNull(subst);

            Assert.AreEqual(5, subst.GetValue(v1));
            Assert.AreEqual(5, subst.GetValue(v2));
            Assert.AreEqual(5, subst.GetValue(v3));
        }
Exemple #25
0
        public void Can_Handle_Mapping_An_Empty_List()
        {
            // Arrange
            dynamic dynamicCustomer = new ExpandoObject();

            dynamicCustomer.Id                = 1;
            dynamicCustomer.FirstName         = "Bob";
            dynamicCustomer.LastName          = "Smith";
            dynamicCustomer.Orders_Id         = null;
            dynamicCustomer.Orders_OrderTotal = null;

            // Act
            var customer = Slapper.AutoMapper.MapDynamic <Customer>(dynamicCustomer) as Customer;

            // Assert
            Assert.NotNull(customer);

            // Empty list
            Assert.That(customer.Orders != null);
            Assert.That(customer.Orders.Count == 0);
        }
        public void TestDisjunction()
        {
            var v1 = new LogicVar();
            var v2 = new LogicVar();

            var goal0 = MicroKanren.Equal(v1, v2);
            var goal1 = MicroKanren.Equal(v1, 5);
            var goal2 = MicroKanren.Equal(v2, 6);

            var conjGoal = MicroKanren.Conjunction(goal0, MicroKanren.Disjunction(goal1, goal2));

            var subst = conjGoal(MicroKanren.GetEmptySubst());

            Assert.NotNull(subst);

            var substList = subst.ToList();

            Assert.AreEqual(5, substList[0].GetValue(v1));
            Assert.AreEqual(5, substList[0].GetValue(v2));
            Assert.AreEqual(6, substList[1].GetValue(v1));
            Assert.AreEqual(6, substList[1].GetValue(v2));
        }
Exemple #27
0
        public void enable_grad()
        {
            //>>> x = torch.tensor([1], requires_grad = True)
            //>>> with torch.no_grad():
            //...   with torch.enable_grad():
            //...     y = x * 2
            //>>> y.requires_grad
            //True
            //>>> y.backward()
            //>>> x.grad
            //>>> @torch.enable_grad()
            //...def doubler(x):
            //...     return x * 2
            //>>> with torch.no_grad():
            //...     z = doubler(x)
            //>>> z.requires_grad
            //True

            var    x = torch.tensor(new double[] { 1 }, requires_grad: true);
            Tensor y = null;

            Py.With(torch.no_grad(), _ =>
            {
                Py.With(torch.enable_grad(), __ =>
                {
                    y = x * 2;
                    Assert.AreEqual(true, y.requires_grad);
                    y.backward();
                    var grad = x.grad;
                    Assert.NotNull(grad);
                });
            });
            Py.With(torch.no_grad(), _ =>
            {
                var z = x * 2;
            });
            Assert.AreEqual(true, y.requires_grad);
        }
        public void Can_Map_Matching_Property_Names()
        {
            // Arrange
            const int    id        = 1;
            const string firstName = "Bob";
            const string lastName  = "Smith";

            var dictionary = new Dictionary <string, object>
            {
                { "Id", id },
                { "FirstName", firstName },
                { "LastName", lastName }
            };

            // Act
            var customer = Slapper.AutoMapper.Map <PersonWithProperties>(dictionary);

            // Assert
            Assert.NotNull(customer);
            Assert.That(customer.Id == id);
            Assert.That(customer.FirstName == firstName);
            Assert.That(customer.LastName == lastName);
        }
        public void Can_Map_Matching_Property_Names_With_Different_Types()
        {
            // Arrange
            const int    id        = 1;
            const string firstName = "Bob";
            const string lastName  = "Smith";

            var dictionary = new Dictionary <string, object>
            {
                { "Id", Double.Parse("1.245698", CultureInfo.InvariantCulture) },
                { "FirstName", firstName },
                { "LastName", lastName }
            };

            // Act
            var customer = Slapper.AutoMapper.Map <PersonWithProperties>(dictionary);

            // Assert
            Assert.NotNull(customer);
            Assert.That(customer.Id == id);
            Assert.That(customer.FirstName == firstName);
            Assert.That(customer.LastName == lastName);
        }
Exemple #30
0
        public void Can_Map_Null_Values()
        {
            // Arrange
            const int    id        = 1;
            const string firstName = null;
            const string lastName  = "Smith";

            var dictionary = new Dictionary <string, object>
            {
                { "Id", id },
                { "FirstName", null },
                { "LastName", lastName }
            };

            // Act
            var customer = Slapper.AutoMapper.Map <PersonWithFields>(dictionary);

            // Assert
            Assert.NotNull(customer);
            Assert.That(customer.Id == id);
            Assert.That(customer.FirstName == firstName);
            Assert.That(customer.LastName == lastName);
        }