Commits

lfoust committed d9f97f3

playing around with test ideas (random generating parameters and offline testing)

Comments (0)

Files changed (9)

trunk/data/answers-comments-vnZLCJqAayGM1Qt86HvmXgxSj8Y=.txt

-https://api.stackexchange.com/2.0/answers/12/comments?order=desc&sort=creation&site=stackoverflow
+https://api.stackexchange.com/2.0/answers/12/comments/?order=desc&sort=creation&site=stackoverflow
 {
   "items": [
     {

trunk/source/Stacky/Helpers/Filters.cs

+namespace Stacky
+{
+    public static class Filters
+    {
+        public static readonly string Default = "default";
+        public static readonly string WithBody = "withbody";
+        public static readonly string None = "none";
+        public static readonly string Total = "total";
+    }
+}

trunk/source/Stacky/Helpers/UrlHelper.cs

         {
             Require.NotNullOrEmpty(method, "method");
 
-            string urlBase = String.Format(CultureInfo.CurrentCulture, "http://api.stackexchange.com/{0}/{1}/", version, method);
+            string urlBase = String.Format(CultureInfo.CurrentCulture, "https://api.stackexchange.com/{0}/{1}/", version, method);
             if (urlParameters != null)
             {
                 foreach (string param in urlParameters)

trunk/source/Stacky/Stacky.csproj

     <Compile Include="ErrorResponse.cs" />
     <Compile Include="Helpers\EnumHelper.cs" />
     <Compile Include="Helpers\Execute.cs" />
+    <Compile Include="Helpers\Filters.cs" />
     <Compile Include="Helpers\Paging\PageEnumerator.cs" />
     <Compile Include="Helpers\Paging\PagerInfo.cs" />
     <Compile Include="Helpers\Paging\StackyEnumerable.cs" />

trunk/source/Stacky/StackyClient/StackyClient.cs

         public Response<T> ParseResponse<T>(HttpResponse httpResponse)
             where T : new()
         {
+            if (httpResponse == null)
+                throw new ArgumentNullException("httpResponse");
             if (httpResponse.Error != null && String.IsNullOrEmpty(httpResponse.Body))
                 throw new ApiException("Error retrieving url", null, httpResponse.Error, httpResponse.Url);
 

trunk/source/Tests/Stacky.IntegrationTests/AnswerTests.cs

 using System.Collections.Generic;
 using System.Linq;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System.Linq.Expressions;
 
 namespace Stacky.IntegrationTests
 {
     public class AnswerTests : IntegrationTest
     {
         [TestMethod]
+        public void Answer_GetAnswers_NoParams()
+        {
+            var answers = Client.GetAnswers();
+            Assert.IsNotNull(answers);
+        }
+
+        protected T? Enum<T>()
+            where T : struct
+        {
+            return GenerateValue.NullableEnum<T>();
+        }
+
+        protected SortDirection? SortDirection
+        {
+            get { return GenerateValue.NullableEnum<SortDirection>(); }
+        }
+
+        protected int? Page
+        {
+            get
+            {
+                return GenerateValue.NullableInt32(1, 250);
+            }
+        }
+
+        protected int? PageSize
+        {
+            get
+            {
+                return GenerateValue.NullableInt32(1, 250);
+            }
+        }
+
+        protected DateTime? FromDate
+        {
+            get { return GenerateValue.NullableDateTime(); }
+        }
+
+        protected DateTime? ToDate
+        {
+            get { return GenerateValue.NullableDateTime(); }
+        }
+
+        protected DateTime? DateMin
+        {
+            get { return GenerateValue.NullableDateTime(); }
+        }
+
+        protected DateTime? DateMax
+        {
+            get { return GenerateValue.NullableDateTime(); }
+        }
+
+        protected string Filter
+        {
+            get { return null; }
+        }
+
+        [TestMethod]
+        public void Answer_GetAnswers_RandomParams()
+        {
+            var answers = Client.GetAnswers(Enum<AnswerSort>(), SortDirection, Page, PageSize, FromDate, ToDate, DateMin, DateMax, Filter);
+            Assert.IsNotNull(answers);
+        }
+
+        [TestMethod]
         public void Answer_GetQuestionAnswers()
         {
             var answers = Client.GetQuestionAnswers(31415);
             Assert.IsNotNull(answers);
         }
 
-        [TestMethod]
-        public void Answer_GetAnswers_NoParams()
-        {
-            var answers = Client.GetAnswers();
-            Assert.IsNotNull(answers);
-        }
+        
     }
 }

trunk/source/Tests/Stacky.IntegrationTests/GenerateValue.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace Stacky
+{
+    public static class GenerateValue
+    {
+        private static Random Random = new Random();
+
+        private static bool IsNull
+        {
+            get { return Random.Next(2) == 1; }
+        }
+
+        private static int GetIndex(int max)
+        {
+            return Random.Next(max);
+        }
+
+        public static int? NullableInt32(int min, int max)
+        {
+            if (IsNull)
+            {
+                return null;
+            }
+            return Int32(min, max);
+        }
+
+        public static int Int32(int min, int max)
+        {
+            return Random.Next(min, max);
+        }
+
+        public static DateTime? NullableDateTime()
+        {
+            if (IsNull)
+            {
+                return null;
+            }
+            return DateTime();
+        }
+
+        public static DateTime DateTime()
+        {
+            return UnixDateTime.DateFromUnixTime(UnixDateTime.UnixTimeFromDate(UnixDateTime.UnixEpoch) + (long)Random.Next());
+        }
+
+        public static T Enum<T>()
+            where T : struct
+        {
+            if (!EnumHelper.IsEnum(typeof(T)))
+                throw new ArgumentOutOfRangeException("T", "Type must be enum");
+
+            Array values = System.Enum.GetValues(typeof(T));
+            int index = GetIndex(values.Length);
+            return (T)values.GetValue(index);
+        }
+
+        public static T? NullableEnum<T>()
+            where T : struct
+        {
+            if (IsNull)
+            {
+                return null;
+            }
+            return Enum<T>();
+        }
+    }
+}

trunk/source/Tests/Stacky.IntegrationTests/Stacky.IntegrationTests.csproj

     <Compile Include="ClientTests.cs" />
     <Compile Include="CommentTests.cs" />
     <Compile Include="ErrorTests.cs" />
+    <Compile Include="GenerateValue.cs" />
     <Compile Include="HostSiteTests.cs" />
     <Compile Include="IntegrationTest.cs" />
     <Compile Include="PagerTests.cs" />

trunk/source/Tests/Stacky.UnitTests/UrlHelperTests.cs

 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using System.Collections.Specialized;
 using System.Collections.Generic;
+using System.IO;
+using System.Linq;
 
 namespace Stacky.UnitTests
 {
             Assert.IsTrue(url.ToString().Contains("%23"));
         }
     }
+
+    [TestClass]
+    public class OfflineUrlClientTests
+    {
+        [TestMethod]
+        public void ParamatersMapped()
+        {
+            string url = "https://api.stackexchange.com/2.0/answers/{id}/comments";
+            var newUrl = OfflineUrlClient.MapString(url, new { id = 10 });
+            Assert.AreEqual("https://api.stackexchange.com/2.0/answers/10/comments", newUrl);
+        }
+
+        [TestMethod]
+        public void ResponsesLoadedCorrectly()
+        {
+            IUrlClient urlClient = new OfflineUrlClient();
+            StackyClient client = new StackyClient("2.0", Sites.StackOverflow, urlClient, new JsonProtocol());
+            var comments = client.GetAnswerComments(12, AnswerSort.Creation, SortDirection.Descending);
+            Assert.IsNotNull(comments);
+        }
+    }
+
+    public class OfflineUrlClient : IUrlClient
+    {
+        static List<HttpResponse> responses = new List<HttpResponse>();
+        static readonly string InputDirectory = @"C:\Code\Stacky\trunk\data";
+        static OfflineUrlClient()
+        {
+            foreach (string path in Directory.GetFiles(InputDirectory, "*.txt"))
+            {
+                string[] lines = File.ReadAllLines(path);
+                string url = lines[0];
+                responses.Add(new HttpResponse
+                {
+                    Url = new Uri(url),
+                    Body = String.Join(Environment.NewLine, lines.Skip(1).Select(l => l))
+                });
+            }
+        }
+
+        public static string MapString(string source, object values)
+        {
+            var dictionary = UrlHelper.ObjectToDictionary(values);
+            foreach (var pair in dictionary)
+            {
+                source = source.Replace("{" + pair.Key + "}", pair.Value);
+            }
+            return source;
+        }
+
+        public HttpResponse MakeRequest(Uri url)
+        {
+            return responses.FirstOrDefault(r => UrlEquals(url, r.Url));
+        }
+
+        public static bool UrlEquals(Uri url1, Uri url2)
+        {
+            if (url1.Scheme != url2.Scheme)
+                return false;
+            if (url1.AbsolutePath != url2.AbsolutePath)
+                return false;
+            if (!QueryStringEquals(url1.Query, url2.Query))
+                return false;
+            return true;
+        }
+
+        public static bool QueryStringEquals(string query1, string query2)
+        {
+            return false;
+        }
+    }
+
 }