-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
208 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
using Eleia; | ||
using Eleia.CoyoteApi; | ||
using Eleia.Test.Helpers; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Eleia.Test | ||
{ | ||
public class BotTests | ||
{ | ||
private FakeLoggerFactory fakeLoggerFactory; | ||
|
||
private Bot CreateBot() | ||
{ | ||
fakeLoggerFactory = new FakeLoggerFactory(); | ||
return new Bot(new CoyoteHandler(fakeLoggerFactory), new PostAnalyzer(null), new Blacklist(), fakeLoggerFactory); | ||
} | ||
|
||
private List<string> GetFakeLogs() | ||
{ | ||
return (fakeLoggerFactory.CreateLogger("") as FakeLogger).Logs; | ||
} | ||
|
||
[Fact] | ||
public void LoadAlreadyAnalyzed_WhenIgnoreAlreadyAnalyzed_ShouldBeEmpty() | ||
{ | ||
var bot = CreateBot(); | ||
bot.IgnoreAlreadyAnalyzed = true; | ||
|
||
bot.LoadAlreadyAnalyzed(); | ||
|
||
Assert.Empty(bot.AlreadyAnalyzedDatabase); | ||
} | ||
|
||
[Fact] | ||
public void SaveAlreadyAnalyzed_WhenIgnoreAlreadyAnalyzed_ShouldDoNothing() | ||
{ | ||
var bot = CreateBot(); | ||
bot.IgnoreAlreadyAnalyzed = true; | ||
|
||
bot.SaveAlreadyAnalyzed(); | ||
|
||
var logs = GetFakeLogs(); | ||
|
||
Assert.DoesNotContain(logs, x => x == "Saving already analyzed ids database."); | ||
} | ||
|
||
[Fact] | ||
public void BlacklistUpdateDefinition_ShouldUpdateDefinition() | ||
{ | ||
var bot = CreateBot(); | ||
bot.IgnoreAlreadyAnalyzed = true; | ||
|
||
var old = bot.BlacklistDefinition; | ||
bot.BlacklistDefinition = "1,2,3,4"; | ||
|
||
Assert.NotEqual(bot.BlacklistDefinition, old); | ||
Assert.Equal("1,2,3,4", bot.BlacklistDefinition); | ||
} | ||
|
||
[Fact] | ||
public async void AnalyzePostAsync_Post_ShouldUpdateAlreadyAnalyzed() | ||
{ | ||
var bot = CreateBot(); | ||
bot.IgnoreAlreadyAnalyzed = true; | ||
bot.LoadAlreadyAnalyzed(); | ||
|
||
await bot.AnalyzePostAsync(new Post() { id = 1, text = "" }); | ||
|
||
Assert.Single(bot.AlreadyAnalyzedDatabase); | ||
Assert.Equal(1, bot.AlreadyAnalyzedDatabase.First()); | ||
} | ||
|
||
[Fact] | ||
public async void AnalyzePostAsync_SamePostIdTestedForSecondTime_ShouldBeIgnored() | ||
{ | ||
var bot = CreateBot(); | ||
bot.IgnoreAlreadyAnalyzed = true; | ||
bot.LoadAlreadyAnalyzed(); | ||
|
||
await bot.AnalyzePostAsync(new Post() { id = 1, text = "one text" }); | ||
await bot.AnalyzePostAsync(new Post() { id = 1, text = "different text" }); | ||
|
||
var logs = GetFakeLogs(); | ||
|
||
Assert.Contains(logs, x => x == "[Debug] Ignoring post 1 because already analyzed"); | ||
} | ||
|
||
[Fact] | ||
public async void AnalyzePostAsync_PostFromBlacklist_ShouldBeIgnored() | ||
{ | ||
var bot = CreateBot(); | ||
bot.IgnoreAlreadyAnalyzed = true; | ||
bot.LoadAlreadyAnalyzed(); | ||
bot.BlacklistDefinition = "1"; | ||
|
||
await bot.AnalyzePostAsync(new Post() { id = 1, text = "", forum_id = 1 }); | ||
var logs = GetFakeLogs(); | ||
|
||
Assert.Equal("[Debug] Ignoring post 1 because of blacklist", logs.Last()); | ||
} | ||
|
||
[Fact] | ||
public void Endpoints_WhenNotDebug_ShouldBeBasedOn4pnet() | ||
{ | ||
Endpoints.IsDebug = false; | ||
|
||
Assert.Contains("4programmers.net", Endpoints.CommentPage); | ||
Assert.Contains("4programmers.net", Endpoints.LoginPage); | ||
Assert.Contains("4programmers.net", Endpoints.MainPage); | ||
Assert.Contains("4programmers.net", Endpoints.LogoutPage); | ||
Assert.Contains("4programmers.net", Endpoints.PostsApi); | ||
} | ||
|
||
[Fact] | ||
public void Endpoints_WhenDebug_ShouldBeBasedOn4pinfo() | ||
{ | ||
Endpoints.IsDebug = true; | ||
|
||
Assert.Contains("dev.4programmers.info", Endpoints.CommentPage); | ||
Assert.Contains("dev.4programmers.info", Endpoints.LoginPage); | ||
Assert.Contains("dev.4programmers.info", Endpoints.MainPage); | ||
Assert.Contains("dev.4programmers.info", Endpoints.LogoutPage); | ||
Assert.Contains("dev.4programmers.info", Endpoints.PostsApi); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Eleia.Test.Helpers | ||
{ | ||
public sealed class FakeLoggerFactory : ILoggerFactory, IDisposable | ||
{ | ||
private static FakeLogger instance; | ||
|
||
public void AddProvider(ILoggerProvider provider) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
if (instance == null) | ||
{ | ||
instance = new FakeLogger(); | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
} | ||
|
||
public class FakeLogger : ILogger | ||
{ | ||
public List<string> Logs { get; } | ||
|
||
public FakeLogger() | ||
{ | ||
Logs = new List<string>(); | ||
} | ||
|
||
public IDisposable BeginScope<TState>(TState state) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public bool IsEnabled(LogLevel logLevel) | ||
{ | ||
return true; | ||
} | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) | ||
{ | ||
Logs.Add(string.Format("[{0}] {1}", logLevel, formatter.Invoke(state, exception))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters