Skip to content

Commit

Permalink
test(all): use a single instance of IpfsClient
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Jan 17, 2017
1 parent 3ee3ac5 commit 1779869
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 45 deletions.
8 changes: 4 additions & 4 deletions test/AddTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public partial class IpfsClientTest
[TestMethod]
public void AddText()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var result = ipfs.AddTextAsync("hello world").Result;
Assert.AreEqual("Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD", result.Hash);
}
Expand All @@ -26,7 +26,7 @@ public void AddFile()
File.WriteAllText(path, "hello world");
try
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var result = ipfs.AddFileAsync(path).Result;
Assert.AreEqual("Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD", result.Hash);
Assert.AreEqual(0, result.Links.Count());
Expand All @@ -40,7 +40,7 @@ public void AddFile()
[TestMethod]
public void AddDirectory()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var temp = MakeTemp();
try
{
Expand Down Expand Up @@ -69,7 +69,7 @@ public void AddDirectory()
[TestMethod]
public void AddDirectoryRecursive()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var temp = MakeTemp();
try
{
Expand Down
2 changes: 1 addition & 1 deletion test/Commands/BlockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Ipfs.Api
[TestClass]
public class BlockTest
{
IpfsClient ipfs = new IpfsClient();
IpfsClient ipfs = TestFixture.Ipfs;
string hash = "QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ";
byte[] blob = Encoding.UTF8.GetBytes("blorb");

Expand Down
12 changes: 6 additions & 6 deletions test/Commands/ConfigTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ public class ConfigTest
[TestMethod]
public void Get_Entire_Config()
{
IpfsClient ipfs = new IpfsClient();
IpfsClient ipfs = TestFixture.Ipfs;
var config = ipfs.Config.GetAsync().Result;
Assert.AreEqual(apiAddress, config["Addresses"]["API"]);
}

[TestMethod]
public void Get_Scalar_Key_Value()
{
IpfsClient ipfs = new IpfsClient();
IpfsClient ipfs = TestFixture.Ipfs;
var api = ipfs.Config.GetAsync("Addresses.API").Result;
Assert.AreEqual(apiAddress, api);
}

[TestMethod]
public void Get_Object_Key_Value()
{
IpfsClient ipfs = new IpfsClient();
IpfsClient ipfs = TestFixture.Ipfs;
var addresses = ipfs.Config.GetAsync("Addresses").Result;
Assert.AreEqual(apiAddress, addresses["API"]);
Assert.AreEqual(gatewayAddress, addresses["Gateway"]);
Expand All @@ -41,7 +41,7 @@ public void Get_Object_Key_Value()
[TestMethod]
public void Keys_are_Case_Sensitive()
{
IpfsClient ipfs = new IpfsClient();
IpfsClient ipfs = TestFixture.Ipfs;
var api = ipfs.Config.GetAsync("Addresses.API").Result;
Assert.AreEqual(apiAddress, api);

Expand All @@ -53,7 +53,7 @@ public void Set_String_Value()
{
const string key = "foo";
const string value = "foobar";
IpfsClient ipfs = new IpfsClient();
IpfsClient ipfs = TestFixture.Ipfs;
ipfs.Config.SetAsync(key, value).Wait();
Assert.AreEqual(value, ipfs.Config.GetAsync(key).Result);
}
Expand All @@ -63,7 +63,7 @@ public void Set_JSON_Value()
{
const string key = "API.HTTPHeaders.Access-Control-Allow-Origin";
JToken value = JToken.Parse("['http://example.io']");
IpfsClient ipfs = new IpfsClient();
IpfsClient ipfs = TestFixture.Ipfs;
ipfs.Config.SetAsync(key, value).Wait();
Assert.AreEqual("http://example.io", ipfs.Config.GetAsync(key).Result[0]);
}
Expand Down
4 changes: 2 additions & 2 deletions test/Commands/DhtTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DhtTest
[TestMethod]
public async Task FindPeer()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var mars = await ipfs.Dht.FindPeerAsync(marsId);
Assert.AreEqual(marsId, mars.Id);

Expand All @@ -30,7 +30,7 @@ public async Task FindPeer()
[TestMethod]
public async Task FindProviders()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var providers = await ipfs.Dht.FindProvidersAsync(helloWorldID);
Assert.IsFalse(providers.Take(3).Contains(""));
}
Expand Down
6 changes: 3 additions & 3 deletions test/Commands/GenericTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ public class GenericTest
[TestMethod]
public void Local_Node_Info()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var node = ipfs.IdAsync().Result;
Assert.IsInstanceOfType(node, typeof(PeerNode));
}

[TestMethod]
public void Mars_Node_Info()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var node = ipfs.IdAsync(marsId).Result;
Assert.IsInstanceOfType(node, typeof(PeerNode));
}

[TestMethod]
public void Version_Info()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var versions = ipfs.VersionAsync().Result;
Assert.IsNotNull(versions);
Assert.IsTrue(versions.ContainsKey("Version"));
Expand Down
2 changes: 1 addition & 1 deletion test/Commands/ObjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Ipfs.Api
[TestClass]
public class ObjectTest
{
IpfsClient ipfs = new IpfsClient();
IpfsClient ipfs = TestFixture.Ipfs;

[TestMethod]
public async Task New_Template_Null()
Expand Down
6 changes: 3 additions & 3 deletions test/Commands/PinTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PinTest
[TestMethod]
public void List()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var pins = ipfs.Pin.ListAsync().Result;
Assert.IsNotNull(pins);
Assert.IsTrue(pins.Length > 0);
Expand All @@ -24,7 +24,7 @@ public void List()
[TestMethod]
public async Task List_Filtered()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var all = await ipfs.Pin.ListAsync();
var some = await ipfs.Pin.ListAsync(PinMode.Direct);
Assert.AreNotEqual(all.Length, some.Length);
Expand All @@ -33,7 +33,7 @@ public async Task List_Filtered()
[TestMethod]
public async Task Add_Remove()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var result = await ipfs.AddTextAsync("I am pinned");
var id = result.Hash;

Expand Down
8 changes: 4 additions & 4 deletions test/Commands/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class SwarmTest
[TestMethod]
public async Task Addresses()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var swarm = await ipfs.Swarm.AddressesAsync();
foreach (var peer in swarm)
{
Expand All @@ -28,7 +28,7 @@ public async Task Addresses()
[TestMethod]
public async Task Peers()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var peers = await ipfs.Swarm.PeersAsync();
Assert.AreNotEqual(0, peers.Count());
foreach (var peer in peers)
Expand All @@ -42,7 +42,7 @@ public async Task Peers()
[TestMethod]
public async Task Peers_Info()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var peers = await ipfs.Swarm.PeersAsync();
await Task.WhenAll(peers
.Take(10)
Expand All @@ -56,7 +56,7 @@ await Task.WhenAll(peers
[TestMethod]
public async Task Connection()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var peers = await ipfs.Swarm.PeersAsync();

// Sometimes we cannot connect to a specific peer. This
Expand Down
1 change: 1 addition & 0 deletions test/IpfsApiTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<Compile Include="ExceptionAssert.cs" />
<Compile Include="PinnedObjectsTest.cs" />
<Compile Include="PinnedObjectTest.cs" />
<Compile Include="TestFixture.cs" />
<Compile Include="TrustedPeersTest.cs" />
<Compile Include="IpfsClientTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
16 changes: 2 additions & 14 deletions test/IpfsClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,27 @@
namespace Ipfs.Api
{


/// <summary>
///This is a test class for IpfsClientTest and is intended
///to contain all IpfsClientTest Unit Tests
///</summary>
[TestClass]
public partial class IpfsClientTest
{
/// <summary>
/// Fiddler cannot see localhost traffic because .Net bypasses the network stack for localhost/127.0.0.1.
/// By using "127.0.0.1." (note trailing dot) fiddler will receive the traffic and if its not running
/// the localhost will get it!
/// </summary>
[AssemblyInitialize]
public static void AssemblyInit(TestContext context)
{
//IpfsClient.DefaultApiUri = new Uri("http://127.0.0.1.:5001");
}

/// <summary>
/// A test for IpfsClient Constructor
///</summary>
[TestMethod]
public void Can_Create()
{
IpfsClient target = new IpfsClient();
IpfsClient target = TestFixture.Ipfs;
Assert.IsNotNull(target);
}

[TestMethod]
public void Do_Command_Throws_Exception_On_Invalid_Command()
{
IpfsClient target = new IpfsClient();
IpfsClient target = TestFixture.Ipfs;
object unknown;
ExceptionAssert.Throws<Exception>(() => unknown = target.DoCommandAsync("foobar").Result, "Invalid IPFS command");
}
Expand Down
4 changes: 2 additions & 2 deletions test/PinnedObjectsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public partial class IpfsClientTest
[TestMethod]
public void Pin_List()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
Assert.IsNotNull(ipfs.PinnedObjects);
Assert.IsTrue(ipfs.PinnedObjects.Count > 0);
}

[TestMethod]
public void Pin_Add_Remove()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var result = ipfs.AddTextAsync("I am pinned").Result;
var id = result.Hash;

Expand Down
20 changes: 20 additions & 0 deletions test/TestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ipfs.Api
{
public static class TestFixture
{
/// <summary>
/// Fiddler cannot see localhost traffic because .Net bypasses the network stack for localhost/127.0.0.1.
/// By using "127.0.0.1." (note trailing dot) fiddler will receive the traffic and if its not running
/// the localhost will get it!
/// </summary>
//IpfsClient.DefaultApiUri = new Uri("http://127.0.0.1.:5001");

public static IpfsClient Ipfs = new IpfsClient();
}
}
10 changes: 5 additions & 5 deletions test/TrustedPeersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public partial class IpfsClientTest
[TestMethod]
public void Trusted_Peers_List()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
Assert.IsNotNull(ipfs.TrustedPeers);
Assert.IsTrue(ipfs.TrustedPeers.Count > 0);
}

[TestMethod]
public void Trusted_Peers_Add_Remove()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
Assert.IsFalse(ipfs.TrustedPeers.Contains(newTrustedPeer));

ipfs.TrustedPeers.Add(newTrustedPeer);
Expand All @@ -35,14 +35,14 @@ public void Trusted_Peers_Add_Remove()
public void Trusted_Peers_Add_Missing_Peer_ID()
{
var missingPeerId = new MultiAddress("/ip4/25.196.147.100/tcp/4001");
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
ExceptionAssert.Throws<Exception>(() => ipfs.TrustedPeers.Add(missingPeerId), "invalid ipfs address");
}

[TestMethod]
public void Trusted_Peers_Clear()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var original = ipfs.TrustedPeers.ToArray();

ipfs.TrustedPeers.Clear();
Expand All @@ -55,7 +55,7 @@ public void Trusted_Peers_Clear()
[TestMethod]
public void Trusted_Peers_Add_Default_Nodes()
{
var ipfs = new IpfsClient();
var ipfs = TestFixture.Ipfs;
var original = ipfs.TrustedPeers.ToArray();

ipfs.TrustedPeers.Clear();
Expand Down

0 comments on commit 1779869

Please sign in to comment.