Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request tokens endpoint #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions CoverMyDotNet.Samples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,48 @@ public static void MultipleRequests(Client client)
var requests = client.GetRequests(tokens.ToArray());
foreach(var v in requests.Requests)
{
Console.WriteLine("Patient: {0} {1}", v.Patient.FirstName, v.Patient.LastName);
Console.WriteLine("Token: {0} {1}", v.Id, v.Tokens.First().Id);
}
}

public static void DrugSearch(Client client)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are your thoughts on combining the drug-related examples into one class, and ditto for form ones? Then perhaps extracting each into their own files so that Program.cs just has the Main class in it?? I haven't done any .NET programming in awhile, but IIRC correctly that's a common practice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be a good idea. I was mainly using the CoverMyDotNet.Samples to make sure the client class was working correctly with the api. Ill make those changes though.

{
var drugResults = client.SearchDrugs("ambien");
Console.WriteLine(drugResults.Drugs.Count.ToString() + " Results");
foreach(var v in drugResults.Drugs)
Console.WriteLine(v.FullName + " " + v.Id);
}

public static void GetDrug(Client client)
{
var drug = client.GetDrug("018058");
Console.WriteLine(drug.FullName + " comes in the form of " + drug.DosageForm);
}

public static void GetForm(Client client)
{
var form = client.GetForm("medco_general_pa_form_21039");
Console.WriteLine(form.Id + " " + form.Description);
}

public static void SearchForms(Client client)
{
var forms = client.SearchForms("064691", "OH", "bcbs");
Console.WriteLine(forms.Forms.Count + " Results");
Console.WriteLine("First form has a score of " + forms.Forms.First().Score);
}

public static void Main (string[] args)
{
string apiId = ConfigurationManager.AppSettings ["ApiId"];
string secret = ConfigurationManager.AppSettings ["Secret"];
string host = ConfigurationManager.AppSettings ["Host"];
var client = new Client (apiId, secret);
MultipleRequests(client);
//MultipleRequests(client);
DrugSearch(client);
GetDrug(client);
GetForm(client);
SearchForms(client);
}
}
}
234 changes: 231 additions & 3 deletions CoverMyDotNet.Tests/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,15 @@ public void Should_Client_Get_Requests_Return_Multiple()
Assert.AreEqual(resp.Requests.Count, 5);
}
}
[Test]
public void Should_Client_Delete_Post_Correct_Data()
{
string id = Guid.NewGuid().ToString();
string token = Guid.NewGuid().ToString();
string requestContent = "";
var requestHandlers = new List<MockHttpHandler>()
{
new MockHttpHandler("/requests/", "DELETE", (req, rsp, prm) =>
new MockHttpHandler("/requests/" + id, "DELETE", (req, rsp, prm) =>
{
requestContent = req.Content();
})
Expand All @@ -204,6 +205,233 @@ public void Should_Client_Delete_Post_Correct_Data()
Assert.GreaterOrEqual(requestContent.IndexOf("Steve"), 0);
}
}
}
}

[Test]
public void Should_Client_Search_Drug_Use_Correct_Data()
{
string drug_name = Guid.NewGuid().ToString();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit nitty, but should the drug name match the drug(s) that is in the fixture?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I moved removed this test from this branch and added it to the form and drug search PR.

Dictionary<string, string> requestContent = new Dictionary<string, string>();
var requestHandlers = new List<MockHttpHandler>()
{
new MockHttpHandler("/drugs", "GET", (req, rsp, prm) =>
{
requestContent = prm;
})
};
using(new MockServer(DEFAULT_MOCK_PORT, requestHandlers))
{
_client.SearchDrugs(drug_name);
Assert.That(requestContent.Any(p => p.Key == "q"));
Assert.That(requestContent.Any(p => p.Value == drug_name));
}
}

[Test]
public void Should_Client_Search_Drug_Parse_Data()
{
string drug_name = Guid.NewGuid().ToString();
Dictionary<string, string> requestContent = new Dictionary<string, string>();
var requestHandlers = new List<MockHttpHandler>()
{
new MockHttpHandler("/drugs", "GET", (req, rsp, prm) =>
{
return File.ReadAllText("Fixtures/Drugs.json");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find this (or other) fixture files. Was in included in another PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice catch. I didn't push any of the fixtures because they were in the Debug folder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay I added them in this commit ec41011

})
};
using(new MockServer(DEFAULT_MOCK_PORT, requestHandlers, (req, rsp, prm) => rsp.Header("Content-Type", "application/json")))
{
var drugs = _client.SearchDrugs(drug_name);
Assert.AreEqual(drugs.Drugs.Count, 3);
Assert.AreEqual(drugs.Drugs.First().FullName, "Boniva 150MG tablets");
Assert.AreEqual(drugs.Drugs.First().Strength, "150");
Assert.AreEqual(drugs.Drugs[1].Href, "https://staging.api.covermymeds.com/drugs/094563");
Assert.AreEqual(drugs.Drugs[2].GPI, "30042048102030");
}
}


[Test]
public void Should_Client_Get_Drug_Use_Correct_Data()
{
string drug_id = Guid.NewGuid().ToString();
bool called = false;
var requestHandlers = new List<MockHttpHandler>()
{
new MockHttpHandler(string.Format("/drugs/{0}", drug_id), "GET", (req, rsp, prm) =>
{
called = true;
})
};
using(new MockServer(DEFAULT_MOCK_PORT, requestHandlers))
{
_client.GetDrug(drug_id);
Assert.That(called);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know anything about MockHttpHandler, only conceptually what it is doing here, but I'd expect it to have some kind of method to assert that a request was made. (In lieu of using a local variable for this.) Is there anything like that in the usage documentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so as well, but because it uses lambdas for handling the responses, NUnit does not see them as tests, so the asserts don't work

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are spoiled by rspec/webmock

}
}

[Test]
public void Should_Client_Get_Form_Parse_Data()
{
string form_id = Guid.NewGuid().ToString();
var requestHandlers = new List<MockHttpHandler>()
{
new MockHttpHandler(string.Format("/forms/{0}", form_id), "GET", (req, rsp, prm) =>
{
return File.ReadAllText("Fixtures/Form.json");
})
};
using(new MockServer(DEFAULT_MOCK_PORT, requestHandlers, (req, rsp, prm)=> rsp.Header("Content-Type", "application/json")))
{
var form = _client.GetForm(form_id);
Assert.AreEqual(form.Id, 4);
Assert.AreEqual(form.Name, "humana_tracleer");
Assert.AreEqual(form.IsEPA, false);
Assert.AreEqual(form.ContactFax, "(877) 486-2621");
}
}

[Test]
public void Should_Client_Get_Form_Use_Correct_URL()
{
string form_id = Guid.NewGuid().ToString();
bool called = false;
var requestHandlers = new List<MockHttpHandler>()
{
new MockHttpHandler(string.Format("/forms/{0}", form_id), "GET", (req, rsp, prm) =>
{
called = true;
})
};
using(new MockServer(DEFAULT_MOCK_PORT, requestHandlers, (req, rsp, prm) => rsp.Header("Content-Type", "application/json")))
{
_client.GetForm(form_id);
Assert.That(called);
}
}

[Test]
public void Should_Client_Search_Forms_Parse_Data()
{
var requestHandlers = new List<MockHttpHandler>()
{
new MockHttpHandler("/forms/", "GET", (req, rsp, prm) =>
{
return File.ReadAllText("Fixtures/Forms.Json");
})
};
using(new MockServer(DEFAULT_MOCK_PORT, requestHandlers, (req, rsp, prm) => rsp.Header("Content-Type", "application/json")))
{
var forms = _client.SearchForms("1234", "OH", "12345");
Assert.AreEqual(forms.Forms.Count, 5);
Assert.AreEqual(forms.Forms.First().Id, 15257);
Assert.AreEqual(forms.Forms[2].Name, "express_scripts_medicare_part_d_quantity_limit_exceptions");
Assert.AreEqual(forms.Forms[1].Directions, "Prior Authorization of Benefits (PAB) Form for Multi-Source Brand Medications");
Assert.AreEqual(forms.Forms.Last().RequestFormId, "anthem_general_3876");
}
}

[Test]
public void Should_Client_Search_Forms_Use_Correct_Data()
{
var requestContent = new Dictionary<string, string>();
var requestHandlers = new List<MockHttpHandler>()
{
new MockHttpHandler("/forms", "GET", (req, rsp, prm) =>
{
requestContent = prm;
})
};
using(new MockServer(DEFAULT_MOCK_PORT, requestHandlers, (req, rsp, prm) => rsp.Header("Content-Type", "application/json")))
{
_client.SearchForms("1234", "OH", "12345");
Assert.AreEqual(requestContent["drug_id"], "1234");
Assert.AreEqual(requestContent["state"], "OH");
Assert.AreEqual(requestContent["q"], "12345");
}
}

