private static ZumoTest CreateTypedApiTest(Random seedGenerator, TypedTestType testType)
        {
            string testName = "Typed overload - " + testType;
            return new ZumoTest(testName, async delegate(ZumoTest test)
            {
                var client = ZumoTestGlobals.Instance.Client;
                var apiName = MovieFinderApiName;
                var testResult = true;
                for (int i = 0; i < 10; i++)
                {
                    int seed = seedGenerator.Next();
                    test.AddLog("Test with seed = {0}", seed);
                    Random rndGen = new Random(seed);

                    Movie[] expectedResult = null;
                    AllMovies actualResult = null;
                    Movie inputTemplate = ZumoQueryTestData.AllMovies[rndGen.Next(ZumoQueryTestData.AllMovies.Length)];
                    test.AddLog("Using movie '{0}' as template", inputTemplate.Title);
                    string apiUrl;
                    switch (testType)
                    {
                        case TypedTestType.GetByTitle:
                            apiUrl = apiName + "/title/" + inputTemplate.Title;
                            expectedResult = new Movie[] { inputTemplate };
                            actualResult = await client.InvokeApiAsync<AllMovies>(apiUrl, HttpMethod.Get, null);
                            break;
                        case TypedTestType.GetByDate:
                            var releaseDate = inputTemplate.ReleaseDate;
                            apiUrl = apiName + "/date/" + releaseDate.Year + "/" + releaseDate.Month + "/" + releaseDate.Day;
                            expectedResult = ZumoQueryTestData.AllMovies.Where(m => m.ReleaseDate == releaseDate).ToArray();
                            actualResult = await client.InvokeApiAsync<AllMovies>(apiUrl, HttpMethod.Get, null);
                            break;
                        case TypedTestType.PostByDuration:
                        case TypedTestType.PostByYear:
                            string orderBy = null;
                            switch (rndGen.Next(3))
                            {
                                case 0:
                                    orderBy = null;
                                    break;
                                case 1:
                                    orderBy = "id";
                                    break;
                                case 2:
                                    orderBy = "Title";
                                    break;
                            }

                            Dictionary<string, string> queryParams = orderBy == null ?
                                null :
                                new Dictionary<string, string> { { "orderBy", orderBy } };

                            Func<Movie, bool> predicate;
                            if (testType == TypedTestType.PostByYear)
                            {
                                predicate = m => m.Year == inputTemplate.Year;
                                apiUrl = apiName + "/moviesOnSameYear";
                            }
                            else
                            {
                                predicate = m => m.Duration == inputTemplate.Duration;
                                apiUrl = apiName + "/moviesWithSameDuration";
                            }

                            if (queryParams == null)
                            {
                                actualResult = await client.InvokeApiAsync<Movie, AllMovies>(apiUrl, inputTemplate);
                            }
                            else
                            {
                                actualResult = await client.InvokeApiAsync<Movie, AllMovies>(apiUrl, inputTemplate, HttpMethod.Post, queryParams);
                            }

                            expectedResult = ZumoQueryTestData.AllMovies.Where(predicate).ToArray();
                            if (orderBy == null || orderBy == "Title")
                            {
                                Array.Sort(expectedResult, (m1, m2) => m1.Title.CompareTo(m2.Title));
                            }

                            break;
                        default:
                            throw new ArgumentException("Invalid test type: " + testType);
                    }

                    test.AddLog("  - Sent request to {0}", apiUrl);
                    List<string> errors = new List<string>();
                    if (Util.CompareArrays(expectedResult, actualResult.Movies, errors))
                    {
                        test.AddLog("  - Result is expected");
                    }
                    else
                    {
                        foreach (var error in errors)
                        {
                            test.AddLog("  - {0}", error);
                        }

                        test.AddLog("Expected: {0}", string.Join(", ", expectedResult.Select(m => m.Title)));
                        test.AddLog("Actual: {0}", string.Join(", ", actualResult.Movies.Select(m => m.Title)));
                        testResult = false;
                        break;
                    }
                }

                return testResult;
            });
        }
