Guest User

Untitled

a guest
Nov 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. import unittest
  2. from copy import copy
  3. from solution import *
  4.  
  5. raw = """
  6. 01; Mr. P.C.; John Coltrane; Jazz, Bebop; fast
  7. 02; My Favourite Things; John Coltrane; Jazz, Bebop; popular, cover
  8. 03; Greensleves; John Coltrane; Jazz, Bebop; popular, cover
  9. 04; Alabama; John Coltrane; Jazz, Avantgarde; melancholic
  10. 05; Acknowledgement; John Coltrane; Jazz, Avantgarde;
  11. 06; Afro Blue; John Coltrane; Jazz; melancholic
  12. 07; 'Round Midnight; John Coltrane; Jazz; cover
  13. 08; My Funny Valentine; Miles Davis; Jazz; popular
  14. 09; Tutu; Miles Davis; Jazz, Fusion;
  15. 10; Miles Runs The Voodo Down; Miles Davis; Jazz, Fusion;
  16. 11; Boplicity; Miles Davis; Jazz, Bebop;
  17. 12; Autumn Leaves; Bill Evans; Jazz; popular, cover
  18. 13; Waltz for Debbie; Bill Evans; Jazz;
  19. 14; 'Round Midnight; Thelonious Monk; Jazz, Bebop;
  20. 15; Ruby, My Dear; Thelonious Monk; Jazz; saxophone
  21. 16; Blue Monk; Thelonious Monk; Jazz;
  22. 17; Fur Elise; L. van Beethoven; Classical; popular
  23. 18; Moonlight Sonata; L. van Beethoven; Classical; popular
  24. 19; Pathetique; L. van Beethoven; Classical;
  25. 20; Toccata e Fuga; J.S. Bach; Classical, Baroque; popular
  26. 21; Goldberg Variations; J.S. Bach; Classical, Baroque;
  27. 22; Brandenburg Concerto; J.S. Bach; Classical, Baroque;
  28. 23; Eine Kleine Nachtmusik; W.A. Mozart; Classical; popular, violin
  29. """.strip()
  30.  
  31. songs_text = "\n".join(line[4:] for line in raw.split("\n"))
  32.  
  33. handles = {}
  34.  
  35. for line in raw.split("\n"):
  36. number, name, artist, genre, tags = map(str.strip, line.split(";"))
  37. handles[int(number.strip())] = name + "/" + artist
  38.  
  39. tags = {
  40. "John Coltrane": {'saxophone'},
  41. "Miles Davis": {'trumpet'},
  42. "Bill Evans": {'piano'},
  43. "Thelonious Monk": {'piano', 'bebop'},
  44. "J.S. Bach": {'piano', 'polyphony'},
  45. "L. van Beethoven": {'piano'},
  46. "W.A. Mozart": {'piano'},
  47. }
  48.  
  49. class CollectionTest(unittest.TestCase):
  50. def setUp(self):
  51. self.collection = Collection(songs_text, copy(tags))
  52.  
  53. def assertSongs(self, song_ids, **what):
  54. expected = sorted([handles[song_id] for song_id in song_ids])
  55. actual = sorted([song.name + "/" + song.artist for song in self.collection.find('songs', **what)])
  56. self.assertEqual(expected, actual)
  57.  
  58. def test_simple_tag(self):
  59. self.assertSongs([4, 6], tags={'melancholic'})
  60. self.assertSongs([2, 3, 8, 12, 17, 18, 20, 23], tags={'popular'})
  61. self.assertSongs([1], tags={'fast'})
  62.  
  63. def test_negative_tags(self):
  64. self.assertSongs([6], tags={'melancholic', 'avantgarde!'})
  65. self.assertSongs([17, 18, 23], tags={'popular', 'piano', 'jazz!', 'baroque!'})
  66.  
  67. def test_find_subgenre(self):
  68. self.assertEqual(
  69. {'Baroque', 'Bebop'},
  70. set(self.collection.find('subgenre', tags='popular')))
  71.  
  72. def test_name(self):
  73. self.assertSongs([4], name='Alabama')
  74. self.assertEqual(['Alabama'], self.collection.find('name', name='Alabama'))
  75. self.assertSongs([4, 5, 6, 12], name=re.compile(r'^A'))
  76.  
  77. def test_artist(self):
  78. self.assertSongs([1,2,3,4,5,6,7,20,21,22], artist=re.compile(r'J'))
  79.  
  80. def test_filter(self):
  81. classical = lambda song: song.genre == 'Classical'
  82. has_sub = lambda song: not song.subgenre is None
  83. self.assertSongs([17,18,19,20,21,22,23], filter=classical)
  84. self.assertSongs([20,21,22], filter=[classical, has_sub])
  85.  
  86. def test_none_all(self):
  87. self.assertSongs([i for i in range(1,24)])
  88. self.assertSongs([], filter=lambda song: False)
  89.  
  90. if __name__ == "__main__":
  91. unittest.main()
Add Comment
Please, Sign In to add comment