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

CreatePostsFromJsonメソッドからPostClassの生成に関係のない処理を分離 #324

Merged
merged 4 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 16 additions & 16 deletions OpenTween.Tests/Api/GraphQL/TimelineTweetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public void ToStatus_WithTwitterPostFactory_SimpleTweet_Test()
var rootElm = this.LoadResponseDocument("TimelineTweet_SimpleTweet.json");
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);

Assert.Equal("1613784711020826626", post.StatusId.Id);
Assert.Equal(40480664L, post.UserId);
Expand All @@ -86,8 +86,8 @@ public void ToStatus_WithTwitterPostFactory_TweetWithMedia_Test()
var rootElm = this.LoadResponseDocument("TimelineTweet_TweetWithMedia.json");
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);

Assert.Equal("1614587968567783424", post.StatusId.Id);
Assert.Equal(40480664L, post.UserId);
Expand All @@ -104,8 +104,8 @@ public void ToStatus_WithTwitterPostFactory_RetweetedTweet_Test()
var rootElm = this.LoadResponseDocument("TimelineTweet_RetweetedTweet.json");
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);

Assert.Equal("1617128268548964354", post.StatusId.Id);
Assert.Equal(40480664L, post.RetweetedByUserId);
Expand All @@ -119,8 +119,8 @@ public void ToStatus_WithTwitterPostFactory_TweetWithVisibility_Test()
var rootElm = this.LoadResponseDocument("TimelineTweet_TweetWithVisibility.json");
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);

Assert.Equal("1602775353088524288", post.StatusId.Id);
Assert.Equal(357750891L, post.UserId);
Expand All @@ -132,8 +132,8 @@ public void ToStatus_WithTwitterPostFactory_SelfThread_Test()
var rootElm = this.LoadResponseDocument("TimelineTweet_SelfThread.json");
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);

Assert.Equal("1511751702684499968", post.StatusId.Id);
Assert.Equal(40480664L, post.UserId);
Expand All @@ -145,8 +145,8 @@ public void ToStatus_WithTwitterPostFactory_QuotedTweet_Test()
var rootElm = this.LoadResponseDocument("TimelineTweet_QuotedTweet.json");
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);

Assert.Equal("1588614645866147840", post.StatusId.Id);
var quotedPostId = Assert.Single(post.QuoteStatusIds);
Expand All @@ -159,8 +159,8 @@ public void ToStatus_WithTwitterPostFactory_QuotedTweet_Tombstone_Test()
var rootElm = this.LoadResponseDocument("TimelineTweet_QuotedTweet_Tombstone.json");
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);

Assert.Equal("1614653321310253057", post.StatusId.Id);
var quotedPostId = Assert.Single(post.QuoteStatusIds);
Expand All @@ -173,8 +173,8 @@ public void ToStatus_WithTwitterPostFactory_PromotedTweet_Test()
var rootElm = this.LoadResponseDocument("TimelineTweet_PromotedTweet.json");
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>());
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);

Assert.Equal("1674737917363888129", post.StatusId.Id);
Assert.Equal(2941313791L, post.UserId);
Expand Down
118 changes: 92 additions & 26 deletions OpenTween.Tests/Models/TwitterPostFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ private TwitterUser CreateUser()
[Fact]
public void CreateFromStatus_Test()
{
var factory = new TwitterPostFactory(this.CreateTabinfo());
var factory = new TwitterPostFactory(this.CreateTabinfo(), new());
var status = this.CreateStatus();
var post = factory.CreateFromStatus(status, selfUserId: 20000L, followerIds: EmptyIdSet);
var post = factory.CreateFromStatus(status, selfUserId: 20000L, followerIds: EmptyIdSet, firstLoad: false);

Assert.Equal(new TwitterStatusId(status.IdStr), post.StatusId);
Assert.Equal(new DateTimeUtc(2022, 1, 1, 0, 0, 0), post.CreatedAt);
Expand Down Expand Up @@ -114,52 +114,54 @@ public void CreateFromStatus_Test()
Assert.False(post.IsProtect);
Assert.False(post.IsOwl);
Assert.False(post.IsMe);

Assert.False(post.IsRead);
}