示例#2
0
        private async Task CreateTypedApiTest(Random seedGenerator, TypedTestType testType)
        {
            Log("### Typed overload - {0}", testType);

            var client  = GetClient();
            var apiName = MovieFinderApiName;

            for (int i = 0; i < 10; i++)
            {
                int seed = seedGenerator.Next();
                Log("Test with seed = {0}", seed);
                Random rndGen = new Random(seed);

                Movie[]   expectedResult = null;
                AllMovies actualResult   = null;
                Movie     inputTemplate;

                // TODO: BUG #2132434: .NET runtime should allow URI's that end with a dot
                while (true)
                {
                    inputTemplate = QueryTestData.TestMovies()[rndGen.Next(QueryTestData.TestMovies().Length)];
                    if (testType == TypedTestType.GetByTitle && inputTemplate.Title.EndsWith("."))
                    {
                        // The .NET backend barfs and returns 404 if the URI ends with a dot, so let's get another movie
                        continue;
                    }

                    if (testType == TypedTestType.GetByTitle && inputTemplate.Title.EndsWith("?"))
                    {
                        // The .NET & Node backend do not return anything if the URI ends with a '?' so let's get another movie
                        continue;
                    }
                    break;
                }

                Log("Using movie '{0}' as template", inputTemplate.Title);
                string apiUrl;
                switch (testType)
                {
                case TypedTestType.GetByTitle:
                    apiUrl         = apiName + "/title/" + inputTemplate.Title;
                    expectedResult = new Movie[] { inputTemplate };
                    actualResult   = await client.InvokeApiAsync <AllMovies>(apiUrl, HttpMethod.Get, null);

                    break;

                case TypedTestType.GetByDate:
                    var releaseDate = inputTemplate.ReleaseDate;
                    apiUrl         = apiName + "/date/" + releaseDate.Year + "/" + releaseDate.Month + "/" + releaseDate.Day;
                    expectedResult = QueryTestData.TestMovies().Where(m => m.ReleaseDate == releaseDate).ToArray();
                    actualResult   = await client.InvokeApiAsync <AllMovies>(apiUrl, HttpMethod.Get, null);

                    break;

                case TypedTestType.PostByDuration:
                case TypedTestType.PostByYear:
                    string orderBy = null;
                    switch (rndGen.Next(3))
                    {
                    case 0:
                        orderBy = null;
                        break;

                    case 1:
                        orderBy = "id";
                        break;

                    case 2:
                        orderBy = "Title";
                        break;
                    }

                    Dictionary <string, string> queryParams = orderBy == null ?
                                                              null :
                                                              new Dictionary <string, string> {
                        { "orderBy", orderBy }
                    };

                    Func <Movie, bool> predicate;
                    if (testType == TypedTestType.PostByYear)
                    {
                        predicate = m => m.Year == inputTemplate.Year;
                        apiUrl    = apiName + "/moviesOnSameYear";
                    }
                    else
                    {
                        predicate = m => m.Duration == inputTemplate.Duration;
                        apiUrl    = apiName + "/moviesWithSameDuration";
                    }

                    if (queryParams == null)
                    {
                        actualResult = await client.InvokeApiAsync <Movie, AllMovies>(apiUrl, inputTemplate);
                    }
                    else
                    {
                        actualResult = await client.InvokeApiAsync <Movie, AllMovies>(apiUrl, inputTemplate, HttpMethod.Post, queryParams);
                    }

                    expectedResult = QueryTestData.TestMovies().Where(predicate).ToArray();
                    if (orderBy == null || orderBy == "Title")
                    {
                        Array.Sort(expectedResult, (m1, m2) => m1.Title.CompareTo(m2.Title));
                    }

                    break;

                default:
                    throw new ArgumentException("Invalid test type: " + testType);
                }

                Log("  - Sent request to {0}", apiUrl);
                List <string> errors = new List <string>();
                Assert.AreEquivalent(expectedResult, actualResult.Movies);
            }
        }
        private async Task CreateTypedApiTest(Random seedGenerator, TypedTestType testType)
        {
            Log("### Typed overload - {0}", testType);

            var client = GetClient();
            var apiName = MovieFinderApiName;
            for (int i = 0; i < 10; i++)
            {
                int seed = seedGenerator.Next();
                Log("Test with seed = {0}", seed);
                Random rndGen = new Random(seed);

                Movie[] expectedResult = null;
                AllMovies actualResult = null;
                Movie inputTemplate;

                // TODO: BUG #2132434: .NET runtime should allow URI's that end with a dot
                while (true)
                {
                    inputTemplate = QueryTestData.TestMovies()[rndGen.Next(QueryTestData.TestMovies().Length)];
                    if (testType == TypedTestType.GetByTitle && inputTemplate.Title.EndsWith("."))
                    {
                        // The .NET backend barfs and returns 404 if the URI ends with a dot, so let's get another movie
                        continue;
                    }

                    if (testType == TypedTestType.GetByTitle && inputTemplate.Title.EndsWith("?"))
                    {
                        // The .NET & Node backend do not return anything if the URI ends with a '?' so let's get another movie
                        continue;
                    }
                    break;
                }

                Log("Using movie '{0}' as template", inputTemplate.Title);
                string apiUrl;
                switch (testType)
                {
                    case TypedTestType.GetByTitle:
                        apiUrl = apiName + "/title/" + inputTemplate.Title;
                        expectedResult = new Movie[] { inputTemplate };
                        actualResult = await client.InvokeApiAsync<AllMovies>(apiUrl, HttpMethod.Get, null);
                        break;

                    case TypedTestType.GetByDate:
                        var releaseDate = inputTemplate.ReleaseDate;
                        apiUrl = apiName + "/date/" + releaseDate.Year + "/" + releaseDate.Month + "/" + releaseDate.Day;
                        expectedResult = QueryTestData.TestMovies().Where(m => m.ReleaseDate == releaseDate).ToArray();
                        actualResult = await client.InvokeApiAsync<AllMovies>(apiUrl, HttpMethod.Get, null);
                        break;

                    case TypedTestType.PostByDuration:
                    case TypedTestType.PostByYear:
                        string orderBy = null;
                        switch (rndGen.Next(3))
                        {
                            case 0:
                                orderBy = null;
                                break;

                            case 1:
                                orderBy = "id";
                                break;

                            case 2:
                                orderBy = "Title";
                                break;
                        }

                        Dictionary<string, string> queryParams = orderBy == null ?
                            null :
                            new Dictionary<string, string> { { "orderBy", orderBy } };

                        Func<Movie, bool> predicate;
                        if (testType == TypedTestType.PostByYear)
                        {
                            predicate = m => m.Year == inputTemplate.Year;
                            apiUrl = apiName + "/moviesOnSameYear";
                        }
                        else
                        {
                            predicate = m => m.Duration == inputTemplate.Duration;
                            apiUrl = apiName + "/moviesWithSameDuration";
                        }

                        if (queryParams == null)
                        {
                            actualResult = await client.InvokeApiAsync<Movie, AllMovies>(apiUrl, inputTemplate);
                        }
                        else
                        {
                            actualResult = await client.InvokeApiAsync<Movie, AllMovies>(apiUrl, inputTemplate, HttpMethod.Post, queryParams);
                        }

                        expectedResult = QueryTestData.TestMovies().Where(predicate).ToArray();
                        if (orderBy == null || orderBy == "Title")
                        {
                            Array.Sort(expectedResult, (m1, m2) => m1.Title.CompareTo(m2.Title));
                        }

                        break;

                    default:
                        throw new ArgumentException("Invalid test type: " + testType);
                }

                Log("  - Sent request to {0}", apiUrl);
                List<string> errors = new List<string>();
                Assert.AreEquivalent(expectedResult, actualResult.Movies);
            }
        }
        private static ZumoTest CreateTypedApiTest(Random seedGenerator, TypedTestType testType)
        {
            string testName = "Typed overload - " + testType;

            return(new ZumoTest(testName, async delegate(ZumoTest test)
            {
                var client = ZumoTestGlobals.Instance.Client;
                var apiName = MovieFinderApiName;
                var testResult = true;
                for (int i = 0; i < 10; i++)
                {
                    int seed = seedGenerator.Next();
                    test.AddLog("Test with seed = {0}", seed);
                    Random rndGen = new Random(seed);

                    Movie[] expectedResult = null;
                    AllMovies actualResult = null;
                    Movie inputTemplate = ZumoQueryTestData.AllMovies[rndGen.Next(ZumoQueryTestData.AllMovies.Length)];
                    test.AddLog("Using movie '{0}' as template", inputTemplate.Title);
                    string apiUrl;
                    switch (testType)
                    {
                    case TypedTestType.GetByTitle:
                        apiUrl = apiName + "/title/" + inputTemplate.Title;
                        expectedResult = new Movie[] { inputTemplate };
                        actualResult = await client.InvokeApiAsync <AllMovies>(apiUrl, HttpMethod.Get, null);
                        break;

                    case TypedTestType.GetByDate:
                        var releaseDate = inputTemplate.ReleaseDate;
                        apiUrl = apiName + "/date/" + releaseDate.Year + "/" + releaseDate.Month + "/" + releaseDate.Day;
                        expectedResult = ZumoQueryTestData.AllMovies.Where(m => m.ReleaseDate == releaseDate).ToArray();
                        actualResult = await client.InvokeApiAsync <AllMovies>(apiUrl, HttpMethod.Get, null);
                        break;

                    case TypedTestType.PostByDuration:
                    case TypedTestType.PostByYear:
                        string orderBy = null;
                        switch (rndGen.Next(3))
                        {
                        case 0:
                            orderBy = null;
                            break;

                        case 1:
                            orderBy = "id";
                            break;

                        case 2:
                            orderBy = "Title";
                            break;
                        }

                        Dictionary <string, string> queryParams = orderBy == null ?
                                                                  null :
                                                                  new Dictionary <string, string> {
                            { "orderBy", orderBy }
                        };

                        Func <Movie, bool> predicate;
                        if (testType == TypedTestType.PostByYear)
                        {
                            predicate = m => m.Year == inputTemplate.Year;
                            apiUrl = apiName + "/moviesOnSameYear";
                        }
                        else
                        {
                            predicate = m => m.Duration == inputTemplate.Duration;
                            apiUrl = apiName + "/moviesWithSameDuration";
                        }

                        if (queryParams == null)
                        {
                            actualResult = await client.InvokeApiAsync <Movie, AllMovies>(apiUrl, inputTemplate);
                        }
                        else
                        {
                            actualResult = await client.InvokeApiAsync <Movie, AllMovies>(apiUrl, inputTemplate, HttpMethod.Post, queryParams);
                        }

                        expectedResult = ZumoQueryTestData.AllMovies.Where(predicate).ToArray();
                        if (orderBy == null || orderBy == "Title")
                        {
                            Array.Sort(expectedResult, (m1, m2) => m1.Title.CompareTo(m2.Title));
                        }

                        break;

                    default:
                        throw new ArgumentException("Invalid test type: " + testType);
                    }

                    test.AddLog("  - Sent request to {0}", apiUrl);
                    List <string> errors = new List <string>();
                    if (Util.CompareArrays(expectedResult, actualResult.Movies, errors))
                    {
                        test.AddLog("  - Result is expected");
                    }
                    else
                    {
                        foreach (var error in errors)
                        {
                            test.AddLog("  - {0}", error);
                        }

                        test.AddLog("Expected: {0}", string.Join(", ", expectedResult.Select(m => m.Title)));
                        test.AddLog("Actual: {0}", string.Join(", ", actualResult.Movies.Select(m => m.Title)));
                        testResult = false;
                        break;
                    }
                }

                return testResult;
            }));
        }