PageRenderTime 4ms CodeModel.GetById 4ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/TwitterFeedApplication/TwitterFeedManager.Tests/TwitterFeedEngineTests.cs

https://bitbucket.org/nesanpather/twitterfeedrepo
C# | 478 lines | 424 code | 45 blank | 9 comment | 0 complexity | 237226b4f1e92b5fb3c5a43c48ac6897 MD5 | raw file
  1using System.Collections.Generic;
  2using System.Linq;
  3using System.Threading.Tasks;
  4using DataManager.Interfaces;
  5using NSubstitute;
  6using NUnit.Framework;
  7using TwitterFeedManager.Interfaces;
  8using TwitterFeedModels;
  9
 10namespace TwitterFeedManager.Tests
 11{
 12    [TestFixture]
 13    public class TwitterFeedEngineTests
 14    {
 15        [Test]
 16        public async Task GetTwitterFeedAsync_Given_ValidInput1_ValidInput2_3Users_3Tweets_Returns_ValidIEnumerableTwitterFeed_Count2()
 17        {
 18            // Arrange.
 19            var dataManager = Substitute.For<IDataManager>();
 20            var twitterUsersBuilder = Substitute.For<ITwitterUsersBuilder>();
 21            var twitterFeedBuilder = Substitute.For<ITweetBuilder>();
 22
 23            dataManager.GetTwitterUsersAsync(Arg.Any<string>())
 24                .Returns(info => Task.FromResult<IEnumerable<TwitterUser>>(
 25                    new List<TwitterUser>
 26                    {
 27                        new TwitterUser
 28                        {
 29                            User = new User {UserName = "Ward"},
 30                            Followers = new List<User>
 31                            {
 32                                new User {UserName = "Alan"}
 33                            }
 34                        },
 35                        new TwitterUser
 36                        {
 37                            User = new User {UserName = "Alan"},
 38                            Followers = new List<User>
 39                            {
 40                                new User {UserName = "Martin"}
 41                            }
 42                        },
 43                        new TwitterUser
 44                        {
 45                            User = new User {UserName = "Ward"},
 46                            Followers = new List<User>
 47                            {
 48                                new User {UserName = "Martin"},
 49                                new User {UserName = "Alan"}
 50                            }
 51                        }
 52                    }));
 53
 54            dataManager.GetTweetsAsync(Arg.Any<string>())
 55                .Returns(info => Task.FromResult<IEnumerable<Tweet>>(new List<Tweet>
 56                {
 57                    new Tweet
 58                    {
 59                        Id = 1,
 60                        TwitterUser = new User {UserName = "Alan"},
 61                        Message = "If you have a procedure with 10 parameters, you probably missed some."
 62                    },
 63                    new Tweet
 64                    {
 65                        Id = 2,
 66                        TwitterUser = new User {UserName = "Ward"},
 67                        Message = "There are only two hard things in Computer Science: cache invalidation, naming things and off-by-1 errors."
 68                    },
 69                    new Tweet
 70                    {
 71                        Id = 3,
 72                        TwitterUser = new User {UserName = "Alan"},
 73                        Message = "Random numbers should not be generated with a method chosen at random."
 74                    }
 75                }));
 76
 77            twitterUsersBuilder.UnionDuplicateUsers(Arg.Any<IEnumerable<TwitterUser>>())
 78                .Returns(info => new List<TwitterUser>
 79                {
 80                    new TwitterUser
 81                    {
 82                        User = new User {UserName = "Ward"},
 83                        Followers = new List<User>
 84                        {
 85                            new User {UserName = "Alan"},
 86                            new User {UserName = "Martin"}
 87                        }
 88                    },
 89                    new TwitterUser
 90                    {
 91                        User = new User {UserName = "Alan"},
 92                        Followers = new List<User>
 93                        {
 94                            new User {UserName = "Martin"}
 95                        }
 96                    }
 97                });
 98
 99            twitterUsersBuilder.SortUsers(Arg.Any<IEnumerable<TwitterUser>>())
100                .Returns(info => new List<TwitterUser>
101                {
102                    new TwitterUser
103                    {
104                        User = new User {UserName = "Alan"},
105                        Followers = new List<User>
106                        {
107                            new User {UserName = "Martin"}
108                        }
109                    },
110                    new TwitterUser
111                    {
112                        User = new User {UserName = "Ward"},
113                        Followers = new List<User>
114                        {
115                            new User {UserName = "Alan"},
116                            new User {UserName = "Martin"}
117                        }
118                    }
119                });
120
121            twitterUsersBuilder.SortFollowers(Arg.Any<IEnumerable<TwitterUser>>())
122                .Returns(info => new List<TwitterUser>
123                {
124                    new TwitterUser
125                    {
126                        User = new User {UserName = "Alan"},
127                        Followers = new List<User>
128                        {
129                            new User {UserName = "Martin"}
130                        }
131                    },
132                    new TwitterUser
133                    {
134                        User = new User {UserName = "Ward"},
135                        Followers = new List<User>
136                        {
137                            new User {UserName = "Alan"},
138                            new User {UserName = "Martin"}
139                        }
140                    }
141                });
142
143            twitterFeedBuilder.SortTweets(Arg.Any<IEnumerable<TwitterFeed>>()).Returns(info => new List<TwitterFeed>
144            {
145               new TwitterFeed
146               {
147                   User = new User {UserName = "Alan"},
148                   Followers = new List<User>
149                   {
150                       new User {UserName = "Martin"}
151                   },
152                   Tweets = new List<Tweet>
153                   {
154                       new Tweet
155                       {
156                           Id = 1,
157                           TwitterUser = new User {UserName = "Alan"},
158                           Message = "If you have a procedure with 10 parameters, you probably missed some."
159                       },
160                       new Tweet
161                       {
162                           Id = 3,
163                           TwitterUser = new User {UserName = "Alan"},
164                           Message = "Random numbers should not be generated with a method chosen at random."
165                       }
166                   }
167               },
168                new TwitterFeed
169                {
170                    User = new User {UserName = "Ward"},
171                    Followers = new List<User>
172                    {
173                        new User {UserName = "Alan"},
174                        new User {UserName = "Martin"}
175                    },
176                    Tweets = new List<Tweet>
177                    {
178                        new Tweet
179                        {
180                            Id = 1,
181                            TwitterUser = new User {UserName = "Alan"},
182                            Message = "If you have a procedure with 10 parameters, you probably missed some."
183                        },
184                        new Tweet
185                        {
186                            Id = 2,
187                            TwitterUser = new User {UserName = "Ward"},
188                            Message = "There are only two hard things in Computer Science: cache invalidation, naming things and off-by-1 errors."
189                        },
190                        new Tweet
191                        {
192                            Id = 3,
193                            TwitterUser = new User {UserName = "Alan"},
194                            Message = "Random numbers should not be generated with a method chosen at random."
195                        }
196                    }
197                }
198            });
199
200            var twitterFeedEngine = new TwitterFeedEngine(dataManager, twitterUsersBuilder, twitterFeedBuilder);
201
202            // Act.
203            var actual = await twitterFeedEngine.GetTwitterFeedAsync("test1.txt", "test2.txt");
204
205            // Assert.
206            var twitterFeeds = actual as IList<TwitterFeed> ?? actual.ToList();
207
208            Assert.AreEqual(2, twitterFeeds.Count);
209            Assert.AreEqual("Alan", twitterFeeds.ToList()[0].User.UserName);
210
211            Assert.AreEqual("Alan", twitterFeeds.ToList()[0].Tweets.ToList()[0].TwitterUser.UserName);
212            Assert.AreEqual("If you have a procedure with 10 parameters, you probably missed some.", twitterFeeds.ToList()[0].Tweets.ToList()[0].Message);
213            Assert.AreEqual("Alan", twitterFeeds.ToList()[0].Tweets.ToList()[1].TwitterUser.UserName);
214            Assert.AreEqual("Random numbers should not be generated with a method chosen at random.", twitterFeeds.ToList()[0].Tweets.ToList()[1].Message);
215
216            Assert.AreEqual("Martin", twitterFeeds.ToList()[0].Followers.ToList()[0].UserName);            
217
218            Assert.AreEqual("Ward", twitterFeeds.ToList()[1].User.UserName);
219
220            Assert.AreEqual("Alan", twitterFeeds.ToList()[1].Tweets.ToList()[0].TwitterUser.UserName);
221            Assert.AreEqual("If you have a procedure with 10 parameters, you probably missed some.", twitterFeeds.ToList()[1].Tweets.ToList()[0].Message);
222            Assert.AreEqual("Ward", twitterFeeds.ToList()[1].Tweets.ToList()[1].TwitterUser.UserName);
223            Assert.AreEqual("There are only two hard things in Computer Science: cache invalidation, naming things and off-by-1 errors.", twitterFeeds.ToList()[1].Tweets.ToList()[1].Message);
224            Assert.AreEqual("Alan", twitterFeeds.ToList()[1].Tweets.ToList()[2].TwitterUser.UserName);
225            Assert.AreEqual("Random numbers should not be generated with a method chosen at random.", twitterFeeds.ToList()[1].Tweets.ToList()[2].Message);
226
227            Assert.AreEqual("Alan", twitterFeeds.ToList()[1].Followers.ToList()[0].UserName);
228            Assert.AreEqual("Martin", twitterFeeds.ToList()[1].Followers.ToList()[1].UserName);
229        }
230
231        [Test]
232        public async Task GetTwitterFeedAsync_Given_ValidInput1_ValidInput2_1User1Follower_3Tweets_Returns_ValidIEnumerableTwitterFeed_Count1()
233        {
234            // Arrange.
235            var dataManager = Substitute.For<IDataManager>();
236            var twitterUsersBuilder = Substitute.For<ITwitterUsersBuilder>();
237            var twitterFeedBuilder = Substitute.For<ITweetBuilder>();
238
239            dataManager.GetTwitterUsersAsync(Arg.Any<string>())
240                .Returns(info => Task.FromResult<IEnumerable<TwitterUser>>(
241                    new List<TwitterUser>
242                    {
243                        new TwitterUser
244                        {
245                            User = new User {UserName = "Ward"},
246                            Followers = new List<User>
247                            {
248                                new User {UserName = "Alan"}
249                            }
250                        }
251                    }));
252
253            dataManager.GetTweetsAsync(Arg.Any<string>())
254                .Returns(info => Task.FromResult<IEnumerable<Tweet>>(new List<Tweet>
255                {
256                    new Tweet
257                    {
258                        Id = 1,
259                        TwitterUser = new User {UserName = "Alan"},
260                        Message = "If you have a procedure with 10 parameters, you probably missed some."
261                    },
262                    new Tweet
263                    {
264                        Id = 2,
265                        TwitterUser = new User {UserName = "Ward"},
266                        Message = "There are only two hard things in Computer Science: cache invalidation, naming things and off-by-1 errors."
267                    },
268                    new Tweet
269                    {
270                        Id = 3,
271                        TwitterUser = new User {UserName = "Alan"},
272                        Message = "Random numbers should not be generated with a method chosen at random."
273                    }
274                }));
275
276            twitterUsersBuilder.UnionDuplicateUsers(Arg.Any<IEnumerable<TwitterUser>>())
277                .Returns(info => new List<TwitterUser>
278                {
279                    new TwitterUser
280                    {
281                        User = new User {UserName = "Ward"},
282                        Followers = new List<User>
283                        {
284                            new User {UserName = "Alan"}
285                        }
286                    }
287                });
288
289            twitterUsersBuilder.SortUsers(Arg.Any<IEnumerable<TwitterUser>>())
290                .Returns(info => new List<TwitterUser>
291                {
292                    new TwitterUser
293                    {
294                        User = new User {UserName = "Ward"},
295                        Followers = new List<User>
296                        {
297                            new User {UserName = "Alan"}
298                        }
299                    }
300                });
301
302            twitterUsersBuilder.SortFollowers(Arg.Any<IEnumerable<TwitterUser>>())
303                .Returns(info => new List<TwitterUser>
304                {
305                    new TwitterUser
306                    {
307                        User = new User {UserName = "Ward"},
308                        Followers = new List<User>
309                        {
310                            new User {UserName = "Alan"}
311                        }
312                    }
313                });
314
315            twitterFeedBuilder.SortTweets(Arg.Any<IEnumerable<TwitterFeed>>()).Returns(info => new List<TwitterFeed>
316            {
317                new TwitterFeed
318                {
319                    User = new User {UserName = "Ward"},
320                    Followers = new List<User>
321                    {
322                        new User {UserName = "Alan"}
323                    },
324                    Tweets = new List<Tweet>
325                    {
326                        new Tweet
327                        {
328                            Id = 1,
329                            TwitterUser = new User {UserName = "Alan"},
330                            Message = "If you have a procedure with 10 parameters, you probably missed some."
331                        },
332                        new Tweet
333                        {
334                            Id = 2,
335                            TwitterUser = new User {UserName = "Ward"},
336                            Message = "There are only two hard things in Computer Science: cache invalidation, naming things and off-by-1 errors."
337                        },
338                        new Tweet
339                        {
340                            Id = 3,
341                            TwitterUser = new User {UserName = "Alan"},
342                            Message = "Random numbers should not be generated with a method chosen at random."
343                        }
344                    }
345                }
346            });
347
348            var twitterFeedEngine = new TwitterFeedEngine(dataManager, twitterUsersBuilder, twitterFeedBuilder);
349
350            // Act.
351            var actual = await twitterFeedEngine.GetTwitterFeedAsync("test1.txt", "test2.txt");
352
353            // Assert.
354            var twitterFeeds = actual as IList<TwitterFeed> ?? actual.ToList();
355
356            Assert.AreEqual(1, twitterFeeds.Count);
357            Assert.AreEqual("Ward", twitterFeeds.ToList()[0].User.UserName);
358
359            Assert.AreEqual("Alan", twitterFeeds.ToList()[0].Tweets.ToList()[0].TwitterUser.UserName);
360            Assert.AreEqual("If you have a procedure with 10 parameters, you probably missed some.", twitterFeeds.ToList()[0].Tweets.ToList()[0].Message);
361            Assert.AreEqual("Ward", twitterFeeds.ToList()[0].Tweets.ToList()[1].TwitterUser.UserName);
362            Assert.AreEqual("There are only two hard things in Computer Science: cache invalidation, naming things and off-by-1 errors.", twitterFeeds.ToList()[0].Tweets.ToList()[1].Message);
363            Assert.AreEqual("Alan", twitterFeeds.ToList()[0].Tweets.ToList()[2].TwitterUser.UserName);
364            Assert.AreEqual("Random numbers should not be generated with a method chosen at random.", twitterFeeds.ToList()[0].Tweets.ToList()[2].Message);
365
366            Assert.AreEqual("Alan", twitterFeeds.ToList()[0].Followers.ToList()[0].UserName);
367        }
368
369
370        [Test]
371        public async Task GetTwitterFeedAsync_Given_ValidInput1_ValidInput2_1User0Follower_3Tweets_Returns_ValidIEnumerableTwitterFeed_Count1_Tweet()
372        {
373            // Arrange.
374            var dataManager = Substitute.For<IDataManager>();
375            var twitterUsersBuilder = Substitute.For<ITwitterUsersBuilder>();
376            var twitterFeedBuilder = Substitute.For<ITweetBuilder>();
377
378            dataManager.GetTwitterUsersAsync(Arg.Any<string>())
379                .Returns(info => Task.FromResult<IEnumerable<TwitterUser>>(
380                    new List<TwitterUser>
381                    {
382                        new TwitterUser
383                        {
384                            User = new User {UserName = "Ward"}
385                        }
386                    }));
387
388            dataManager.GetTweetsAsync(Arg.Any<string>())
389                .Returns(info => Task.FromResult<IEnumerable<Tweet>>(new List<Tweet>
390                {
391                    new Tweet
392                    {
393                        Id = 1,
394                        TwitterUser = new User {UserName = "Alan"},
395                        Message = "If you have a procedure with 10 parameters, you probably missed some."
396                    },
397                    new Tweet
398                    {
399                        Id = 2,
400                        TwitterUser = new User {UserName = "Ward"},
401                        Message = "There are only two hard things in Computer Science: cache invalidation, naming things and off-by-1 errors."
402                    },
403                    new Tweet
404                    {
405                        Id = 3,
406                        TwitterUser = new User {UserName = "Alan"},
407                        Message = "Random numbers should not be generated with a method chosen at random."
408                    }
409                }));
410
411            twitterUsersBuilder.UnionDuplicateUsers(Arg.Any<IEnumerable<TwitterUser>>())
412                .Returns(info => new List<TwitterUser>
413                {
414                    new TwitterUser
415                    {
416                        User = new User {UserName = "Ward"},
417                        Followers = new List<User>()
418                    }
419                });
420
421            twitterUsersBuilder.SortUsers(Arg.Any<IEnumerable<TwitterUser>>())
422                .Returns(info => new List<TwitterUser>
423                {
424                    new TwitterUser
425                    {
426                        User = new User {UserName = "Ward"},
427                        Followers = new List<User>()
428                    }
429                });
430
431            twitterUsersBuilder.SortFollowers(Arg.Any<IEnumerable<TwitterUser>>())
432                .Returns(info => new List<TwitterUser>
433                {
434                    new TwitterUser
435                    {
436                        User = new User {UserName = "Ward"},
437                        Followers = new List<User>()
438                    }
439                });
440
441            twitterFeedBuilder.SortTweets(Arg.Any<IEnumerable<TwitterFeed>>()).Returns(info => new List<TwitterFeed>
442            {
443                new TwitterFeed
444                {
445                    User = new User {UserName = "Ward"},
446                    Followers = new List<User>(),
447                    Tweets = new List<Tweet>
448                    {
449                        
450                        new Tweet
451                        {
452                            Id = 2,
453                            TwitterUser = new User {UserName = "Ward"},
454                            Message = "There are only two hard things in Computer Science: cache invalidation, naming things and off-by-1 errors."
455                        }
456                    }
457                }
458            });
459
460            var twitterFeedEngine = new TwitterFeedEngine(dataManager, twitterUsersBuilder, twitterFeedBuilder);
461
462            // Act.
463            var actual = await twitterFeedEngine.GetTwitterFeedAsync("test1.txt", "test2.txt");
464
465            // Assert.
466            var twitterFeeds = actual as IList<TwitterFeed> ?? actual.ToList();
467
468            Assert.AreEqual(1, twitterFeeds.Count);
469            Assert.AreEqual("Ward", twitterFeeds.ToList()[0].User.UserName);
470            
471            Assert.AreEqual("Ward", twitterFeeds.ToList()[0].Tweets.ToList()[0].TwitterUser.UserName);
472            Assert.AreEqual("There are only two hard things in Computer Science: cache invalidation, naming things and off-by-1 errors.", twitterFeeds.ToList()[0].Tweets.ToList()[0].Message);
473
474            Assert.AreEqual(0, twitterFeeds.ToList()[0].Followers.Count());
475        }
476
477    }
478}