public void SelectManyFilterGroupTest()
        {
            PetOwner[] owners =
            { new PetOwner {
                  Name = "Mike", PetNames = new List <string>{
                      "Fluffy", "Mittens"
                  }
              },
              new PetOwner {
                  Name = "Ramit", PetNames = new List <string>{
                      "Roger", "Muffy", "Fluffy"
                  }
              } };

            var a = new ExternalInput <List <string> >();
            var g = a.SelectMany(v => v).GroupBy(v => (string.IsNullOrEmpty(v) ? "" : v.Substring(0, 1)), _ => 1, (ai, bi) => ai + bi).Where(v => v.Item1 == "M");

            var aData = owners.Select(v => v.PetNames);

            var inMemory = new EnumerableAdapter();

            inMemory.SetInput(a, aData);
            var gNew = inMemory.Get(g);

            Assert.Equal(1, gNew.Count());
            Assert.True(gNew.Any(v => v.Item1 == "M" && v.Item2 == 2));
        }
        public void EmployeeTest()
        {
            // ExternalInput allows for late binding
            var msftEmployees = new ExternalInput <Employee>();

            // Logic to find msft youth, veterans and inBetweeners
            var youthLogic        = msftEmployees.Where(emp => emp.Age < 30);
            var veteransLogic     = msftEmployees.Where(emp => emp.Age > 40);
            var inBetweenersLogic = msftEmployees.Where(emp => emp.Age >= 30 && emp.Age <= 40);
            var averageAgeLogic   = msftEmployees.Select(emp => (double)emp.Age).Average();

            // Set up the adapter layer which performs operations on the data
            // var azureAliqAdapter = new AzureAliqAdapter();
            var azureAliqAdapter = new EnumerableAdapter();

            // Data can be anywhere! For example, Azure blob container or an array
            var dataStore = new Employee[]
            {
                new Employee {
                    Name = "Deepak Shankargouda", Age = 20
                },
                new Employee {
                    Name = "Sergey Shandar", Age = 22
                },
                new Employee {
                    Name = "Mike Liu", Age = 18
                },
                new Employee {
                    Name = "Bob Smith", Age = 31
                },
                new Employee {
                    Name = "Mickey Mouse", Age = 89
                },
            };

            azureAliqAdapter.SetInput(msftEmployees, dataStore);

            // Calculate results!
            // logic to find msft youth, veterans and inBetweeners
            var youth        = azureAliqAdapter.Get(youthLogic).ToArray();
            var veterans     = azureAliqAdapter.Get(veteransLogic).ToArray();
            var inBetweeners = azureAliqAdapter.Get(inBetweenersLogic).ToArray();
            var averageAge   = azureAliqAdapter.Get(averageAgeLogic).ToArray();

            Assert.Equal(youth.Length, 3);
            Assert.True(youth.Any(v => v.Name == "Deepak Shankargouda" && v.Age == 20));
            Assert.True(youth.Any(v => v.Name == "Sergey Shandar" && v.Age == 22));
            Assert.True(youth.Any(v => v.Name == "Mike Liu" && v.Age == 18));
            Assert.False(youth.Any(v => v.Name == "Bob Smith" && v.Age == 31));
            Assert.False(youth.Any(v => v.Name == "Mickey Mouse" && v.Age == 89));

            Assert.Equal(veterans.Length, 1);
            Assert.True(veterans.Any(v => v.Name == "Mickey Mouse" && v.Age == 89));

            Assert.Equal(inBetweeners.Length, 1);
            Assert.True(inBetweeners.Any(v => v.Name == "Bob Smith" && v.Age == 31));

            Assert.Equal(averageAge, new[] { 36.0 });
        }
        public void TestAverage()
        {
            var a = new ExternalInput <double>();
            var g = a.Average();

            //
            var aData = new double[] { 3, 4, 5 };

            //
            var inMemory = new EnumerableAdapter();

            inMemory.SetInput(a, aData);
            var gNew = inMemory.Get(g);

            Assert.Equal(4.0, gNew.First());
        }
        public void WhereTestNumber()
        {
            var a = new ExternalInput <int>();
            var g = a.Where(v => (v > 7 && v % 4 == 0));

            //
            var aData = new int[] { 12, 9, 3, 4, 8 };

            //
            var inMemory = new EnumerableAdapter();

            inMemory.SetInput(a, aData);
            var gNew = inMemory.Get(g);

            Assert.Equal(2, gNew.Count());
            Assert.False(gNew.Contains(12));
            Assert.True(gNew.Contains(8));
        }
        public void TestSetInput()
        {
            // logic
            var a = new ExternalInput <string>();

            // data
            var aTable = new[] { "Hello", "world", "!" };

            // back end
            var inMemory = new EnumerableAdapter();

            // binding
            inMemory.SetInput(a, aTable);

            // get
            var newA = inMemory.Get(a);

            Assert.Equal(aTable, newA);
        }
        public void WhereTestString()
        {
            var a = new ExternalInput <string>();
            var g = a.Where(v => (v.StartsWith("M")));

            //
            var aData = new string[] { "Mike", "Sergey", "Mandar" };

            //
            var inMemory = new EnumerableAdapter();

            inMemory.SetInput(a, aData);
            var gNew = inMemory.Get(g);

            Assert.Equal(2, gNew.Count());
            Assert.True(gNew.Contains("Mike"));
            Assert.True(gNew.Contains("Mandar"));
            Assert.False(gNew.Contains("Sergey"));
        }
        public void SelectManyTest()
        {
            char[][] multiNumbers = new char[][] { new[] { 'a', 'a' }, new[] { 'b', 'c' }, new[] { 'd', 'd' } };

            var a = new ExternalInput <char[]>();
            var g = a.SelectMany(v => v).Where(character => character < 'd');

            var aData = new char[][] { new[] { 'a', 'a' }, new[] { 'b', 'c' }, new[] { 'd', 'd' } };

            var inMemory = new EnumerableAdapter();

            inMemory.SetInput(a, aData);
            var gNew = inMemory.Get(g);

            Assert.Equal(4, gNew.Count());
            Assert.True(gNew.Contains('a'));
            Assert.True(gNew.Contains('b'));
            Assert.True(gNew.Contains('c'));
            Assert.False(gNew.Contains('d'));
        }
        public void GroupingTest()
        {
            var a = new ExternalInput <double>();
            var g = a.GroupBy(v => v.ToString(), _ => 1, (ai, bi) => ai + bi);


            //data
            var aData = new double[] { 6, 6, 5, 7, 7, 7 };

            //backend
            var inMemory = new EnumerableAdapter();

            inMemory.SetInput(a, aData);
            var gNew = inMemory.Get(g);

            Assert.Equal(3, gNew.Count());
            Assert.True(gNew.Any(v => v.Item1 == "5" && v.Item2 == 1));
            Assert.True(gNew.Any(v => v.Item1 == "6" && v.Item2 == 2));
            Assert.True(gNew.Any(v => v.Item1 == "7" && v.Item2 == 3));
        }
        public void BankTotals()
        {
            BankAccount[] banks =
            {
                new BankAccount {
                    BankName = "Mike", CashAmount = (decimal)380.8, City = "Chicago", State = "IL"
                },
                new BankAccount {
                    BankName = "Sergey", CashAmount = (decimal)480.8, City = "Seattle", State = "WA"
                },
                new BankAccount {
                    BankName = "BankOfAmerica", CashAmount = (decimal)1480.8, City = "Chicago", State = "IL"
                },
                new BankAccount {
                    BankName = "CapitalOne", CashAmount = (decimal)1280.8, City = "Fairfax", State = "VA"
                },
                new BankAccount {
                    BankName = "JPMorgan", CashAmount = (decimal)980.8, City = "Seattle", State = "WA"
                },
                new BankAccount {
                    BankName = "WellsFargo", CashAmount = (decimal)180.8, City = "LosAngeles", State = "CA"
                }
            };

            var a = new ExternalInput <BankAccount>();
            var g = a.GroupBy(v => (v.City.ToString() + ";" + v.State.ToString()), ai => ai.CashAmount, (ai, bi) => ai + bi);

            var aData = banks;

            var inMemory = new EnumerableAdapter();

            inMemory.SetInput(a, aData);
            var gNew = inMemory.Get(g);

            Assert.Equal(4, gNew.Count());
            Assert.True(true);
            Assert.True(gNew.Any(v => v.Item1 == "Chicago;IL" && v.Item2 == (decimal)1861.6));
            Assert.True(gNew.Any(v => v.Item1 == "Seattle;WA" && v.Item2 == (decimal)1461.6));
            Assert.True(gNew.Any(v => v.Item1 == "Fairfax;VA" && v.Item2 == (decimal)1280.8));
            Assert.True(gNew.Any(v => v.Item1 == "LosAngeles;CA" && v.Item2 == (decimal)180.8));
        }
        public void InventoryTotalByStoreAndCity()
        {
            InventoryItem[] inventory =
            {
                new InventoryItem {
                    ItemName = "Socks", NumberOfItems = 5, StoreName = "Walmart", City = "Bellevue"
                },
                new InventoryItem {
                    ItemName = "Cheezits", NumberOfItems = 2, StoreName = "Walmart", City = "Kirkland"
                },
                new InventoryItem {
                    ItemName = "Socks", NumberOfItems = 9, StoreName = "Walgreen", City = "Bellevue"
                },
                new InventoryItem {
                    ItemName = "Shirts", NumberOfItems = 3, StoreName = "Walgreen", City = "Kirkland"
                },
                new InventoryItem {
                    ItemName = "Beer", NumberOfItems = 8, StoreName = "Walgreen", City = "Kirkland"
                },
            };


            var a = new ExternalInput <InventoryItem>();
            var g = a.GroupBy(v => (v.StoreName.ToString() + ";" + v.City.ToString()), ai => ai.NumberOfItems, (ai, bi) => ai + bi);

            var aData = inventory;

            var inMemory = new EnumerableAdapter();

            inMemory.SetInput(a, aData);
            var gNew = inMemory.Get(g);

            Assert.Equal(4, gNew.Count());
            Assert.True(gNew.Any(v => v.Item1 == "Walmart;Bellevue" && v.Item2 == 5));
            Assert.True(gNew.Any(v => v.Item1 == "Walmart;Kirkland" && v.Item2 == 2));
            Assert.True(gNew.Any(v => v.Item1 == "Walgreen;Bellevue" && v.Item2 == 9));
            Assert.True(gNew.Any(v => v.Item1 == "Walgreen;Kirkland" && v.Item2 == 11));
        }
        public void TestGroupByDoubleMean()
        {
            // logic
            var a = new ExternalInput <double>();
            var g = a.Average();

            // data
            double[] aTable = (new[] { 8.0, 30.0, 4.0, 18.0 });

            // back end
            var inMemory = new EnumerableAdapter();

            // binding
            inMemory.SetInput <double>(a, aTable);

            // get
            var gNew = inMemory.Get(g);

            Assert.Equal(1, gNew.Count());
            var item = gNew.First();

            Assert.Equal(15, item);
        }
        public void TestGroupByIntegersSum()
        {
            // logic
            var a = new ExternalInput <int>();
            var g = a.Aggregate((ai, bi) => ai + bi);

            // data
            var aTable = new[] { 8, 30, 4 };

            // back end
            var inMemory = new EnumerableAdapter();

            // binding
            inMemory.SetInput(a, aTable);

            // get
            var gNew = inMemory.Get(g);

            Assert.Equal(1, gNew.Count());
            var item = gNew.First();

            Assert.Equal(42, item);
        }
        public void TestGroupBy()
        {
            // logic
            var a = new ExternalInput <string>();
            var g = a.Aggregate((ai, bi) => ai + bi);

            // data
            var aTable = new[] { "Hello", "world", "!" };

            // back end
            var inMemory = new EnumerableAdapter();

            // binding
            inMemory.SetInput(a, aTable);

            // get
            var gNew = inMemory.Get(g);

            Assert.Equal(1, gNew.Count());
            var item = gNew.First();

            Assert.Equal("Helloworld!".Length, item.Length);
        }