[Fact]
public void CreateFromStatus_AuthorTest()
{
var factory = new TwitterPostFactory(this.CreateTabinfo());
var factory = new TwitterPostFactory(this.CreateTabinfo(), new());
var status = this.CreateStatus();
var selfUserId = status.User.Id;
var post = factory.CreateFromStatus(status, selfUserId, followerIds: EmptyIdSet);
var post = factory.CreateFromStatus(status, selfUserId, followerIds: EmptyIdSet, firstLoad: false);

Assert.True(post.IsMe);
}

[Fact]
public void CreateFromStatus_FollowerTest()
{
var factory = new TwitterPostFactory(this.CreateTabinfo());
var factory = new TwitterPostFactory(this.CreateTabinfo(), new());
var status = this.CreateStatus();
var followerIds = new HashSet<long> { status.User.Id };
var post = factory.CreateFromStatus(status, selfUserId: 20000L, followerIds);
var post = factory.CreateFromStatus(status, selfUserId: 20000L, followerIds, firstLoad: false);

Assert.False(post.IsOwl);
}

[Fact]
public void CreateFromStatus_NotFollowerTest()
{
var factory = new TwitterPostFactory(this.CreateTabinfo());
var factory = new TwitterPostFactory(this.CreateTabinfo(), new());
var status = this.CreateStatus();
var followerIds = new HashSet<long> { 30000L };
var post = factory.CreateFromStatus(status, selfUserId: 20000L, followerIds);
var post = factory.CreateFromStatus(status, selfUserId: 20000L, followerIds, firstLoad: false);

Assert.True(post.IsOwl);
}

[Fact]
public void CreateFromStatus_RetweetTest()
{
var factory = new TwitterPostFactory(this.CreateTabinfo());
var factory = new TwitterPostFactory(this.CreateTabinfo(), new());
var originalStatus = this.CreateStatus();

var retweetStatus = this.CreateStatus();
retweetStatus.RetweetedStatus = originalStatus;
retweetStatus.Source = """<a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>""";

var post = factory.CreateFromStatus(retweetStatus, selfUserId: 20000L, followerIds: EmptyIdSet);
var post = factory.CreateFromStatus(retweetStatus, selfUserId: 20000L, followerIds: EmptyIdSet, firstLoad: false);

Assert.Equal(new TwitterStatusId(retweetStatus.IdStr), post.StatusId);
Assert.Equal(retweetStatus.User.Id, post.RetweetedByUserId);
Expand All @@ -170,6 +172,68 @@ public void CreateFromStatus_RetweetTest()
Assert.Equal("https://www.opentween.org/", post.SourceUri?.OriginalString);
}

[Fact]
public void CreateFromStatus_FirstLoadNotUnreadTest()
{
var settingCommon = new SettingCommon
{
Read = true, // 起動時の読み込み分を既読扱いにする
};

var factory = new TwitterPostFactory(this.CreateTabinfo(), settingCommon);
var status = this.CreateStatus();
var post = factory.CreateFromStatus(status, selfUserId: 20000L, followerIds: EmptyIdSet, firstLoad: true);

Assert.True(post.IsRead); // 既読
}

[Fact]
public void CreateFromStatus_FirstLoadUnreadTest()
{
var settingCommon = new SettingCommon
{
Read = false, // 起動時の読み込み分を既読扱いにしない
};

var factory = new TwitterPostFactory(this.CreateTabinfo(), settingCommon);
var status = this.CreateStatus();
var post = factory.CreateFromStatus(status, selfUserId: 20000L, followerIds: EmptyIdSet, firstLoad: true);

Assert.False(post.IsRead); // 未読
}

[Fact]
public void CreateFromStatus_SelfPostUnreadTest()
{
var settingCommon = new SettingCommon
{
ReadOwnPost = false, // 自分自身の発言を既読扱いにしない
};

var factory = new TwitterPostFactory(this.CreateTabinfo(), settingCommon);
var status = this.CreateStatus();
status.User.Id = 20000L;
var post = factory.CreateFromStatus(status, selfUserId: 20000L, followerIds: EmptyIdSet, firstLoad: false);

Assert.False(post.IsRead); // 未読
}

