1
+ using System ;
2
+ using System . Linq ;
3
+ using System . Threading . Tasks ;
4
+ using RedditSharp ;
5
+ using RedditSharp . Things ;
6
+ using Retry ;
7
+ using Xunit ;
8
+
9
+ namespace RedditSharpTests . Collections
10
+ {
11
+ [ Collection ( "AuthenticatedTests" ) ]
12
+ public class CollectionTests
13
+ {
14
+ private readonly Reddit _reddit ;
15
+ private readonly string _subredditName ;
16
+
17
+ public CollectionTests ( AuthenticatedTestsFixture authenticatedFixture )
18
+ {
19
+ var authFixture = authenticatedFixture ;
20
+ var agent = new WebAgent ( authFixture . AccessToken ) ;
21
+ _reddit = new Reddit ( agent , true ) ;
22
+ _subredditName = authFixture . Config [ "TestSubreddit" ] ;
23
+ }
24
+
25
+ [ SkippableFact ]
26
+ public async Task CreatingACollection ( )
27
+ {
28
+ var guid = GenerateGuid ( ) ;
29
+ var currentDate = DateTime . UtcNow . AddMinutes ( - 2 ) ;
30
+
31
+ var sub = await _reddit . GetSubredditAsync ( _subredditName ) ;
32
+ SkipIfNotModerator ( sub ) ;
33
+
34
+ var title = $ "A collection with no posts { guid } ";
35
+ var description = $ "Collection description { GenerateGuid ( ) } ";
36
+
37
+ var result = await sub . CreateCollectionAsync ( title , description ) ;
38
+
39
+ Assert . Equal ( description , result . Description ) ;
40
+ Assert . Equal ( title , result . Title ) ;
41
+ Assert . Equal ( sub . FullName , result . SubredditId ) ;
42
+ Assert . True ( result . CreatedAtUtc >= currentDate ) ;
43
+ Assert . True ( result . LastUpdateUtc >= currentDate ) ;
44
+
45
+ var collections = await sub . GetCollectionsAsync ( ) ;
46
+ Assert . True ( collections . Count >= 1 , "there should be at least one collection" ) ;
47
+ var collection = collections . FirstOrDefault ( x => x . CollectionId == result . CollectionId ) ;
48
+ Assert . NotNull ( collection ) ;
49
+
50
+ await _reddit . DeleteCollectionAsync ( collection . CollectionId ) ;
51
+ }
52
+
53
+ [ SkippableFact ]
54
+ public async Task CreatingACollectionAndAddingPosts ( )
55
+ {
56
+ var post1Guid = GenerateGuid ( ) ;
57
+ var post2Guid = GenerateGuid ( ) ;
58
+ var title = $ "Collection of { post1Guid } and { post2Guid } ";
59
+ var description = $ "Awesome new collection { GenerateGuid ( ) } ";
60
+
61
+ var sub = await _reddit . GetSubredditAsync ( _subredditName ) ;
62
+ SkipIfNotModerator ( sub ) ;
63
+
64
+ var post1Task = sub . SubmitPostAsync ( $ "Post { post1Guid } ", "https://github.com/CrustyJew/RedditSharp" , resubmit : true ) ;
65
+ var post2Task = sub . SubmitTextPostAsync ( $ "Post { post2Guid } ", $ "Post { post2Guid } ") ;
66
+
67
+ var createCollectionTask = sub . CreateCollectionAsync ( title , description ) ;
68
+
69
+ var post1 = await post1Task ;
70
+ var post2 = await post2Task ;
71
+ var collectionResult = await createCollectionTask ;
72
+
73
+ Assert . NotNull ( post1 ) ;
74
+ Assert . NotNull ( post2 ) ;
75
+
76
+ var addPost1Task = collectionResult . AddPostAsync ( post1 . FullName ) ;
77
+ var addPost2Task = collectionResult . AddPostAsync ( post2 . FullName ) ;
78
+
79
+ await addPost1Task ;
80
+ await addPost2Task ;
81
+
82
+ var collection = await RetryHelper . Instance
83
+ . Try ( ( ) => _reddit . GetCollectionAsync ( collectionResult . CollectionId ) )
84
+ . WithTryInterval ( TimeSpan . FromSeconds ( 0.5 ) )
85
+ . WithMaxTryCount ( 10 )
86
+ . Until ( c => c . LinkIds . Length > 1 ) ;
87
+
88
+ Assert . Equal ( 2 , collection . LinkIds . Length ) ;
89
+ Assert . Contains ( post1 . FullName , collection . LinkIds ) ;
90
+ Assert . Contains ( post2 . FullName , collection . LinkIds ) ;
91
+
92
+ Assert . Equal ( 2 , collection . Posts . Length ) ;
93
+
94
+ var collectionWithLinkContent = await _reddit . GetCollectionAsync ( collectionResult . CollectionId , includePostsContent : false ) ;
95
+
96
+ Assert . Empty ( collectionWithLinkContent . Posts ) ;
97
+
98
+ await _reddit . DeleteCollectionAsync ( collection . CollectionId ) ;
99
+ }
100
+
101
+ [ Fact ]
102
+ public async Task DeletingANonExistentCollection ( )
103
+ {
104
+ var exception = await Assert . ThrowsAsync < RedditException > ( ( ) => _reddit . DeleteCollectionAsync ( "00000000-0000-0000-1111-111111111111" ) ) ;
105
+ Assert . Contains ( exception . Errors , error => error [ 0 ] . ToString ( ) . Equals ( "INVALID_COLLECTION_ID" ) ) ;
106
+ }
107
+
108
+ private void SkipIfNotModerator ( Subreddit sub )
109
+ {
110
+ Skip . If ( sub . UserIsModerator != true , $ "User isn't a moderator of ${ _subredditName } so a collection cannot be made.") ;
111
+ }
112
+
113
+ private static string GenerateGuid ( )
114
+ {
115
+ return Guid . NewGuid ( ) . ToString ( "N" ) . Substring ( 0 , 5 ) ;
116
+ }
117
+ }
118
+ }
0 commit comments