public void Each_should_call_the_provided_action()
        {
            IEnumerable<int> list = new[]{ 4 };
            bool isCalled = false;

            list.Each(i=> isCalled = true);

            Assert.True(isCalled);
        }
        protected void LoadBasicStrings(IRedisClient redis)
        {
            int A = 'A';
            int Z = 'Z';
            var letters = (Z - A + 1).Times(i => ((char)(i + A)).ToString());
            var numbers = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

            var pos = 0;
            letters.Each(x => redis.Set("string:letters/" + x, x));
            numbers.Each(x => redis.Set("string:numbers/" + pos++, x));
        }
        public void numbers_should_be_invalid()
        {
            var numbers = new[]
                              {
                                  "1,00",
                                  "100,1",
                                  "100,1.01",
                                  "A,Jun.K",
                              };

            numbers.Each(x => x.IsValidNumber(CultureInfo.CreateSpecificCulture("en-us")).ShouldBeFalse());
        }
        public void numbers_with_commas_and_periods_should_be_valid()
        {
            var numbers = new[]
                              {
                                  "1,000",
                                  "100.1",
                                  "1000.1",
                                  "1,000.1",
                                  "10,000.1",
                                  "100,000.1",
                              };

            numbers.Each(x => x.IsValidNumber(CultureInfo.CreateSpecificCulture("en-us")).ShouldBeTrue());
        }
        protected void LoadDifferentKeyTypes(IRedisClient redis)
        {
            int A = 'A';
            int Z = 'Z';
            var letters = (Z - A + 1).Times(i => ((char)(i + A)).ToString());
            var numbers = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

            redis.RemoveEntry("list:letters", "list:numbers"); //don't add duplicates to existing list
            letters.Each(x => redis.AddItemToList("list:letters", x));
            numbers.Each(x => redis.AddItemToList("list:numbers", x));

            letters.Each(x => redis.AddItemToSet("set:letters", x));
            numbers.Each(x => redis.AddItemToSet("set:numbers", x));

            var pos = 0;
            letters.Each(x => redis.AddItemToSortedSet("zset:letters", x, pos++));
            pos = 0;
            numbers.Each(x => redis.AddItemToSortedSet("zset:numbers", x, pos++));

            pos = 0;
            letters.Each(x => redis.SetEntryInHash("hash:letters", x, (pos++).ToString()));
            pos = 0;
            numbers.Each(x => redis.SetEntryInHash("hash:numbers", x, (pos++).ToString()));
        }
        public void Can_support_multiple_self_references()
        {
            var customers = new[]
            {
                new MultiSelfCustomer
                {
                    Name = "Customer 1",
                    HomeAddress = new SelfCustomerAddress
                    {
                        AddressLine1 = "1 Home Street",
                        Country = "Australia"
                    },
                    WorkAddress = new SelfCustomerAddress
                    {
                        AddressLine1 = "1 Work Street",
                        Country = "Australia"
                    },
                },
                new MultiSelfCustomer
                {
                    Name = "Customer 2",
                    HomeAddress = new SelfCustomerAddress
                    {
                        AddressLine1 = "2 Home Park",
                        Country = "USA"
                    },
                    WorkAddress = new SelfCustomerAddress
                    {
                        AddressLine1 = "2 Work Park",
                        Country = "UK"
                    },
                },
            };

            customers.Each(x =>
                db.Save(x, references: true));

            var results = db.LoadSelect<MultiSelfCustomer>(q =>
                q.HomeAddressId != null &&
                q.WorkAddressId != null);

            results.PrintDump();

            Assert.That(results.Count, Is.EqualTo(2));
            Assert.That(results[0].HomeAddress.AddressLine1, Is.StringContaining("Home"));
            Assert.That(results[0].WorkAddress.AddressLine1, Is.StringContaining("Work"));
            Assert.That(results[1].HomeAddress.AddressLine1, Is.StringContaining("Home"));
            Assert.That(results[1].WorkAddress.AddressLine1, Is.StringContaining("Work"));

            var ukAddress = db.Single<SelfCustomerAddress>(q => q.Country == "UK");
            ukAddress.PrintDump();
            Assert.That(ukAddress.AddressLine1, Is.EqualTo("2 Work Park"));
        }
        public void Can_load_list_of_self_references()
        {
            var customers = new[]
            {
                new SelfCustomer
                {
                    Name = "Customer 1",
                    PrimaryAddress = new SelfCustomerAddress
                    {
                        AddressLine1 = "1 Australia Street",
                        Country = "Australia"
                    },
                },
                new SelfCustomer
                {
                    Name = "Customer 2",
                    PrimaryAddress = new SelfCustomerAddress
                    {
                        AddressLine1 = "2 Prospect Park",
                        Country = "USA"
                    },
                },
            };

            db.Save(new SelfCustomer { Name = "Dummy Incrementer" });

            customers.Each(x =>
                db.Save(x, references: true));

            var results = db.LoadSelect<SelfCustomer>(q => q.SelfCustomerAddressId != null);
            Assert.That(results.Count, Is.EqualTo(2));
            Assert.That(results.All(x => x.PrimaryAddress != null));

            var customer1 = results.First(x => x.Name == "Customer 1");
            Assert.That(customer1.PrimaryAddress.Country, Is.EqualTo("Australia"));

            var customer2 = results.First(x => x.Name == "Customer 2");
            Assert.That(customer2.PrimaryAddress.Country, Is.EqualTo("USA"));

            results = db.LoadSelect<SelfCustomer>(q => q.Name == "Customer 1");
            Assert.That(results.Count, Is.EqualTo(1));
            Assert.That(results[0].PrimaryAddress.Country, Is.EqualTo("Australia"));
        }
        public void Each()
        {
            var source = new[] {1, 2, 3};
            var resultBuffer = new List<int>();

            source.Each(resultBuffer.Add);

            CollectionAssert.AreEqual(source, resultBuffer);
        }
        public void Can_Join_on_matching_Alias_convention()
        {
            db.DropAndCreateTable<AliasedCustomer>();
            db.DropAndCreateTable<AliasedCustomerAddress>();

            var customers = new[]
            {
                new AliasedCustomer
                {
                    Name = "Customer 1",
                    PrimaryAddress = new AliasedCustomerAddress {
                        AddressLine1 = "1 Australia Street",
                        Country = "Australia"
                    },
                },
                new AliasedCustomer
                {
                    Name = "Customer 2",
                    PrimaryAddress = new AliasedCustomerAddress {
                        AddressLine1 = "2 America Street",
                        Country = "USA"
                    },
                },
                new AliasedCustomer
                {
                    Name = "Customer 3",
                    PrimaryAddress = new AliasedCustomerAddress {
                        AddressLine1 = "3 Canada Street",
                        Country = "Canada"
                    },
                },
            };

            customers.Each(c =>
                db.Save(c, references: true));

            db.Insert(
                new Country { CountryName = "Australia", CountryCode = "AU" },
                new Country { CountryName = "USA", CountryCode = "US" },
                new Country { CountryName = "Italy", CountryCode = "IT" },
                new Country { CountryName = "Spain", CountryCode = "ED" });

            //Normal Join
            var dbCustomers = db.Select<AliasedCustomer>(q => q
                .Join<AliasedCustomerAddress>()
                .Join<AliasedCustomerAddress, Country>((ca, c) => ca.Country == c.CountryName));

            Assert.That(dbCustomers.Count, Is.EqualTo(2));

            //Left Join
            dbCustomers = db.Select<AliasedCustomer>(q => q
                .Join<AliasedCustomerAddress>()
                .LeftJoin<AliasedCustomerAddress, Country>((ca, c) => ca.Country == c.CountryName));

            Assert.That(dbCustomers.Count, Is.EqualTo(3));

            //Warning: Right and Full Joins are not implemented by Sqlite3. Avoid if possible.
            var dbCountries = db.Select<Country>(q => q
                .LeftJoin<AliasedCustomerAddress>((c, ca) => ca.Country == c.CountryName)
                .LeftJoin<AliasedCustomerAddress, AliasedCustomer>());

            Assert.That(dbCountries.Count, Is.EqualTo(4));

            var dbAddresses = db.Select<AliasedCustomerAddress>(q => q
                .LeftJoin<Country>((ca, c) => ca.Country == c.CountryName)
                .LeftJoin<AliasedCustomerAddress, AliasedCustomer>());

            Assert.That(dbAddresses.Count, Is.EqualTo(3));
        }
        public void Can_do_joins_with_complex_wheres_using_SqlExpression()
        {
            var customers = new[]
            {
                new Customer
                {
                    Name = "Customer 1",
                    PrimaryAddress = new CustomerAddress {
                        AddressLine1 = "1 Australia Street",
                        Country = "Australia"
                    },
                    Orders = new[] {
                        new Order { LineItem = "Line 1", Qty = 1, Cost = 1.99m },
                        new Order { LineItem = "Line 1", Qty = 2, Cost = 3.98m },
                        new Order { LineItem = "Line 2", Qty = 1, Cost = 1.49m },
                        new Order { LineItem = "Line 2", Qty = 2, Cost = 2.98m },
                        new Order { LineItem = "Australia Flag", Qty = 1, Cost = 9.99m },
                    }.ToList(),
                },
                new Customer
                {
                    Name = "Customer 2",
                    PrimaryAddress = new CustomerAddress {
                        AddressLine1 = "2 Prospect Park",
                        Country = "USA"
                    },
                    Orders = new[] {
                        new Order { LineItem = "USA", Qty = 1, Cost = 20m },
                    }.ToList(),
                },
            };

            customers.Each(c =>
                db.Save(c, references: true));

            db.Insert(
                new Country { CountryName = "Australia", CountryCode = "AU" },
                new Country { CountryName = "USA", CountryCode = "US" });

            var results = db.Select<FullCustomerInfo, Customer>(q => q
                .Join<CustomerAddress>() //implicit
                .Join<Customer, Order>() //explicit
                .Where(c => c.Name == "Customer 1")
                .And<Order>(o => o.Cost < 2)
                .Or<Order>(o => o.LineItem == "Australia Flag"));

            var costs = results.ConvertAll(x => x.Cost);
            Assert.That(costs, Is.EquivalentTo(new[] { 1.99m, 1.49m, 9.99m }));
            var orderIds = results.ConvertAll(x => x.OrderId);
            var expectedOrderIds = new []{customers[0].Orders[0].Id, customers[0].Orders[2].Id, customers[0].Orders[4].Id};
            Assert.That(orderIds, Is.EquivalentTo(expectedOrderIds));

            //Same as above using using db.From<Customer>()
            results = db.Select<FullCustomerInfo>(db.From<Customer>()
                .Join<CustomerAddress>() //implicit
                .Join<Customer, Order>() //explicit
                .Where(c => c.Name == "Customer 1")
                .And<Order>(o => o.Cost < 2)
                .Or<Order>(o => o.LineItem == "Australia Flag"));

            costs = results.ConvertAll(x => x.Cost);
            Assert.That(costs, Is.EquivalentTo(new[] { 1.99m, 1.49m, 9.99m }));

            results = db.Select<FullCustomerInfo, Customer>(q => q
                .Join<Customer, CustomerAddress>()
                .Join<Customer, Order>()
                .Where(c => c.Name == "Customer 2")
                .And<CustomerAddress, Order>((a, o) => a.Country == o.LineItem));

            costs = results.ConvertAll(x => x.Cost);
            Assert.That(costs, Is.EquivalentTo(new[] { 20m }));

            var countryResults = db.Select<FullCustomerInfo>(db.From<Customer>()
                .Join<CustomerAddress>()                     //implicit join with Customer
                .Join<Order>((c, o) => c.Id == o.CustomerId) //explicit join condition
                .Join<CustomerAddress, Country>((ca, c) => ca.Country == c.CountryName)
                .Where(c => c.Name == "Customer 2")          //implicit condition with Customer
                .And<CustomerAddress, Order>((a, o) => a.Country == o.LineItem));

            costs = countryResults.ConvertAll(x => x.Cost);
            Assert.That(costs, Is.EquivalentTo(new[] { 20m }));
            Assert.That(countryResults.ConvertAll(x => x.CountryCode), Is.EquivalentTo(new[] { "US" }));
        }
예제 #11
0
        public void numbers_with_commas_and_periods_should_be_valid_in_european_culture()
        {
            var numbers = new[]
                              {
                                  "1.000",
                                  "100,1",
                                  "1000,1",
                                  "1.000,1",
                                  "10.000,1",
                                  "100.000,1",
                              };

            numbers.Each(x => x.IsValidNumber(CultureInfo.CreateSpecificCulture("de-DE")).ShouldBeTrue());
        }