[Fact]
public void CreateFromStatus_SelfPostNotUnreadTest()
{
var settingCommon = new SettingCommon
{
ReadOwnPost = true, // 自分自身の発言を既読扱いにする
};

var factory = new TwitterPostFactory(this.CreateTabinfo(), settingCommon);
var status = this.CreateStatus();
status.User.Id = 20000L;
var post = factory.CreateFromStatus(status, selfUserId: 20000L, followerIds: EmptyIdSet, firstLoad: false);

Assert.True(post.IsRead); // 既読
}

private TwitterMessageEvent CreateDirectMessage(string senderId, string recipientId)
{
var messageId = this.random.Next(10000);
Expand Down Expand Up @@ -212,7 +276,7 @@ private TwitterMessageEvent CreateDirectMessage(string senderId, string recipien
[Fact]
public void CreateFromDirectMessageEvent_Test()
{
var factory = new TwitterPostFactory(this.CreateTabinfo());
var factory = new TwitterPostFactory(this.CreateTabinfo(), new());

var selfUser = this.CreateUser();
var otherUser = this.CreateUser();
Expand All @@ -223,7 +287,7 @@ public void CreateFromDirectMessageEvent_Test()
[otherUser.IdStr] = otherUser,
};
var apps = this.CreateApps();
var post = factory.CreateFromDirectMessageEvent(eventItem, users, apps, selfUserId: selfUser.Id);
var post = factory.CreateFromDirectMessageEvent(eventItem, users, apps, selfUserId: selfUser.Id, firstLoad: false);

Assert.Equal(new TwitterDirectMessageId(eventItem.Id), post.StatusId);
Assert.Equal(new DateTimeUtc(2022, 1, 1, 0, 0, 0), post.CreatedAt);
Expand Down Expand Up @@ -263,12 +327,14 @@ public void CreateFromDirectMessageEvent_Test()
Assert.False(post.IsProtect);
Assert.True(post.IsOwl);
Assert.False(post.IsMe);

Assert.False(post.IsRead);
}

[Fact]
public void CreateFromDirectMessageEvent_SenderTest()
{
var factory = new TwitterPostFactory(this.CreateTabinfo());
var factory = new TwitterPostFactory(this.CreateTabinfo(), new());

var selfUser = this.CreateUser();
var otherUser = this.CreateUser();
Expand All @@ -279,7 +345,7 @@ public void CreateFromDirectMessageEvent_SenderTest()
[otherUser.IdStr] = otherUser,
};
var apps = this.CreateApps();
var post = factory.CreateFromDirectMessageEvent(eventItem, users, apps, selfUserId: selfUser.Id);
var post = factory.CreateFromDirectMessageEvent(eventItem, users, apps, selfUserId: selfUser.Id, firstLoad: false);

Assert.Equal(otherUser.Id, post.UserId);
Assert.False(post.IsOwl);
Expand All @@ -289,7 +355,7 @@ public void CreateFromDirectMessageEvent_SenderTest()
[Fact]
public void GetReceivedHashtags_Test()
{
var factory = new TwitterPostFactory(this.CreateTabinfo());
var factory = new TwitterPostFactory(this.CreateTabinfo(), new());
var status = this.CreateStatus();
status.FullText = "hoge #OpenTween";
status.Entities.Hashtags = new[]
Expand All @@ -301,7 +367,7 @@ public void GetReceivedHashtags_Test()
},
};

_ = factory.CreateFromStatus(status, selfUserId: 20000L, followerIds: EmptyIdSet);
_ = factory.CreateFromStatus(status, selfUserId: 20000L, followerIds: EmptyIdSet, firstLoad: false);

Assert.Equal(new[] { "#OpenTween" }, factory.GetReceivedHashtags());
Assert.Empty(factory.GetReceivedHashtags());
Expand All @@ -310,7 +376,7 @@ public void GetReceivedHashtags_Test()
[Fact]
public void CreateFromStatus_MediaAltTest()
{
var factory = new TwitterPostFactory(this.CreateTabinfo());
var factory = new TwitterPostFactory(this.CreateTabinfo(), new());

var status = this.CreateStatus();
status.FullText = "https://t.co/hoge";
Expand All @@ -329,7 +395,7 @@ public void CreateFromStatus_MediaAltTest()
},
};

