sashomaga

Some unit tests

May 19th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.54 KB | None | 0 0
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Wintellect.PowerCollections;
  6. using CatalogOfFreeContent;
  7. using System.Reflection;
  8.  
  9. namespace CatalogTests
  10. {
  11.     [TestClass]
  12.     public class CatalogTests
  13.     {
  14.         [TestMethod]
  15.         public void TestAddTitleWithSingleElement()
  16.         {
  17.             Catalog catalog = new Catalog();
  18.             catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "16148072", "http://www.mozilla.org" }));
  19.             // Test Reflection
  20.             FieldInfo title = typeof(Catalog).GetField("title", BindingFlags.NonPublic | BindingFlags.Instance);
  21.             OrderedMultiDictionary<string, IContent> titleList = (OrderedMultiDictionary<string, IContent>)title.GetValue(catalog);
  22.  
  23.             Assert.AreEqual(titleList.Count, 1);
  24.             Assert.IsTrue(titleList.ContainsKey("Firefox v.11.0"));
  25.         }
  26.  
  27.         [TestMethod]
  28.         public void TestAddUrlWithSingleElement()
  29.         {
  30.             Catalog catalog = new Catalog();
  31.             catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "16148072", "http://www.mozilla.org" }));
  32.  
  33.             FieldInfo url = typeof(Catalog).GetField("url", BindingFlags.NonPublic | BindingFlags.Instance);
  34.             MultiDictionary<string, IContent> urlList = (MultiDictionary<string, IContent>)url.GetValue(catalog);
  35.  
  36.             Assert.AreEqual(urlList.Count, 1);
  37.             Assert.IsTrue(urlList.ContainsKey("http://www.mozilla.org"));
  38.         }
  39.  
  40.         [TestMethod]
  41.         public void TestAddDublicateValue()
  42.         {
  43.             Catalog catalog = new Catalog();
  44.             catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "16148072", "http://www.mozilla.org" }));
  45.             catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "16148072", "http://www.mozilla.org" }));
  46.  
  47.  
  48.             Assert.AreEqual(2, catalog.Count);
  49.         }
  50.  
  51.         [TestMethod]
  52.         public void TestAddMultipalItems()
  53.         {
  54.             Catalog catalog = new Catalog();
  55.             catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
  56.             catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
  57.             catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
  58.             catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
  59.             catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
  60.  
  61.             Assert.AreEqual(5, catalog.Count);
  62.         }
  63.  
  64.         [TestMethod]
  65.         public void TestGetListContentWithMatchesLessThanCount()
  66.         {
  67.             Catalog catalog = new Catalog();
  68.             catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
  69.             catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
  70.             catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
  71.             catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
  72.  
  73.             IEnumerable<IContent> actual = catalog.GetListContent("intro Firefox", 5);
  74.  
  75.             Assert.AreEqual(actual.Count(), 2);
  76.         }
  77.  
  78.         [TestMethod]
  79.         public void TestGetListContentWhetherReturnsLimitedMatches()
  80.         {
  81.             Catalog catalog = new Catalog();
  82.             catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "2148072", "http://www.firefox.com" }));
  83.             catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho2", "161480722", "http://www.firefox.com" }));
  84.             catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho3", "16148072111", "http://www.firefox.com" }));
  85.             catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
  86.             catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
  87.  
  88.             IEnumerable<IContent> actual = catalog.GetListContent("intro Firefox", 2);
  89.  
  90.             Assert.AreEqual(actual.Count(), 2);
  91.         }
  92.  
  93.         [TestMethod]
  94.         public void TestGetListContentWithSingleMatch()
  95.         {
  96.             Catalog catalog = new Catalog();
  97.             catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
  98.             catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
  99.             catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
  100.  
  101.             IEnumerable<IContent> actual = catalog.GetListContent("intro Firefox", 10);
  102.  
  103.             Assert.AreEqual(actual.Count(), 1);
  104.         }
  105.  
  106.         [TestMethod]
  107.         public void TestGetListContentWithNoMatches()
  108.         {
  109.             Catalog catalog = new Catalog();
  110.             catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
  111.             catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
  112.             catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
  113.             catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
  114.             catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
  115.  
  116.             IEnumerable<IContent> actual = catalog.GetListContent("No match element", 10);
  117.  
  118.             Assert.AreEqual(actual.Count(), 0);
  119.         }
  120.  
  121.         [TestMethod]
  122.         public void TestGetListContentSorting()
  123.         {
  124.             Catalog catalog = new Catalog();
  125.             catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
  126.             catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
  127.             catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Intro Firefox", "Gosho", "261472", "http://www.btv.org" }));
  128.             catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
  129.             catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Intro Firefox", "Ani", "261472", "http://www.btv.org" }));
  130.  
  131.             IEnumerable<IContent> actual = catalog.GetListContent("Intro Firefox", 10);
  132.             string[] actualTextRepresentation = new string[actual.Count()];
  133.             int index = 0;
  134.             foreach (var item in actual)
  135.             {
  136.                 actualTextRepresentation[index] = item.TextRepresentation;
  137.                 index++;
  138.             }
  139.             // Ordered by Movie than ordered by Name -> Ani, Gosho, Pesho
  140.             string[] expected = new string[] {
  141.                                                 "Movie: Intro Firefox; Ani; 261472; http://www.btv.org",
  142.                                                 "Movie: Intro Firefox; Gosho; 261472; http://www.btv.org",
  143.                                                 "Movie: Intro Firefox; Pesho; 16148072; http://www.firefox.com"
  144.                                              };
  145.  
  146.             CollectionAssert.AreEqual(actualTextRepresentation, expected);
  147.         }
  148.  
  149.         [TestMethod]
  150.         public void TestUpdateContentWithOneMatch()
  151.         {
  152.             Catalog catalog = new Catalog();
  153.             catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
  154.             catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
  155.             catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
  156.  
  157.             catalog.UpdateContent("http://www.firefox.com", "www.yahoo.com");
  158.  
  159.             FieldInfo url = typeof(Catalog).GetField("url", BindingFlags.NonPublic | BindingFlags.Instance);
  160.             MultiDictionary<string, IContent> urlList = (MultiDictionary<string, IContent>)url.GetValue(catalog);
  161.  
  162.             Assert.IsTrue(urlList.ContainsKey("www.yahoo.com"));
  163.         }
  164.  
  165.         [TestMethod]
  166.         public void TestUpdateContentNoMatch()
  167.         {
  168.             Catalog catalog = new Catalog();
  169.             catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
  170.             catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
  171.             catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
  172.  
  173.             catalog.UpdateContent("invalid.url.com", "www.yahoo.com");
  174.  
  175.             FieldInfo url = typeof(Catalog).GetField("url", BindingFlags.NonPublic | BindingFlags.Instance);
  176.             MultiDictionary<string, IContent> urlList = (MultiDictionary<string, IContent>)url.GetValue(catalog);
  177.  
  178.             Assert.IsFalse(urlList.ContainsKey("www.yahoo.com"));
  179.         }
  180.  
  181.         [TestMethod]
  182.         public void TestUpdateContentMultipleUpdates()
  183.         {
  184.             Catalog catalog = new Catalog();
  185.             catalog.Add(new ContentItem(ContentItemType.Book, new string[] { "intro Firefox", "Pesho", "16148072", "http://www.firefox.com" }));
  186.             catalog.Add(new ContentItem(ContentItemType.Application, new string[] { "Firefox v.11.0", "Mozilla", "314834072", "http://www.mozilla.org" }));
  187.             catalog.Add(new ContentItem(ContentItemType.Movie, new string[] { "Firefox movie", "btv", "261472", "http://www.btv.org" }));
  188.  
  189.             catalog.UpdateContent("http://www.firefox.com", "www.yahoo.com");
  190.  
  191.             FieldInfo url = typeof(Catalog).GetField("url", BindingFlags.NonPublic | BindingFlags.Instance);
  192.             MultiDictionary<string, IContent> urlList = (MultiDictionary<string, IContent>)url.GetValue(catalog);
  193.  
  194.             Assert.IsTrue(urlList.ContainsKey("www.yahoo.com"));
  195.  
  196.             catalog.UpdateContent("www.yahoo.com", "http://www.firefox.com");
  197.  
  198.             url = typeof(Catalog).GetField("url", BindingFlags.NonPublic | BindingFlags.Instance);
  199.             urlList = (MultiDictionary<string, IContent>)url.GetValue(catalog);
  200.  
  201.             Assert.IsTrue(urlList.ContainsKey("http://www.firefox.com"));
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment