Skip to content

Commit

Permalink
MAS-129 Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
d-lan1 committed Jan 14, 2020
1 parent 0a12a63 commit 697face
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lambda/MAS.Tests/Infrastructure/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using System;
using System.Net.Http;

namespace MAS.Tests.Infrastructure
{
public class TestBase
public class TestBase : IDisposable
{
protected readonly TestServer _server;
protected readonly HttpClient _client;
Expand All @@ -30,5 +31,10 @@ public TestBase()
_server = new TestServer(builder);
_client = _server.CreateClient();
}

public virtual void Dispose()
{
// Do "global" teardown here; Called after every test method.
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class='evidenceType'><strong>Some evidence type</strong><div class='item'>Some Title<br>Some source<br>Some speciality<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div></div><div class='evidenceType'><strong>Some evidence type 2</strong><div class='item'>Some Title<br>Some source<br>Some speciality 2<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class='evidenceType'><strong>Some evidence type</strong><div class='item'>Some Title<br>Some source<br>Some speciality<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div><div class='item'>Some Title<br>Some source<br>Some speciality<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class='evidenceType'><strong>Some evidence type</strong><div class='item'>Some Title<br>Some source<br>Some speciality<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class='evidenceType'><strong>Some evidence type</strong><div class='item'>Some Title<br>Some source<br>Some speciality | Another speciality<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div></div>
142 changes: 142 additions & 0 deletions lambda/MAS.Tests/UnitTests/DailyEmailTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using MailChimp.Net.Core;
using MailChimp.Net.Interfaces;
using MailChimp.Net.Models;
using MAS.Models;
using MAS.Services;
using MAS.Tests.Infrastructure;
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Source = MAS.Models.Source;

namespace MAS.Tests.UnitTests
{
public class DailyEmailTests : TestBase
{

Mock<ILogger<MailService>> MockLogger;
Mock<IMailChimpManager> MockMailChimpManager;
MailService MailService;

public DailyEmailTests()
{
//Arrange
MockLogger = new Mock<ILogger<MailService>>();

MockMailChimpManager = new Mock<IMailChimpManager>();
MockMailChimpManager.Setup(x => x.Campaigns.AddAsync(It.IsAny<Campaign>())).ReturnsAsync(new Campaign() { Id = "1234" });
MockMailChimpManager.Setup(x => x.Content.AddOrUpdateAsync(It.IsAny<string>(), It.IsAny<ContentRequest>()));
MockMailChimpManager.Setup(x => x.Campaigns.SendAsync(It.IsAny<string>()));

MailService = new MailService(MockMailChimpManager.Object, MockLogger.Object);
}

public override void Dispose()
{

}

Item exampleItem = new Item()
{
Id = "123",
Title = "Some Title",
Slug = "abc",
ShortSummary = "Some short summary",
ResourceLinks = "",
Comment = "",
Speciality = new List<Speciality>
{
new Speciality()
{
Key = "1a",
Title = "Some speciality",

}
},
EvidenceType = new EvidenceType()
{
Key = "1b",
Title = "Some evidence type"
},
Source = new Source()
{
Id = "1c",
Title = "Some source"
}
};
Item exampleItem2 = new Item()
{
Id = "123",
Title = "Some Title",
Slug = "abc",
ShortSummary = "Some short summary",
ResourceLinks = "",
Comment = "",
Speciality = new List<Speciality>
{
new Speciality()
{
Key = "1a",
Title = "Some speciality 2",

}
},
EvidenceType = new EvidenceType()
{
Key = "1c",
Title = "Some evidence type 2"
},
Source = new Source()
{
Id = "1c",
Title = "Some source"
}
};

[Fact]
public void CanCreateSingleItemEmail()
{
var items = new List<Item> { exampleItem };
var actualHtml = this.MailService.CreateDailyEmailBody(items);

actualHtml.ShouldMatchApproved();

}

[Fact]
public void CanCreateEmailWithTwoItemsSharingEvidenceType()
{
var items = new List<Item> { exampleItem, exampleItem };
var actualHtml = this.MailService.CreateDailyEmailBody(items);

actualHtml.ShouldMatchApproved();

}

[Fact]
public void CanCreateEmailWithTwoItemsDifferentEvidenceType()
{
var items = new List<Item> { exampleItem, exampleItem2 };
var actualHtml = this.MailService.CreateDailyEmailBody(items);

actualHtml.ShouldMatchApproved();

}

[Fact]
public void ItemsWithManySpecialitiesRenderCorrectly()
{
exampleItem.Speciality.Add(new Speciality() { Key = "abcd", Title = "Another speciality" });
var items = new List<Item> { exampleItem };
var actualHtml = this.MailService.CreateDailyEmailBody(items);

actualHtml.ShouldMatchApproved();

}

}
}
4 changes: 2 additions & 2 deletions lambda/MAS/Services/ContentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public async Task<IEnumerable<Item>> GetItemsAsync()
try
{
var jsonStr = await client.DownloadStringTaskAsync(new Uri(AppSettings.CMSConfig.URI));
var json = JsonConvert.DeserializeObject<Item[]>(jsonStr);
return json;
var item = JsonConvert.DeserializeObject<Item[]>(jsonStr);
return item;
}
catch(Exception e)
{
Expand Down

2 comments on commit 697face

@NICE-TeamCity
Copy link

Choose a reason for hiding this comment

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

TeamCity MAS / MAS Build 1.1.0.544-MAS-129_121__112_Dai outcome was FAILURE
Summary: Artifacts size 172 KB is 95% different from 3.6 MB in build #1.1.0.543-rAF001FD (new) Build time: 00:00:38

@NICE-TeamCity
Copy link

Choose a reason for hiding this comment

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

TeamCity MAS / MAS Build 1.1.0.545-MAS-129_121__112_Dai outcome was FAILURE
Summary: Artifacts size 172 KB is 95% different from 3.6 MB in build #1.1.0.543-rAF001FD Build time: 00:00:37

Please sign in to comment.