var post = factory.CreateFromStatus(status, selfUserId: 100L, followerIds: EmptyIdSet);
var post = factory.CreateFromStatus(status, selfUserId: 100L, followerIds: EmptyIdSet, firstLoad: false);

var accessibleText = string.Format(Properties.Resources.ImageAltText, "代替テキスト");
Assert.Equal(accessibleText, post.AccessibleText);
Expand All @@ -341,7 +407,7 @@ public void CreateFromStatus_MediaAltTest()
[Fact]
public void CreateFromStatus_MediaNoAltTest()
{
var factory = new TwitterPostFactory(this.CreateTabinfo());
var factory = new TwitterPostFactory(this.CreateTabinfo(), new());

var status = this.CreateStatus();
status.FullText = "https://t.co/hoge";
Expand All @@ -360,7 +426,7 @@ public void CreateFromStatus_MediaNoAltTest()
},
};

var post = factory.CreateFromStatus(status, selfUserId: 100L, followerIds: EmptyIdSet);
var post = factory.CreateFromStatus(status, selfUserId: 100L, followerIds: EmptyIdSet, firstLoad: false);

Assert.Equal("pic.twitter.com/hoge", post.AccessibleText);
Assert.Equal("""<a href="https://t.co/hoge" title="https://twitter.com/hoge/status/1234567890/photo/1">pic.twitter.com/hoge</a>""", post.Text);
Expand All @@ -371,7 +437,7 @@ public void CreateFromStatus_MediaNoAltTest()
[Fact]
public void CreateFromStatus_QuotedUrlTest()
{
var factory = new TwitterPostFactory(this.CreateTabinfo());
var factory = new TwitterPostFactory(this.CreateTabinfo(), new());

var status = this.CreateStatus();
status.FullText = "https://t.co/hoge";
Expand Down Expand Up @@ -401,7 +467,7 @@ public void CreateFromStatus_QuotedUrlTest()
FullText = "test",
};

var post = factory.CreateFromStatus(status, selfUserId: 100L, followerIds: EmptyIdSet);
var post = factory.CreateFromStatus(status, selfUserId: 100L, followerIds: EmptyIdSet, firstLoad: false);

var accessibleText = string.Format(Properties.Resources.QuoteStatus_AccessibleText, "foo", "test");
Assert.Equal(accessibleText, post.AccessibleText);
Expand All @@ -413,7 +479,7 @@ public void CreateFromStatus_QuotedUrlTest()
[Fact]
public void CreateFromStatus_QuotedUrlWithPermelinkTest()
{
var factory = new TwitterPostFactory(this.CreateTabinfo());
var factory = new TwitterPostFactory(this.CreateTabinfo(), new());

var status = this.CreateStatus();
status.FullText = "hoge";
Expand All @@ -436,7 +502,7 @@ public void CreateFromStatus_QuotedUrlWithPermelinkTest()
Expanded = "https://twitter.com/hoge/status/1234567890",
};

var post = factory.CreateFromStatus(status, selfUserId: 100L, followerIds: EmptyIdSet);
var post = factory.CreateFromStatus(status, selfUserId: 100L, followerIds: EmptyIdSet, firstLoad: false);

var accessibleText = "hoge " + string.Format(Properties.Resources.QuoteStatus_AccessibleText, "foo", "test");
Assert.Equal(accessibleText, post.AccessibleText);
Expand All @@ -448,7 +514,7 @@ public void CreateFromStatus_QuotedUrlWithPermelinkTest()
[Fact]
public void CreateFromStatus_QuotedUrlNoReferenceTest()
{
var factory = new TwitterPostFactory(this.CreateTabinfo());
var factory = new TwitterPostFactory(this.CreateTabinfo(), new());

var status = this.CreateStatus();
status.FullText = "https://t.co/hoge";
Expand All @@ -467,7 +533,7 @@ public void CreateFromStatus_QuotedUrlNoReferenceTest()
};
status.QuotedStatus = null;

var post = factory.CreateFromStatus(status, selfUserId: 100L, followerIds: EmptyIdSet);
var post = factory.CreateFromStatus(status, selfUserId: 100L, followerIds: EmptyIdSet, firstLoad: false);

var accessibleText = "twitter.com/hoge/status/1…";
Assert.Equal(accessibleText, post.AccessibleText);
Expand Down
Loading
Loading