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

File_Toolkit: Add unit tests #163

Merged
merged 4 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
99 changes: 99 additions & 0 deletions .ci/unit-tests/File_Toolkit_Tests/Adapter/PushPullTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using NUnit.Framework;
using BH.Adapter.File;
using BH.oM.Structure.Elements;
using System.Reflection;
using FluentAssertions;
using Newtonsoft.Json;

namespace BH.Tests.Adapter
{
public class PushPullTests
{
string randomTestFilePath = "";

[SetUp]
public void SetupRandomTestFilePath()
{
randomTestFilePath = Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid().ToString() + ".json");
}

[TearDown]
public void DeleteRandomTestFile()
{
if (File.Exists(randomTestFilePath))
File.Delete(randomTestFilePath);
}

[Test]
public void Bar()
{
Bar bar = (Bar)BH.Engine.Base.Create.RandomObject(typeof(Bar));

FileAdapter fa = new FileAdapter(randomTestFilePath);
var objectsToPush = new List<object>() { bar };
fa.Push(objectsToPush, "", BH.oM.Adapter.PushType.DeleteThenCreate);

if (!File.Exists(randomTestFilePath))
Assert.Fail("File was not created.");

var fileContent = fa.Pull(new BH.oM.Adapters.File.FileContentRequest() { File = randomTestFilePath });

fileContent.Should().BeEquivalentTo(objectsToPush);
}

[Test]
public void Bar_FormattedJson()
{
Bar bar = (Bar)BH.Engine.Base.Create.RandomObject(typeof(Bar));

FileAdapter fa = new FileAdapter(randomTestFilePath);
var objectsToPush = new List<object>() { bar };
fa.Push(objectsToPush, "", BH.oM.Adapter.PushType.DeleteThenCreate);

if (!File.Exists(randomTestFilePath))
Assert.Fail("File was not created.");

// Format the json.
OverwriteWithFormattedJson(randomTestFilePath);

var fileContent = fa.Pull(new BH.oM.Adapters.File.FileContentRequest() { File = randomTestFilePath });

fileContent.Should().BeEquivalentTo(objectsToPush);
}

private static string FormatJson(string json)
{
dynamic parsedJson = JsonConvert.DeserializeObject(json);
return JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
}

private static void OverwriteWithFormattedJson(string filepath)
{
string json = File.ReadAllText(filepath);
string formattedJson = FormatJson(json);
File.WriteAllText(filepath, formattedJson);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using NUnit.Framework;
using BH.Adapter.File;
using BH.oM.Structure.Elements;
using System.Reflection;
using FluentAssertions;
using Newtonsoft.Json;

namespace BH.Tests.Engine.Compute
{
public class Compute
{
string randomTestFilePath = "";

[SetUp]
public void SetupRandomTestFilePath()
{
randomTestFilePath = Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid().ToString() + ".json");
}

[TearDown]
public void DeleteRandomTestFile()
{
if (File.Exists(randomTestFilePath))
File.Delete(randomTestFilePath);
}

[Test]
public void Bar()
{
Bar bar = (Bar)BH.Engine.Base.Create.RandomObject(typeof(Bar));

FileAdapter fa = new FileAdapter(randomTestFilePath);
var objectsToPush = new List<object>() { bar };
fa.Push(objectsToPush, "", BH.oM.Adapter.PushType.DeleteThenCreate);

if (!File.Exists(randomTestFilePath))
Assert.Fail("File was not created.");

var fileContent = BH.Engine.Adapters.File.Compute.ReadFromJsonFile(randomTestFilePath, true);

fileContent.Should().BeEquivalentTo(objectsToPush);
}

[Test]
public void Bar_FormattedJson()
{
Bar bar = (Bar)BH.Engine.Base.Create.RandomObject(typeof(Bar));

FileAdapter fa = new FileAdapter(randomTestFilePath);
var objectsToPush = new List<object>() { bar };
fa.Push(objectsToPush, "", BH.oM.Adapter.PushType.DeleteThenCreate);

if (!File.Exists(randomTestFilePath))
Assert.Fail("File was not created.");

// Format the json.
OverwriteWithFormattedJson(randomTestFilePath);

var fileContent = BH.Engine.Adapters.File.Compute.ReadFromJsonFile(randomTestFilePath, true);

fileContent.Should().BeEquivalentTo(objectsToPush);
}

private static string FormatJson(string json)
{
dynamic parsedJson = JsonConvert.DeserializeObject(json);
return JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
}

private static void OverwriteWithFormattedJson(string filepath)
{
string json = File.ReadAllText(filepath);
string formattedJson = FormatJson(json);
File.WriteAllText(filepath, formattedJson);
}
}
}
91 changes: 91 additions & 0 deletions .ci/unit-tests/File_Toolkit_Tests/File_Toolkit_Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>

FraserGreenroyd marked this conversation as resolved.
Show resolved Hide resolved
<Configurations>Debug;Release;Test</Configurations>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\File_Adapter\File_Adapter.csproj" />
<ProjectReference Include="..\..\..\File_Engine\File_Engine.csproj" />
<ProjectReference Include="..\..\..\File_oM\File_oM.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Adapter_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Adapter_oM.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Analytical_Engine">
<HintPath>$(ProgramData)\BHoM\Assemblies\Analytical_Engine.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Analytical_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Analytical_oM.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="BHoM">
<HintPath>$(ProgramData)\BHoM\Assemblies\BHoM.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="BHoM_Adapter">
<HintPath>$(ProgramData)\BHoM\Assemblies\BHoM_Adapter.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="BHoM_Engine">
<HintPath>$(ProgramData)\BHoM\Assemblies\BHoM_Engine.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Data_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Data_oM.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Dimensional_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Dimensional_oM.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Geometry_Engine">
<HintPath>$(ProgramData)\BHoM\Assemblies\Geometry_Engine.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Geometry_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Geometry_oM.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Physical_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Physical_oM.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Spatial_Engine">
<HintPath>$(ProgramData)\BHoM\Assemblies\Spatial_Engine.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Spatial_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Spatial_oM.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Structure_Engine">
<HintPath>$(ProgramData)\BHoM\Assemblies\Structure_Engine.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Structure_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Structure_oM.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
</ItemGroup>

</Project>
43 changes: 43 additions & 0 deletions .ci/unit-tests/File_Toolkit_Tests/File_Toolkit_Tests.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33424.131
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "File_Toolkit_Tests", "File_Toolkit_Tests.csproj", "{DABCC8EE-CA63-4A69-BA89-AB6E58E0E499}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "File_Adapter", "..\..\..\File_Adapter\File_Adapter.csproj", "{A5D81731-370E-4741-997B-14EC448756FC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "File_Engine", "..\..\..\File_Engine\File_Engine.csproj", "{1213A0FB-F189-4998-9A28-CAAFE8C66826}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "File_oM", "..\..\..\File_oM\File_oM.csproj", "{727D28D4-A36D-4CB3-8AD5-77498E2A81A6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DABCC8EE-CA63-4A69-BA89-AB6E58E0E499}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DABCC8EE-CA63-4A69-BA89-AB6E58E0E499}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DABCC8EE-CA63-4A69-BA89-AB6E58E0E499}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DABCC8EE-CA63-4A69-BA89-AB6E58E0E499}.Release|Any CPU.Build.0 = Release|Any CPU
{A5D81731-370E-4741-997B-14EC448756FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A5D81731-370E-4741-997B-14EC448756FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5D81731-370E-4741-997B-14EC448756FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5D81731-370E-4741-997B-14EC448756FC}.Release|Any CPU.Build.0 = Release|Any CPU
{1213A0FB-F189-4998-9A28-CAAFE8C66826}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1213A0FB-F189-4998-9A28-CAAFE8C66826}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1213A0FB-F189-4998-9A28-CAAFE8C66826}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1213A0FB-F189-4998-9A28-CAAFE8C66826}.Release|Any CPU.Build.0 = Release|Any CPU
{727D28D4-A36D-4CB3-8AD5-77498E2A81A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{727D28D4-A36D-4CB3-8AD5-77498E2A81A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{727D28D4-A36D-4CB3-8AD5-77498E2A81A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{727D28D4-A36D-4CB3-8AD5-77498E2A81A6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AEBBD38D-19BB-40B5-A805-E1AC6E36279E}
EndGlobalSection
EndGlobal