Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asvol committed Nov 27, 2024
1 parent b0c8060 commit 3d4f5e6
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 21 deletions.
12 changes: 8 additions & 4 deletions src/Asv.Cfg.Test/ConfigurationBaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
using System.Linq;
using System.Threading;
using Xunit;
using Xunit.Abstractions;

namespace Asv.Cfg.Test;



public abstract class ConfigurationBaseTest<T>
where T:IConfiguration
public abstract class ConfigurationBaseTest<T>(ITestOutputHelper log)
where T : IConfiguration
{
protected abstract IDisposable CreateForTest(out T configuration);


public TestLoggerFactory LogFactory { get; set; } = new(log, TimeProvider.System, "Test");

[Fact]
public void Check_If_Saved_Setting_Exists()
{
Expand Down Expand Up @@ -65,7 +68,8 @@ public void Check_Available_Parts_Value()
Thread.Sleep(50);

var expectedResult = new string[] { "test1", "test2", "test3", "test4" };
var actualResult = cfg.AvailableParts.OrderBy(x=>x).ToArray(); // items can be reordered in any way, so we need to sort them
var exclude = cfg.ReservedParts.ToHashSet();
var actualResult = cfg.AvailableParts.OrderBy(x=>x).Where(x=>exclude.Contains(x) == false).ToArray(); // items can be reordered in any way, so we need to sort them

Assert.Equal(expectedResult, actualResult);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Asv.Cfg.Test/InMemory/InMemoryConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
namespace Asv.Cfg.Test
{
[TestSubject(typeof(InMemoryConfiguration))]
public class InMemoryConfigurationTest(ITestOutputHelper log) : ConfigurationBaseTest<InMemoryConfiguration>
public class InMemoryConfigurationTest(ITestOutputHelper log) : ConfigurationBaseTest<InMemoryConfiguration>(log)
{
private readonly ITestOutputHelper _log = log;

protected override IDisposable CreateForTest(out InMemoryConfiguration configuration)
{
configuration = new InMemoryConfiguration(new TestLogger(_log,TimeProvider.System, "IM_MEMORY"));
configuration = new InMemoryConfiguration(LogFactory.CreateLogger("IM_MEMORY"));
var cfg = configuration;
return Disposable.Create(() => {cfg.Dispose(); });
}
Expand Down
6 changes: 3 additions & 3 deletions src/Asv.Cfg.Test/Json/JsonConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public class TestMultiThreadClassFour


[TestSubject(typeof(JsonConfiguration))]
public class JsonConfigurationTest(ITestOutputHelper log) : ConfigurationBaseTest<JsonConfiguration>
public class JsonConfigurationTest(ITestOutputHelper log)
: ConfigurationBaseTest<JsonConfiguration>(log)
{
private readonly IFileSystem _fileSystem = new MockFileSystem();

Expand All @@ -75,8 +76,7 @@ protected override IDisposable CreateForTest(out JsonConfiguration configuration
_fileSystem.Directory.Delete(workingDir);
}

log.WriteLine($"Working directory: {workingDir}");
configuration = new JsonConfiguration(workingDir, logger:new TestLogger(log,TimeProvider.System, "JSON"), fileSystem: _fileSystem);
configuration = new JsonConfiguration(workingDir, logger:LogFactory.CreateLogger("JSON_CONFIG"), fileSystem: _fileSystem);
var cfg = configuration;
return Disposable.Create(() =>
{
Expand Down
5 changes: 3 additions & 2 deletions src/Asv.Cfg.Test/Json/JsonOneFileConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
namespace Asv.Cfg.Test
{
[TestSubject(typeof(JsonOneFileConfiguration))]
public class JsonOneFileConfigurationTest(ITestOutputHelper log) : ConfigurationBaseTest<JsonOneFileConfiguration>
public class JsonOneFileConfigurationTest(ITestOutputHelper log)
: ConfigurationBaseTest<JsonOneFileConfiguration>(log)
{
private readonly IFileSystem _fileSystem = new MockFileSystem();

Expand All @@ -24,7 +25,7 @@ protected override IDisposable CreateForTest(out JsonOneFileConfiguration config
filePath,
true,
null,
logger:new TestLogger(log,TimeProvider.System, "JSON_ONE_FILE"),
logger:LogFactory.CreateLogger("JSON_ONE_FILE"),
fileSystem: _fileSystem
);

Expand Down
4 changes: 2 additions & 2 deletions src/Asv.Cfg.Test/Json/ZipJsonConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Asv.Cfg.Test
{
[TestSubject(typeof(ZipJsonConfiguration))]
public class ZipJsonConfigurationTest(ITestOutputHelper log)
: ConfigurationBaseTest<ZipJsonConfiguration>
: ConfigurationBaseTest<ZipJsonConfiguration>(log)
{
private readonly IFileSystem _fileSystem = new MockFileSystem();

Expand All @@ -33,7 +33,7 @@ protected override IDisposable CreateForTest(out ZipJsonConfiguration configurat
_fileSystem.Directory.CreateDirectory(dir ?? throw new InvalidOperationException());
}
var file = _fileSystem.File.Open(filePath, FileMode.OpenOrCreate);
configuration = new ZipJsonConfiguration(file, true, new TestLogger(log,TimeProvider.System, "ZIP_JSON"));
configuration = new ZipJsonConfiguration(file, true, logger:LogFactory.CreateLogger("ZIP_JSON"));
var cfg = configuration;
return Disposable.Create(() =>
{
Expand Down
9 changes: 7 additions & 2 deletions src/Asv.Cfg.Test/Json/ZipJsonVersionedFileTest.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
using System;
using System.IO;
using Asv.Common;
using JetBrains.Annotations;
using R3;
using Xunit.Abstractions;

namespace Asv.Cfg.Test;

[TestSubject(typeof(ZipJsonVersionedFile))]
public class ZipJsonVersionedFileTest:ConfigurationBaseTest<ZipJsonVersionedFile>
public class ZipJsonVersionedFileTest(ITestOutputHelper log):ConfigurationBaseTest<ZipJsonVersionedFile>(log)
{


protected override IDisposable CreateForTest(out ZipJsonVersionedFile configuration)
{
throw new NotImplementedException();
configuration = new ZipJsonVersionedFile(new MemoryStream(), new SemVersion(1,0),"test",true, false, LogFactory.CreateLogger("ZIP_VERSION_JSON_CONFIG"));
return Disposable.Empty;
}
}
1 change: 1 addition & 0 deletions src/Asv.Cfg/IConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static void Remove<TPocoType>(this IConfiguration src)

public interface IConfiguration:IDisposable
{
IEnumerable<string> ReservedParts { get; }
IEnumerable<string> AvailableParts { get; }
bool Exist(string key);
TPocoType Get<TPocoType>(string key, Lazy<TPocoType> defaultValue);
Expand Down
1 change: 1 addition & 0 deletions src/Asv.Cfg/InMemory/InMemoryConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void Dispose()
_onError.Dispose();
}

public IEnumerable<string> ReservedParts => Array.Empty<string>();
public IEnumerable<string> AvailableParts => GetParts();

private IEnumerable<string> GetParts()
Expand Down
4 changes: 3 additions & 1 deletion src/Asv.Cfg/Json/JsonConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ private string GetFilePath(string key)
{
return _fileSystem.Path.Combine(_folderPath, $"{key}.json");
}


public IEnumerable<string> ReservedParts => Array.Empty<string>();

public IEnumerable<string> AvailableParts
{
get
Expand Down
1 change: 1 addition & 0 deletions src/Asv.Cfg/Json/JsonOneFileConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ private void InternalSaveChanges(Unit unit)
}
}

public IEnumerable<string> ReservedParts => Array.Empty<string>();
public IEnumerable<string> AvailableParts => _values.Keys;

public bool Exist(string key)
Expand Down
1 change: 1 addition & 0 deletions src/Asv.Cfg/Json/ZipJsonConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public IEnumerable<string> AvailableParts
}
}

public virtual IEnumerable<string> ReservedParts => Array.Empty<string>();

public bool Exist(string key)
{
Expand Down
17 changes: 14 additions & 3 deletions src/Asv.Cfg/Json/ZipJsonVersionedFile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using Asv.Common;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace Asv.Cfg
Expand Down Expand Up @@ -43,11 +45,19 @@ public interface IVersionedFile:IConfiguration
SemVersion FileVersion { get; }
}

public abstract class ZipJsonVersionedFile: ZipJsonConfiguration,IVersionedFile
public class ZipJsonVersionedFile: ZipJsonConfiguration,IVersionedFile
{
private readonly SemVersion _version;
private const string InfoKey = "FileInfo";
protected ZipJsonVersionedFile(Stream zipStream,SemVersion lastVersion, string fileType, bool createIfNotExist):base(zipStream)

public ZipJsonVersionedFile(
Stream zipStream,
SemVersion lastVersion,
string fileType,
bool createIfNotExist,
bool leaveOpen = false,
ILogger? logger = null )
:base(zipStream, leaveOpen,logger)
{
var info = Get(InfoKey, new Lazy<ZipJsonFileInfo>(ZipJsonFileInfo.Empty));
string type;
Expand Down Expand Up @@ -82,7 +92,8 @@ protected ZipJsonVersionedFile(Stream zipStream,SemVersion lastVersion, string f
throw new Exception($"Unsupported file type. (Want '{fileType}', got '{type}')");
}
}


public override IEnumerable<string> ReservedParts => [InfoKey];
public SemVersion FileVersion => _version;
}
}
2 changes: 1 addition & 1 deletion src/Asv.IO.Test/Streams/TelnetStreamTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public TelnetStreamTest(ITestOutputHelper output)
{
_output = output;
}
[Fact]
[Fact(Skip = "Manual test")]
public async Task ReadWriteMessageTest()
{
var message1 = "Ping";
Expand Down

0 comments on commit 3d4f5e6

Please sign in to comment.