Skip to content

Commit

Permalink
fix: unit tests not running in pipeline (#15)
Browse files Browse the repository at this point in the history
* test: validate page structure (#14)

* refactor: add version attribute to csproj

* ci: create pipeline to run unit tests

* chore: add missing deps

* test: logging to analyse error

* ci: Update and rename dotnet.yml to unit-test.yml
  • Loading branch information
AnsgarLichter authored Mar 4, 2024
1 parent 7f0b26f commit df7b97c
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 70 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: .NET

on:
push:
branches: [ "*" ]
pull_request:
branches: [ "*" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
3 changes: 2 additions & 1 deletion ExportKindleClippingsToNotion/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public class Arguments

public bool IsParseSuccessful => _parserResult.Tag == ParserResultType.Parsed;

public static Arguments Parse(IEnumerable<string> arguments) => new(Parser.Default.ParseArguments<Options>(arguments));
public static Arguments Parse(IEnumerable<string> arguments) =>
new(Parser.Default.ParseArguments<Options>(arguments));
}
26 changes: 11 additions & 15 deletions ExportKindleClippingsToNotion/ExportKindleClippingsToNotion.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>


<Version>1.0.0</Version>
<PackAsTool>true</PackAsTool>
<ToolCommandName>exportKindleClippingsToNotion</ToolCommandName>
<PackageOutputPath>nupkg</PackageOutputPath>
Expand All @@ -14,32 +15,27 @@
<RepositoryUrl>https://github.com/AnsgarLichter/ExportKindleClippingsToNotion</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>

<ItemGroup>
<None Include="../README.md" Pack="true" PackagePath="/"/>
<None Include="../README.md" Pack="true" PackagePath="/"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="EnumStringValues" Version="4.0.2"/>
<PackageReference Include="FakeItEasy" Version="8.0.1" />
<PackageReference Include="FakeItEasy" Version="8.0.1"/>
<PackageReference Include="Google.Apis.Books.v1" Version="1.64.0.2955"/>
<PackageReference Include="Notion.Net" Version="4.2.0"/>
<PackageReference Include="System.IO.Abstractions" Version="20.0.15" />
<PackageReference Include="System.IO.Abstractions" Version="20.0.15"/>
</ItemGroup>

<ItemGroup>
<Content Include="Assets/**">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<Folder Include="Assets\" />
</ItemGroup>


<ItemGroup>
<Reference Include="JetBrains.ReSharper.TestRunner.Merged">
<HintPath>..\..\..\..\..\..\Program Files\JetBrains\JetBrains Rider 2023.3.3\lib\ReSharperHost\TestRunner\netcoreapp3.0\JetBrains.ReSharper.TestRunner.Merged.dll</HintPath>
</Reference>
<Folder Include="Assets\"/>
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion ExportKindleClippingsToNotion/Parser/ClippingsLanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public SupportedLanguages Determine(string clipping)
{
throw new LanguageNotRecognizedException("The language of your clipping can't be recognized!");
}


Console.Write(secondLine);
foreach (var identifier in _languageIdentifiers
.Where(identifier => secondLine.Contains(identifier.Value)))
return identifier.Key;
Expand Down
192 changes: 172 additions & 20 deletions UnitTests/Notion/Utils/PageBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ public PageBuilderTest()
[Fact]
public void Create_ValidBook_ReturnsPagesCreateParameters()
{
var date = DateTime.Now;
var book = new Book("Author", "Title")
{
ThumbnailUrl = "https://example.com/image.jpg"
ThumbnailUrl = "https://example.com/image.jpg",
LastSynchronized = date
};
book.AddClipping(new Clipping("Text1", 1, 2, 1, DateTime.Now, book));
book.AddClipping(new Clipping("Text2", 3, 4, 2, DateTime.Now, book));
book.AddClipping(new Clipping("Text1", 1, 2, 1, date, book));
book.AddClipping(new Clipping("Text2", 3, 4, 2, date, book));

var result = _testSubject.Create(book);

Expand All @@ -39,24 +41,28 @@ public void Create_ValidBook_ReturnsPagesCreateParameters()
Assert.NotNull(result.Cover);
Assert.NotNull(result.Properties);
Assert.Equal(4, result.Properties.Count);
AssertPagePropertySet(result.Properties, "Title");
AssertPagePropertySet(result.Properties, "Author");
AssertPagePropertySet(result.Properties, "Highlights");
AssertPagePropertySet(result.Properties, "Last Synchronized");
Assert.NotNull(result.Icon);
Assert.NotNull(result.Cover);
AssertPagePropertySet(result.Properties, "Title", typeof(TitlePropertyValue), "Title");
AssertPagePropertySet(result.Properties, "Author", typeof(RichTextPropertyValue), "Author");
AssertPagePropertySet(result.Properties, "Highlights", typeof(NumberPropertyValue), "2");
AssertPagePropertySet(result.Properties, "Last Synchronized", typeof(DatePropertyValue),
date.ToString());
Assert.Equal(book.Emoji, (result.Icon as EmojiObject).Emoji);
Assert.Equal(book.ThumbnailUrl, (result.Cover as ExternalFile).External.Url);
Assert.NotNull(result.Properties);
Assert.NotNull(result.Children);
}

[Fact]
public void CreateClippingsTable_ValidBook_ReturnsTableBlock()
{
var date = DateTime.Now;
var book = new Book("Author", "Title")
{
ThumbnailUrl = "https://example.com/image.jpg"
ThumbnailUrl = "https://example.com/image.jpg",
LastSynchronized = date
};
book.AddClipping(new Clipping("Text1", 1, 2, 1, DateTime.Now, book));
book.AddClipping(new Clipping("Text2", 3, 4, 2, DateTime.Now, book));
book.AddClipping(new Clipping("Text1", 1, 2, 1, date, book));
book.AddClipping(new Clipping("Text2", 3, 4, 2, date, book));

var result = _testSubject.CreateClippingsTable(book);

Expand All @@ -70,11 +76,74 @@ public void CreateClippingsTable_ValidBook_ReturnsTableBlock()
Assert.Equal(4, tableBlock.Table.TableWidth);
Assert.NotEmpty(tableBlock.Table.Children);

var rowEnumerator = tableBlock.Table.Children.GetEnumerator();
Assert.True(rowEnumerator.MoveNext(), "No table rows found in the table.");
Assert.True(rowEnumerator.MoveNext(), "No table rows found in the table.");
Assert.True(rowEnumerator.MoveNext(), "No table rows found in the table.");
Assert.False(rowEnumerator.MoveNext(), "Too many table rows found in the table.");
var tableRows = tableBlock.Table.Children.ToList();
Assert.Equal(3, tableRows.Count);

//First Row
var firstRow = tableRows[0];
Assert.NotNull(firstRow.TableRow);
Assert.NotNull(firstRow.TableRow.Cells);


var cells = firstRow.TableRow.Cells.ToList();

var firstCellContents = cells[0].ToList();
Assert.Single(firstCellContents);
var firstCellText = firstCellContents[0].Text.Content;
Assert.Equal("Clipping", firstCellText);

var secondCellContents = cells[1].ToList();
Assert.Single(secondCellContents);
var secondCellText = secondCellContents[0].Text.Content;
Assert.Equal("Page", secondCellText);

var thirdCellContents = cells[2].ToList();
Assert.Single(thirdCellContents);
var thirdCellText = thirdCellContents[0].Text.Content;
Assert.Equal("Start Position", thirdCellText);

var fourthCellContents = cells[3].ToList();
Assert.Single(fourthCellContents);
var fourthCellText = fourthCellContents[0].Text.Content;
Assert.Equal("Finish Position", fourthCellText);

var secondRow = tableRows[1];
Assert.NotNull(secondRow.TableRow);
Assert.NotNull(secondRow.TableRow.Cells);

var secondRowCells = secondRow.TableRow.Cells.ToList();
Assert.Equal(4, secondRowCells.Count);

var secondRowFirstCellText = secondRowCells[0].ToList()[0].Text.Content;
Assert.Equal("Text1", secondRowFirstCellText);

var secondRowSecondCellText = secondRowCells[1].ToList()[0].Text.Content;
Assert.Equal("1", secondRowSecondCellText);

var secondRowThirdCellText = secondRowCells[2].ToList()[0].Text.Content;
Assert.Equal("1", secondRowThirdCellText);

var secondRowFourthCellText = secondRowCells[3].ToList()[0].Text.Content;
Assert.Equal("2", secondRowFourthCellText);

var thirdRow = tableRows[2];
Assert.NotNull(thirdRow.TableRow);
Assert.NotNull(thirdRow.TableRow.Cells);

var thirdRowCells = thirdRow.TableRow.Cells.ToList();
Assert.Equal(4, thirdRowCells.Count);

var thirdRowFirstCellText = thirdRowCells[0].ToList()[0].Text.Content;
Assert.Equal("Text2", thirdRowFirstCellText);

var thirdRowSecondCellText = thirdRowCells[1].ToList()[0].Text.Content;
Assert.Equal("2", thirdRowSecondCellText);

var thirdRowThirdCellText = thirdRowCells[2].ToList()[0].Text.Content;
Assert.Equal("3", thirdRowThirdCellText);

var thirdRowFourthCellText = thirdRowCells[3].ToList()[0].Text.Content;
Assert.Equal("4", thirdRowFourthCellText);
}

[Fact]
Expand All @@ -89,10 +158,14 @@ public void Update_ValidPageAndBook_ReturnsPagesUpdateParameters()
{ "Last Edited", new RichTextPropertyValue() }
}
};
var date = DateTime.Now;
var book = new Book("Author", "Title")
{
ThumbnailUrl = "https://example.com/image.jpg"
ThumbnailUrl = "https://example.com/image.jpg",
LastSynchronized = date
};
book.AddClipping(new Clipping("Text1", 1, 2, 1, date, book));
book.AddClipping(new Clipping("Text2", 3, 4, 2, date, book));

var WithPropertyAuthor = A.CallTo(() =>
_updateParametersBuilder.WithProperty("Author", A<RichTextPropertyValue>.Ignored));
Expand Down Expand Up @@ -120,9 +193,88 @@ public void Update_ValidPageAndBook_ReturnsPagesUpdateParameters()
WithPropertyLastEdited.MustNotHaveHappened();
}

private void AssertPagePropertySet(IDictionary<string, PropertyValue> properties, string key)
private void AssertPagePropertySet(IDictionary<string, PropertyValue> properties, string key, Type
propertyValueClazz, string expectedValue)
{
PropertyValue value;
Assert.True(properties.TryGetValue(key, out value), "Title property not found in dictionary.");
Assert.True(properties.TryGetValue(key, out value), $"property {key} not found in dictionary.");
Assert.NotNull(value);

var actualProperty = Convert.ChangeType(value, propertyValueClazz);
Assert.NotNull(actualProperty);

switch (propertyValueClazz.Name)
{
case nameof(TitlePropertyValue):
var titleProperty = propertyValueClazz.GetProperty("Title");
Assert.NotNull(titleProperty);
var titleValue = titleProperty.GetValue(actualProperty);
Assert.NotNull(titleValue);

if (titleValue is List<RichTextBase> richTextList && richTextList.Count > 0)
{
var firstRichText = richTextList[0];
if (firstRichText is RichTextText richTextText)
{
Assert.Equal(expectedValue, richTextText.Text.Content);
}
else
{
Assert.True(false, "Invalid property type");
}
}
else
{
Assert.True(false, "Invalid property type");
}

break;
case nameof(RichTextPropertyValue):
var richtTextProperty = propertyValueClazz.GetProperty("RichText");
Assert.NotNull(richtTextProperty);
var richTextValue = richtTextProperty.GetValue(actualProperty);
Assert.NotNull(richTextValue);

if (richTextValue is List<RichTextBase> richTextList2 && richTextList2.Count > 0)
{
var firstRichText = richTextList2[0];
if (firstRichText is RichTextText richTextText)
{
Assert.Equal(expectedValue, richTextText.Text.Content);
}
else
{
Assert.True(false, "Invalid property type");
}
}
else
{
Assert.True(false, "Invalid property type");
}

break;
case nameof(NumberPropertyValue):
var numberProperty = propertyValueClazz.GetProperty("Number");
Assert.NotNull(numberProperty);
Assert.Equal(expectedValue, numberProperty.GetValue(actualProperty)?.ToString());
break;
case nameof(DatePropertyValue):
var dateProperty = propertyValueClazz.GetProperty("Date");
Assert.NotNull(dateProperty);
var dateValue = dateProperty.GetValue(actualProperty);
if (dateValue is Date startDateValue)
{
Assert.Equal(expectedValue, startDateValue.Start.ToString());
}
else
{
Assert.True(false, "Invalid property type");
}

break;
default:
Assert.True(false, "Unexpected property Type");
break;
}
}
}
Loading

0 comments on commit df7b97c

Please sign in to comment.