f916cfad6b
This commit restructures the EventOccurrenceParser by breaking down its functionality into modular components, including EventDefinitionResolver, LineClassifier, LocationPatternMatcher, SectionHeaderMatcher, TimeLocationParser, and TimeParser. This refactoring enhances code readability and maintainability, allowing for easier updates and testing. Additionally, the TextUtil class has been updated to include input sanitization methods. Comprehensive unit tests have been added to ensure the correctness of the new parsing logic and to validate the handling of various event occurrence scenarios.
163 lines
4.3 KiB
C#
163 lines
4.3 KiB
C#
using Core.Entities;
|
|
using Core.Parsers.EventOccurrence;
|
|
using FuzzySharp;
|
|
using NUnit.Framework;
|
|
|
|
namespace Tests.Parsers.EventOccurrence;
|
|
|
|
[TestFixture]
|
|
public class SectionHeaderMatcher_Tests
|
|
{
|
|
[Test]
|
|
public void IsGeneralSchedule_GeneralScheduleLine_ReturnsTrue()
|
|
{
|
|
// Act & Assert
|
|
Assert.That(SectionHeaderMatcher.IsGeneralSchedule("General Schedule"), Is.True);
|
|
Assert.That(SectionHeaderMatcher.IsGeneralSchedule("General Schedule - MS"), Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void IsGeneralSchedule_RegularEvent_ReturnsFalse()
|
|
{
|
|
// Act & Assert
|
|
Assert.That(SectionHeaderMatcher.IsGeneralSchedule("Test Event - MS"), Is.False);
|
|
}
|
|
|
|
[Test]
|
|
public void HasSchoolLevel_ContainsMS_ReturnsTrue()
|
|
{
|
|
// Act & Assert
|
|
Assert.That(SectionHeaderMatcher.HasSchoolLevel("Test Event - MS"), Is.True);
|
|
Assert.That(SectionHeaderMatcher.HasSchoolLevel("Test Event MS"), Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void HasSchoolLevel_ContainsHS_ReturnsTrue()
|
|
{
|
|
// Act & Assert
|
|
Assert.That(SectionHeaderMatcher.HasSchoolLevel("Test Event - HS"), Is.True);
|
|
Assert.That(SectionHeaderMatcher.HasSchoolLevel("Test Event HS"), Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void HasSchoolLevel_NoSchoolLevel_ReturnsFalse()
|
|
{
|
|
// Act & Assert
|
|
Assert.That(SectionHeaderMatcher.HasSchoolLevel("Test Event"), Is.False);
|
|
}
|
|
|
|
[Test]
|
|
public void MatchEventDefinition_ExactMatch_ReturnsEvent()
|
|
{
|
|
// Arrange
|
|
var events = new List<EventDefinition>
|
|
{
|
|
new() { Name = "Test Event" },
|
|
new() { Name = "Another Event" }
|
|
};
|
|
|
|
// Act
|
|
var result = SectionHeaderMatcher.MatchEventDefinition("Test Event", events);
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result!.Name, Is.EqualTo("Test Event"));
|
|
}
|
|
|
|
[Test]
|
|
public void MatchEventDefinition_CloseMatch_ReturnsEvent()
|
|
{
|
|
// Arrange
|
|
var events = new List<EventDefinition>
|
|
{
|
|
new() { Name = "Test Event" },
|
|
new() { Name = "Another Event" }
|
|
};
|
|
|
|
// Act
|
|
var result = SectionHeaderMatcher.MatchEventDefinition("Test Evnt", events); // Typo but close
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result!.Name, Is.EqualTo("Test Event"));
|
|
}
|
|
|
|
[Test]
|
|
public void MatchEventDefinition_NoMatch_ReturnsNull()
|
|
{
|
|
// Arrange
|
|
var events = new List<EventDefinition>
|
|
{
|
|
new() { Name = "Test Event" }
|
|
};
|
|
|
|
// Act
|
|
var result = SectionHeaderMatcher.MatchEventDefinition("Completely Different Event", events);
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void MatchEventDefinition_EmptyEvents_ReturnsNull()
|
|
{
|
|
// Arrange
|
|
var events = new List<EventDefinition>();
|
|
|
|
// Act
|
|
var result = SectionHeaderMatcher.MatchEventDefinition("Test Event", events);
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void MatchEventDefinition_MultipleMatches_ReturnsBestMatch()
|
|
{
|
|
// Arrange
|
|
var events = new List<EventDefinition>
|
|
{
|
|
new() { Name = "Test Event" },
|
|
new() { Name = "Test Event Advanced" },
|
|
new() { Name = "Another Event" }
|
|
};
|
|
|
|
// Act
|
|
var result = SectionHeaderMatcher.MatchEventDefinition("Test Event", events);
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result!.Name, Is.EqualTo("Test Event")); // Exact match should win
|
|
}
|
|
|
|
[Test]
|
|
public void GetBestMatchRatio_ValidEvents_ReturnsRatio()
|
|
{
|
|
// Arrange
|
|
var events = new List<EventDefinition>
|
|
{
|
|
new() { Name = "Test Event" }
|
|
};
|
|
|
|
// Act
|
|
var ratio = SectionHeaderMatcher.GetBestMatchRatio("Test Event", events);
|
|
|
|
// Assert
|
|
Assert.That(ratio, Is.GreaterThan(50)); // Should be high for exact match
|
|
}
|
|
|
|
[Test]
|
|
public void GetBestMatchRatio_EmptyEvents_ReturnsZero()
|
|
{
|
|
// Arrange
|
|
var events = new List<EventDefinition>();
|
|
|
|
// Act
|
|
var ratio = SectionHeaderMatcher.GetBestMatchRatio("Test Event", events);
|
|
|
|
// Assert
|
|
Assert.That(ratio, Is.EqualTo(0));
|
|
}
|
|
}
|
|
|