Skip to content

Commit 2ed85d0

Browse files
author
Ihtasham
committed
Added tests and interface with some more code improvements.
1 parent 492f0ab commit 2ed85d0

File tree

8 files changed

+242
-18
lines changed

8 files changed

+242
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("devRantDotNet.Tests")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Adey Steel Group")]
12+
[assembly: AssemblyProduct("devRantDotNet.Tests")]
13+
[assembly: AssemblyCopyright("Copyright © Adey Steel Group 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("63a9b244-30c5-4a2c-bf57-2ecb62250acc")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

devRantDotNet.Tests/Tests.cs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
4+
namespace devRantDotNet.Tests
5+
{
6+
[TestClass]
7+
public class Tests
8+
{
9+
devRant dr = new devRant();
10+
11+
[TestMethod]
12+
public void GetRantsAsyncTest()
13+
{
14+
var result = dr.GetRantsAsync(devRant.SortType.algo).Result;
15+
Assert.IsTrue(result.Count == 30);
16+
}
17+
18+
[TestMethod]
19+
public void GetRantAsyncTest()
20+
{
21+
int id = 450227;
22+
var result = dr.GetRantAsync(id).Result;
23+
Assert.AreEqual("px06", result.user_username);
24+
}
25+
26+
[TestMethod]
27+
public void GetUserIdAsyncTest()
28+
{
29+
string username = "px06";
30+
var result = dr.GetUserIdAsync(username).Result;
31+
Assert.AreEqual(result, 428514);
32+
}
33+
34+
[TestMethod]
35+
public void GetProfileAsyncTest()
36+
{
37+
long id = 428514;
38+
var result = dr.GetProfileAsync(id).Result;
39+
Assert.AreEqual("redrails", result.github);
40+
Assert.AreEqual("px06", result.username);
41+
Assert.IsTrue(result.rants.Find(x => x.id == 450227).tags.Contains("c#"));
42+
}
43+
44+
[TestMethod]
45+
public void SearchAsyncTest()
46+
{
47+
string term = "api";
48+
var result = dr.SearchAsync(term).Result;
49+
Assert.IsTrue(result.Count > 0);
50+
}
51+
52+
[TestMethod]
53+
public void GetRandomRantAsyncTest()
54+
{
55+
var result = dr.GetRandomRantAsync().Result;
56+
Assert.IsTrue(result.num_upvotes >= 20);
57+
}
58+
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProjectGuid>{63A9B244-30C5-4A2C-BF57-2ECB62250ACC}</ProjectGuid>
7+
<OutputType>Library</OutputType>
8+
<AppDesignerFolder>Properties</AppDesignerFolder>
9+
<RootNamespace>devRantDotNet.Tests</RootNamespace>
10+
<AssemblyName>devRantDotNet.Tests</AssemblyName>
11+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
14+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
15+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
16+
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
17+
<IsCodedUITest>False</IsCodedUITest>
18+
<TestProjectType>UnitTest</TestProjectType>
19+
</PropertyGroup>
20+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
21+
<DebugSymbols>true</DebugSymbols>
22+
<DebugType>full</DebugType>
23+
<Optimize>false</Optimize>
24+
<OutputPath>bin\Debug\</OutputPath>
25+
<DefineConstants>DEBUG;TRACE</DefineConstants>
26+
<ErrorReport>prompt</ErrorReport>
27+
<WarningLevel>4</WarningLevel>
28+
</PropertyGroup>
29+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<Reference Include="System" />
39+
</ItemGroup>
40+
<Choose>
41+
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
42+
<ItemGroup>
43+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
44+
</ItemGroup>
45+
</When>
46+
<Otherwise>
47+
<ItemGroup>
48+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
49+
</ItemGroup>
50+
</Otherwise>
51+
</Choose>
52+
<ItemGroup>
53+
<Compile Include="Tests.cs" />
54+
<Compile Include="Properties\AssemblyInfo.cs" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<ProjectReference Include="..\devRantDotNet\devRantDotNet.csproj">
58+
<Project>{02bc45a3-f4b7-474a-a279-392d3d3332ea}</Project>
59+
<Name>devRantDotNet</Name>
60+
</ProjectReference>
61+
</ItemGroup>
62+
<Choose>
63+
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
64+
<ItemGroup>
65+
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
66+
<Private>False</Private>
67+
</Reference>
68+
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
69+
<Private>False</Private>
70+
</Reference>
71+
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
72+
<Private>False</Private>
73+
</Reference>
74+
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
75+
<Private>False</Private>
76+
</Reference>
77+
</ItemGroup>
78+
</When>
79+
</Choose>
80+
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
81+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
82+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
83+
Other similar extension points exist, see Microsoft.Common.targets.
84+
<Target Name="BeforeBuild">
85+
</Target>
86+
<Target Name="AfterBuild">
87+
</Target>
88+
-->
89+
</Project>

devRantDotNet.sln

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "devRantDotNet", "devRantDot
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "devRantTests", "devRantTests\devRantTests.csproj", "{95FADFA9-BEE3-4BB2-8A09-5B4A8AD79D60}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "devRantDotNet.Tests", "devRantDotNet.Tests\devRantDotNet.Tests.csproj", "{63A9B244-30C5-4A2C-BF57-2ECB62250ACC}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{95FADFA9-BEE3-4BB2-8A09-5B4A8AD79D60}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{95FADFA9-BEE3-4BB2-8A09-5B4A8AD79D60}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{95FADFA9-BEE3-4BB2-8A09-5B4A8AD79D60}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{63A9B244-30C5-4A2C-BF57-2ECB62250ACC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{63A9B244-30C5-4A2C-BF57-2ECB62250ACC}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{63A9B244-30C5-4A2C-BF57-2ECB62250ACC}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{63A9B244-30C5-4A2C-BF57-2ECB62250ACC}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

devRantDotNet/IdevRant.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using devRantDotNet.Source.Models;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using static devRantDotNet.devRant;
8+
9+
namespace devRantDotNet.Source
10+
{
11+
interface IdevRant
12+
{
13+
14+
Task<List<Rant>> GetRantsAsync(SortType type, int limit, int skip);
15+
16+
Task<Rant> GetRantAsync(int id);
17+
18+
Task<int> GetUserIdAsync(string username);
19+
20+
Task<User> GetProfileAsync(long id);
21+
22+
Task<List<Rant>> SearchAsync(string term);
23+
24+
Task<Rant> GetRandomRantAsync();
25+
26+
}
27+
}

devRantDotNet/Source/Resources/Values.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace devRantDotNet
99
internal static class Values
1010
{
1111
internal const string API = "https://www.devrant.io/api";
12-
internal const string AppId = "?app=3";
12+
internal const string AppId = "app=3";
1313
internal const string UsernameById = API + "/get-user-id";
1414
internal const string AllRants = API + "/devrant/rants";
1515
internal const string SingleRant = API + "/devrant/rants/";

devRantDotNet/Source/devRant.cs

+22-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using devRantDotNet.Source.Models;
1+
using devRantDotNet.Source;
2+
using devRantDotNet.Source.Models;
23
using Newtonsoft.Json;
34
using System;
45
using System.Collections.Generic;
@@ -15,7 +16,7 @@ namespace devRantDotNet
1516
/// <summary>
1617
/// A C# Wrapper for the devRant API
1718
/// </summary>
18-
public class devRant : IDisposable
19+
public class devRant : IDisposable, IdevRant
1920
{
2021

2122
/// <summary>
@@ -38,7 +39,7 @@ public enum SortType
3839
/// </summary>
3940
recent
4041
}
41-
42+
4243
/// <summary>
4344
/// Uses <see cref="HttpWebRequest"/> and <see cref="HttpWebResponse"/> to create requests to the API.
4445
/// Returns the JSON result.
@@ -62,7 +63,7 @@ private async Task<string> MakeRequestAsync(string url)
6263
string t;
6364
var response = (HttpWebResponse)await request.GetResponseAsync();
6465

65-
using(var sr = new StreamReader(response.GetResponseStream()))
66+
using (var sr = new StreamReader(response.GetResponseStream()))
6667
{
6768
t = sr.ReadToEnd();
6869
}
@@ -133,10 +134,12 @@ private Comment JSONToCommentObject(dynamic c)
133134
/// Returns all the rants from the feed at: https://www.devrant.io/feed
134135
/// </summary>
135136
/// <param name="type"> Type of sort e.g. Top, Algo or Recent</param>
137+
/// <param name="limit">The amount of rants to load</param>
138+
/// <param name="skip">Skips n amount of rants, useful for paging</param>
136139
/// <returns>A List of Rants which are iterable</returns>
137-
public async Task<List<Rant>> GetRantsAsync(SortType type)
140+
public async Task<List<Rant>> GetRantsAsync(SortType type, int limit = 30, int skip = 10)
138141
{
139-
var req = await MakeRequestAsync(Values.AllRants+"?sort="+type+"&app=3");
142+
var req = await MakeRequestAsync($"{Values.AllRants}?sort={type}&limit={limit}&skip={skip}&{Values.AppId}");
140143
dynamic results = JsonConvert.DeserializeObject<dynamic>(req);
141144

142145
if (results.success != "true")
@@ -146,7 +149,7 @@ public async Task<List<Rant>> GetRantsAsync(SortType type)
146149

147150
List<Rant> rants = new List<Rant>();
148151

149-
for(int i = 0; i<results.rants.Count; i++)
152+
for (int i = 0; i < results.rants.Count; i++)
150153
{
151154
var r = results.rants[i];
152155
Rant rant = JSONToRantObject(r);
@@ -166,7 +169,7 @@ public async Task<Rant> GetRantAsync(int id)
166169
{
167170
try
168171
{
169-
var req = await MakeRequestAsync(Values.SingleRant + id + Values.AppId);
172+
var req = await MakeRequestAsync($"{Values.SingleRant}{id}?{Values.AppId}");
170173
dynamic results = JsonConvert.DeserializeObject<dynamic>(req);
171174
var r = results.rant;
172175

@@ -178,7 +181,8 @@ public async Task<Rant> GetRantAsync(int id)
178181
rant.rant_comments.Add(JSONToCommentObject(current));
179182
}
180183
return rant;
181-
} catch (Exception e)
184+
}
185+
catch (Exception e)
182186
{
183187
return null;
184188
}
@@ -193,7 +197,7 @@ public async Task<int> GetUserIdAsync(string username)
193197
{
194198
try
195199
{
196-
var req = await MakeRequestAsync(Values.UsernameById + "?username=" + username + "&app=3");
200+
var req = await MakeRequestAsync($"{Values.UsernameById}?username={username}&{Values.AppId}");
197201
dynamic results = JsonConvert.DeserializeObject<dynamic>(req);
198202

199203
if (results.success != "true")
@@ -216,10 +220,10 @@ public async Task<User> GetProfileAsync(long id)
216220
{
217221
try
218222
{
219-
var req = await MakeRequestAsync(Values.User + id + Values.AppId);
223+
var req = await MakeRequestAsync($"{Values.User}{id}?{Values.AppId}");
220224
dynamic results = JsonConvert.DeserializeObject<dynamic>(req);
221225
var profile = results.profile;
222-
if(results.success != "true")
226+
if (results.success != "true")
223227
{
224228
throw new Exception("Something went wrong!");
225229
}
@@ -234,7 +238,8 @@ public async Task<User> GetProfileAsync(long id)
234238
user.github = profile.github;
235239
user.website = profile.website;
236240

237-
for (var i = 0; i < profile.content.content.rants.Count; i++){
241+
for (var i = 0; i < profile.content.content.rants.Count; i++)
242+
{
238243
user.rants.Add(JSONToRantObject(profile.content.content.rants[i]));
239244
}
240245

@@ -243,12 +248,12 @@ public async Task<User> GetProfileAsync(long id)
243248
user.upvoted.Add(JSONToRantObject(profile.content.content.upvoted[i]));
244249
}
245250

246-
for(var i = 0; i < profile.content.content.comments.Count; i++)
251+
for (var i = 0; i < profile.content.content.comments.Count; i++)
247252
{
248253
user.comments.Add(JSONToCommentObject(profile.content.content.comments[i]));
249254
}
250255

251-
for(var i = 0; i < profile.content.content.favorites.Count; i++)
256+
for (var i = 0; i < profile.content.content.favorites.Count; i++)
252257
{
253258
user.favorites.Add(JSONToRantObject(profile.content.content.favorites[i]));
254259
}
@@ -278,7 +283,7 @@ public async Task<List<Rant>> SearchAsync(string term)
278283
{
279284
try
280285
{
281-
var req = await MakeRequestAsync(Values.Search + "?term=" + term + "&app=3");
286+
var req = await MakeRequestAsync($"{Values.Search}?term={term}&{Values.AppId}");
282287
dynamic results = JsonConvert.DeserializeObject<dynamic>(req);
283288

284289
if (results.success != "true")
@@ -310,7 +315,7 @@ public async Task<Rant> GetRandomRantAsync()
310315
{
311316
try
312317
{
313-
var req = await MakeRequestAsync(Values.Random + Values.AppId);
318+
var req = await MakeRequestAsync($"{Values.Random}?{Values.AppId}");
314319
dynamic results = JsonConvert.DeserializeObject<dynamic>(req);
315320

316321
return results.success == "true" ? JSONToRantObject(results.rant) : null;

devRantDotNet/devRantDotNet.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<Reference Include="System.Xml" />
4545
</ItemGroup>
4646
<ItemGroup>
47+
<Compile Include="IdevRant.cs" />
4748
<Compile Include="Source\devRant.cs" />
4849
<Compile Include="Properties\AssemblyInfo.cs" />
4950
<Compile Include="Source\Models\Comment.cs" />

0 commit comments

Comments
 (0)