[Test]
public void Should_Client_Search_Forms_Use_Correct_Data2()
{
var requestContent = new Dictionary<string, string>();
var requestHandlers = new List<MockHttpHandler>()
{
new MockHttpHandler("/forms", "GET", (req, rsp, prm) =>
{
requestContent = prm;
})
};
using(new MockServer(DEFAULT_MOCK_PORT, requestHandlers, (req, rsp, prm) => rsp.Header("Content-Type", "application/json")))
{
_client.SearchForms("1234", "OH", "30", "binsample", "223142", "group-567");
Assert.AreEqual(requestContent["drug_id"], "1234");
Assert.AreEqual(requestContent["state"], "OH");
Assert.AreEqual(requestContent["threshold"], "30");
Assert.AreEqual(requestContent["bin"], "binsample");
Assert.AreEqual(requestContent["pcn"], "223142");
Assert.AreEqual(requestContent["group_id"], "group-567");
}
}

[Test]
public void Should_Client_Post_Tokens()
{
var tokenIds = new List<string>();
string requestContent = "";
for(int i = 0;i<5;i++)
tokenIds.Add(Guid.NewGuid().ToString());
var requestHandlers = new List<MockHttpHandler>()
{
new MockHttpHandler("/requests/tokens", "POST", (req, rsp, prm) =>
{
requestContent = req.Content();
})
};
using(new MockServer(DEFAULT_MOCK_PORT, requestHandlers))
{
_client.PostTokens(tokenIds.ToArray());
for(int i = 0;i<5;i++)
Assert.GreaterOrEqual(requestContent.IndexOf(tokenIds[i]), 0);
}
}

[Test]
public void Should_Client_Parse_Token_Data()
{
var requestHandlers = new List<MockHttpHandler>()
{
new MockHttpHandler("/requests/tokens", "POST", (req, rsp, prm) =>
{
return File.ReadAllText("Fixtures/PostToken.json");
})
};
using(new MockServer(DEFAULT_MOCK_PORT, requestHandlers, (req, rsp, prm) => rsp.Header("Content-Type", "application/json")))
{
var resp = _client.PostTokens(new string []{"blah", "test", "Test2"});
Assert.AreEqual(resp.Tokens.Count, 1);
Assert.AreEqual(resp.Tokens.First().Id, "nhe44fu4g22upqqgstea");
Assert.AreEqual(resp.Tokens.First().RequestId, "NT4HJ4");

}
}

[Test]
public void Should_Client_Delete_Token()
{
string _tokenId = Guid.NewGuid().ToString();
bool called = false;
var requestHandlers = new List<MockHttpHandler>()
{
new MockHttpHandler("/requests/tokens/" + _tokenId, "DELETE", (req, rsp, prm) =>
{
called = true;
})
};
using(new MockServer(DEFAULT_MOCK_PORT, requestHandlers))
{
_client.DeleteToken(_tokenId);
Assert.That(called);
}
}
}
}
58 changes: 58 additions & 0 deletions CoverMyDotNet.Tests/GetDrugTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Net.Http;
using System;
using NUnit.Framework;
using Moq;
using RestSharp;
using System.Linq;
using CoverMyDotNet.Requests;

namespace CoverMyDotNet.Tests
{
[TestFixture]
public class GetDrugTests
{
private string _apiId;
private string _drugId;
private GetDrug _getDrug;

[SetUp]
public void Setup()
{
_apiId = Guid.NewGuid().ToString();
_drugId = Guid.NewGuid().ToString();
_getDrug = new GetDrug(_apiId, _drugId);
}

[Test]
public void Should_Use_Correct_Method()
{
Assert.AreEqual(_getDrug.Method, Method.GET);
}

[Test]
public void Should_Use_Correct_Resource()
{
Assert.AreEqual(_getDrug.Resource, string.Format("drugs/{0}", _drugId));
}

[Test]
public void Should_Use_Correct_RootElement()
{
Assert.AreEqual(_getDrug.RootElement, "drug");
}

[Test]
public void Should_Have_Version_Parameter()
{
Assert.That(_getDrug.Parameters.Any(p => p.Name == "v" && p.Value.ToString() == "1"));
}

[Test]
public void Should_Have_Authorization_Parameter()
{
Assert.That(_getDrug.Parameters.Any(p => p.Name == "Authorization" &&
p.Value.ToString() == string.Format("Bearer {0}+x-no-pass", _apiId)));
}

}
}
Loading