Skip to content

Commit

Permalink
Select a post from data provided, using offset
Browse files Browse the repository at this point in the history
  • Loading branch information
instantiator committed Oct 22, 2023
1 parent 87a41e3 commit 3a9630c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class PostOptions
[Option('s', "source-file", Required = false, HelpText = "Path or url to a source file containing posts.")]
public string? DataPath { get; set; }

[Option('o', "offset", Required = false, HelpText = "Offset (index) of a post within the source file to use. Leave blank to send all posts in the source file.")]
public int? Offset { get; set; }

[Option('f', "filter", Required = false, HelpText = "Regular expression filter for network short codes.")]
public string? Filter { get; set; } = null;

Expand Down Expand Up @@ -132,7 +135,10 @@ public static int ExecutePost(PostOptions options)
{
var postListJson = UriReader.ReadStringAsync(dataPath).Result;
var postList = new PostListReader().ReadJson(postListJson);
var messages = postList!.ToSocialMessages();
var messages = options.Offset == null
? postList!.ToSocialMessages()
: new[] { postList!.ToSocialMessages().ElementAt(options.Offset.Value) };

var resultSets = new List<IEnumerable<PostResult>>();
foreach (var message in messages)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ public class InputFileTests
private static string CreateTempFile(string data)
{
var tempPath = Path.GetFullPath(Path.Combine(Path.GetTempPath(), Path.GetTempFileName()));
// Console.WriteLine("Creating temporary file...");
// Console.WriteLine($"Data: {data}");
// Console.WriteLine($"Path: {tempPath}");
File.WriteAllText(tempPath, data);
return tempPath;
}
Expand All @@ -25,6 +22,14 @@ private static string CreateEmptyPostList()
private static string CreateSimplePostListFile()
=> CreateTempFile(@"{ ""posts"": [ { ""parts"": [ { ""part"": ""Text"", ""content"" : { ""any"": ""Hello"" } } ] } ] }");

private static string CreateSimpleMultiPostListFile()
=> CreateTempFile(
@"{ ""posts"": [
{ ""parts"": [ { ""part"": ""Text"", ""content"" : { ""any"": ""First post"" } } ] },
{ ""parts"": [ { ""part"": ""Text"", ""content"" : { ""any"": ""Second post"" } } ] },
{ ""parts"": [ { ""part"": ""Text"", ""content"" : { ""any"": ""Third post"" } } ] },
] }");

[Fact]
public void RejectsNoParameters()
{
Expand Down Expand Up @@ -88,5 +93,42 @@ public void AcceptsSimplePostList()
Assert.True(DistributionCLI.LastPostResults!.Single().Success);
Assert.Equal("Hello", DistributionCLI.LastPostResults!.Single().Message.Parts.Single().Content[NetworkType.Any]);
}

}

[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
public void AcceptsSimplePostListWithOffset(int offset)
{
DistributionCLI.Reset();
var configPath = CreateSimpleConfigFile();
var postsPath = CreateSimpleMultiPostListFile();

var inflated = new PostListReader().ReadFile(postsPath);
Assert.NotNull(inflated);
Assert.Equal(3, inflated!.Posts.Count());

int status = DistributionCLI.Main("post", "-c", configPath, "-s", postsPath, "-o", offset.ToString());
Assert.Equal(0, status);
Assert.Single(DistributionCLI.LastPostResults!);
Assert.True(DistributionCLI.LastPostResults!.Single().Success);

string expectation;
switch (offset)
{
case 0:
expectation = "First post";
break;
case 1:
expectation = "Second post";
break;
case 2:
expectation = "Third post";
break;
default:
throw new NotImplementedException();
};

Assert.Equal(expectation, DistributionCLI.LastPostResults!.Single().Message.Parts.Single().Content[NetworkType.Any]);
}
}

0 comments on commit 3a9630c

Please sign in to comment.