| 1 | #!/usr/bin/python |
|---|
| 2 | """Unit Tests for Nico Live Alert to IRC. |
|---|
| 3 | """ |
|---|
| 4 | # |
|---|
| 5 | # Copyright (c) 2009 Satoshi Fukutomi <info@fuktommy.com>. |
|---|
| 6 | # All rights reserved. |
|---|
| 7 | # |
|---|
| 8 | # Redistribution and use in source and binary forms, with or without |
|---|
| 9 | # modification, are permitted provided that the following conditions |
|---|
| 10 | # are met: |
|---|
| 11 | # 1. Redistributions of source code must retain the above copyright |
|---|
| 12 | # notice, this list of conditions and the following disclaimer. |
|---|
| 13 | # 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 14 | # notice, this list of conditions and the following disclaimer in the |
|---|
| 15 | # documentation and/or other materials provided with the distribution. |
|---|
| 16 | # |
|---|
| 17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND |
|---|
| 18 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 19 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 20 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE |
|---|
| 21 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 22 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|---|
| 23 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 24 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|---|
| 25 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|---|
| 26 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|---|
| 27 | # SUCH DAMAGE. |
|---|
| 28 | # |
|---|
| 29 | # $Id$ |
|---|
| 30 | # |
|---|
| 31 | |
|---|
| 32 | import sqlite3 |
|---|
| 33 | import sys |
|---|
| 34 | import unittest |
|---|
| 35 | |
|---|
| 36 | from nicolivealertirc import Filter, Recommend |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | class BotMock: |
|---|
| 40 | def __init__(self): |
|---|
| 41 | self.queue = [] |
|---|
| 42 | |
|---|
| 43 | def post(self, msg): |
|---|
| 44 | self.queue.append(msg) |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | class FilterTest(unittest.TestCase): |
|---|
| 48 | def setUp(self): |
|---|
| 49 | self.db = sqlite3.connect(':memory:') |
|---|
| 50 | self.db.isolation_level = None |
|---|
| 51 | self.botmock = BotMock() |
|---|
| 52 | |
|---|
| 53 | def test_includes(self): |
|---|
| 54 | filter = Filter(self.db).open() |
|---|
| 55 | self.assertEquals(False, filter.includes('co123')) |
|---|
| 56 | |
|---|
| 57 | def test_add(self): |
|---|
| 58 | filter = Filter(self.db).open() |
|---|
| 59 | filter.add('co123') |
|---|
| 60 | self.assertEquals(False, filter.includes('co123')) |
|---|
| 61 | |
|---|
| 62 | filter.flush_queue(self.botmock) |
|---|
| 63 | self.assertEquals(True, filter.includes('co123')) |
|---|
| 64 | self.assertEquals(['[add] co123'], self.botmock.queue) |
|---|
| 65 | |
|---|
| 66 | def test_open(self): |
|---|
| 67 | filter = Filter(self.db).open() |
|---|
| 68 | filter.add('co123') |
|---|
| 69 | filter.flush_queue(self.botmock) |
|---|
| 70 | self.assertEquals(True, filter.includes('co123')) |
|---|
| 71 | |
|---|
| 72 | filter = Filter(self.db).open() |
|---|
| 73 | self.assertEquals(True, filter.includes('co123')) |
|---|
| 74 | |
|---|
| 75 | def test_delete(self): |
|---|
| 76 | filter = Filter(self.db).open() |
|---|
| 77 | filter.add('co123') |
|---|
| 78 | filter.flush_queue(BotMock()) |
|---|
| 79 | self.assertEquals(True, filter.includes('co123')) |
|---|
| 80 | |
|---|
| 81 | filter.delete('co123') |
|---|
| 82 | self.assertEquals(True, filter.includes('co123')) |
|---|
| 83 | |
|---|
| 84 | filter.flush_queue(self.botmock) |
|---|
| 85 | self.assertEquals(False, filter.includes('co123')) |
|---|
| 86 | self.assertEquals(['[delete] co123'], self.botmock.queue) |
|---|
| 87 | |
|---|
| 88 | def test_list(self): |
|---|
| 89 | filter = Filter(self.db).open() |
|---|
| 90 | filter.add('co123') |
|---|
| 91 | filter.add('ch456') |
|---|
| 92 | filter.flush_queue(BotMock()) |
|---|
| 93 | |
|---|
| 94 | filter.list() |
|---|
| 95 | filter.flush_queue(self.botmock) |
|---|
| 96 | |
|---|
| 97 | expected = [ |
|---|
| 98 | '[list begin]', |
|---|
| 99 | '[list] co123 http://ch.nicovideo.jp/community/co123', |
|---|
| 100 | '[list] ch456 http://ch.nicovideo.jp/channel/ch456', |
|---|
| 101 | '[list end]', |
|---|
| 102 | ] |
|---|
| 103 | self.assertEquals(expected, self.botmock.queue) |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | class RecommendTest(unittest.TestCase): |
|---|
| 107 | def setUp(self): |
|---|
| 108 | self.db = sqlite3.connect(':memory:') |
|---|
| 109 | self.db.isolation_level = None |
|---|
| 110 | self.botmock = BotMock() |
|---|
| 111 | |
|---|
| 112 | def test_default_random_rate(self): |
|---|
| 113 | recommend = Recommend(self.db).open() |
|---|
| 114 | self.assertEquals(0.1, recommend.random_rate) |
|---|
| 115 | |
|---|
| 116 | def test_set_random_rate(self): |
|---|
| 117 | recommend = Recommend(self.db).open() |
|---|
| 118 | recommend.set_random_rate(0.5) |
|---|
| 119 | self.assertEquals(0.1, recommend.random_rate) |
|---|
| 120 | |
|---|
| 121 | recommend.flush_queue(self.botmock) |
|---|
| 122 | self.assertEquals(0.5, recommend.random_rate) |
|---|
| 123 | self.assertEquals(['[rate] 0.500000'], self.botmock.queue) |
|---|
| 124 | |
|---|
| 125 | def test_open(self): |
|---|
| 126 | recommend = Recommend(self.db).open() |
|---|
| 127 | recommend.set_random_rate(0.5) |
|---|
| 128 | recommend.flush_queue(self.botmock) |
|---|
| 129 | self.assertEquals(0.5, recommend.random_rate) |
|---|
| 130 | |
|---|
| 131 | recommend = Recommend(self.db).open() |
|---|
| 132 | self.assertEquals(0.5, recommend.random_rate) |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | def _main(): |
|---|
| 136 | suite = unittest.TestSuite() |
|---|
| 137 | suite.addTest(unittest.makeSuite(FilterTest)) |
|---|
| 138 | suite.addTest(unittest.makeSuite(RecommendTest)) |
|---|
| 139 | result = unittest.TextTestRunner(verbosity=2).run(suite) |
|---|
| 140 | if result.errors or result.failures: |
|---|
| 141 | sys.exit(1) |
|---|
| 142 | else: |
|---|
| 143 | sys.exit() |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | if __name__ == '__main__': |
|---|
| 147 | _main() |
|---|