Exemplo n.º 1
0
 public StringIdMovie(string id, Movie movie)
 {
     this.Id = id;
     this.Title = movie.Title;
     this.Duration = movie.Duration;
     this.MPAARating = movie.MPAARating;
     this.ReleaseDate = movie.ReleaseDate;
     this.BestPictureWinner = movie.BestPictureWinner;
     this.Year = movie.Year;
 }
        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;
            });
        }