Esempio n. 14
0
        public async Task LottoryExampleTest()
        {
            // ExternalInput allows for late binding
            var lotteryExternalInput = new ExternalInput <Lottos>();

            // Get a list of lottery items and the API resource page
            List <Lottos> lotteryList = new List <Lottos>();
            string        page        = "https://data.ny.gov/api/views/5xaw-6ayf/rows.json?accessType=DOWNLOAD";

            // Use HTTP Client to retrieve data
            using (HttpClient client = new HttpClient())
                using (HttpResponseMessage response = await client.GetAsync(page))
                    using (HttpContent content = response.Content)
                    {
                        // ... Read the string.
                        string result = await content.ReadAsStringAsync();

                        if (result != null)
                        {
                            //Get the JSON data you need
                            JObject m     = JObject.Parse(result);
                            var     array = m["data"];

                            //Take in and process JSON data
                            foreach (JToken token in array)
                            {
                                Lottos temp = new Lottos(token[8].ToString(), token[9].ToString(), token[10].ToString());
                                lotteryList.Add(temp);
                            }

                            var azureAliqAdapter = new EnumerableAdapter();

                            //Select drawings where the Mega Ball number is 49, as 49 is the square of 7 (a lucky number).
                            var doublyLuckyLogic = lotteryExternalInput.Where(lot => lot.megaBall == 49);

                            //Your can use other System.Collection items for data as well, such as Lists, to set to external input.  Apply logic here.
                            azureAliqAdapter.SetInput(lotteryExternalInput, lotteryList);

                            //Get the list matching the logic of having MegaBall number 49.
                            var luckyWinnerCombo = azureAliqAdapter.Get(doublyLuckyLogic).ToArray();
                            Assert.Equal(luckyWinnerCombo.Length, 7);
                            Assert.True(luckyWinnerCombo.Any(v => v.megaBall == 49 && v.drawDate == new DateTime(2003, 8, 5) && Array.IndexOf(v.winningNumbers, 7) == 0 && Array.IndexOf(v.winningNumbers, 32) == 1 && Array.IndexOf(v.winningNumbers, 34) == 2 && Array.IndexOf(v.winningNumbers, 38) == 3 && Array.IndexOf(v.winningNumbers, 44) == 4));
                            Assert.True(luckyWinnerCombo.Any(v => v.megaBall == 49 && v.drawDate == new DateTime(2003, 9, 30) && Array.IndexOf(v.winningNumbers, 2) == 0 && Array.IndexOf(v.winningNumbers, 26) == 1 && Array.IndexOf(v.winningNumbers, 37) == 2 && Array.IndexOf(v.winningNumbers, 40) == 3 && Array.IndexOf(v.winningNumbers, 46) == 4));
                            Assert.True(luckyWinnerCombo.Any(v => v.megaBall == 49 && v.drawDate == new DateTime(2003, 12, 30) && Array.IndexOf(v.winningNumbers, 12) == 0 && Array.IndexOf(v.winningNumbers, 18) == 1 && Array.IndexOf(v.winningNumbers, 21) == 2 && Array.IndexOf(v.winningNumbers, 32) == 3 && Array.IndexOf(v.winningNumbers, 46) == 4));
                            Assert.True(luckyWinnerCombo.Any(v => v.megaBall == 49 && v.drawDate == new DateTime(2004, 3, 9) && Array.IndexOf(v.winningNumbers, 16) == 0 && Array.IndexOf(v.winningNumbers, 23) == 1 && Array.IndexOf(v.winningNumbers, 29) == 2 && Array.IndexOf(v.winningNumbers, 36) == 3 && Array.IndexOf(v.winningNumbers, 51) == 4));
                            Assert.True(luckyWinnerCombo.Any(v => v.megaBall == 49 && v.drawDate == new DateTime(2004, 4, 6) && Array.IndexOf(v.winningNumbers, 8) == 0 && Array.IndexOf(v.winningNumbers, 17) == 1 && Array.IndexOf(v.winningNumbers, 29) == 2 && Array.IndexOf(v.winningNumbers, 32) == 3 && Array.IndexOf(v.winningNumbers, 39) == 4));
                            Assert.True(luckyWinnerCombo.Any(v => v.megaBall == 49 && v.drawDate == new DateTime(2004, 4, 20) && Array.IndexOf(v.winningNumbers, 12) == 0 && Array.IndexOf(v.winningNumbers, 22) == 1 && Array.IndexOf(v.winningNumbers, 37) == 2 && Array.IndexOf(v.winningNumbers, 46) == 3 && Array.IndexOf(v.winningNumbers, 48) == 4));
                            Assert.True(luckyWinnerCombo.Any(v => v.megaBall == 49 && v.drawDate == new DateTime(2004, 9, 14) && Array.IndexOf(v.winningNumbers, 12) == 0 && Array.IndexOf(v.winningNumbers, 35) == 1 && Array.IndexOf(v.winningNumbers, 37) == 2 && Array.IndexOf(v.winningNumbers, 38) == 3 && Array.IndexOf(v.winningNumbers, 50) == 4));

                            //Get the 5 oldest entries, since more recent entries will be updated live.  Set logic here accordinly.
                            var oldestDrawLogic = lotteryExternalInput.Where(lot => lot.drawDate < new DateTime(2002, 6, 1));

                            //Apply logic to dataset.
                            var oldestEntries = azureAliqAdapter.Get(oldestDrawLogic).ToArray();

                            Assert.Equal(oldestEntries.Length, 5);
                            Assert.True(oldestEntries.Any(v => v.megaBall == 30 && v.drawDate == new DateTime(2002, 5, 17) && Array.IndexOf(v.winningNumbers, 15) == 0 && Array.IndexOf(v.winningNumbers, 18) == 1 && Array.IndexOf(v.winningNumbers, 25) == 2 && Array.IndexOf(v.winningNumbers, 33) == 3 && Array.IndexOf(v.winningNumbers, 47) == 4));
                            Assert.True(oldestEntries.Any(v => v.megaBall == 9 && v.drawDate == new DateTime(2002, 5, 21) && Array.IndexOf(v.winningNumbers, 4) == 0 && Array.IndexOf(v.winningNumbers, 28) == 1 && Array.IndexOf(v.winningNumbers, 39) == 2 && Array.IndexOf(v.winningNumbers, 41) == 3 && Array.IndexOf(v.winningNumbers, 44) == 4));
                            Assert.True(oldestEntries.Any(v => v.megaBall == 36 && v.drawDate == new DateTime(2002, 5, 24) && Array.IndexOf(v.winningNumbers, 2) == 0 && Array.IndexOf(v.winningNumbers, 4) == 1 && Array.IndexOf(v.winningNumbers, 32) == 2 && Array.IndexOf(v.winningNumbers, 44) == 3 && Array.IndexOf(v.winningNumbers, 52) == 4));
                            Assert.True(oldestEntries.Any(v => v.megaBall == 24 && v.drawDate == new DateTime(2002, 5, 28) && Array.IndexOf(v.winningNumbers, 6) == 0 && Array.IndexOf(v.winningNumbers, 21) == 1 && Array.IndexOf(v.winningNumbers, 22) == 2 && Array.IndexOf(v.winningNumbers, 29) == 3 && Array.IndexOf(v.winningNumbers, 32) == 4));
                            Assert.True(oldestEntries.Any(v => v.megaBall == 47 && v.drawDate == new DateTime(2002, 5, 31) && Array.IndexOf(v.winningNumbers, 12) == 0 && Array.IndexOf(v.winningNumbers, 28) == 1 && Array.IndexOf(v.winningNumbers, 45) == 2 && Array.IndexOf(v.winningNumbers, 46) == 3 && Array.IndexOf(v.winningNumbers, 52) == 4));
                        }
                        else
                        {
                            Assert.False(true);
                        }
                    }
        }