def test_set_language(self): user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') user_config.set_language("TEST_LANGUAGE") self.assertEqual('TEST_LANGUAGE', user_config.language) config_file = UserConfigFile('var', 'TEST') user_config = UserConfig.load(Config('calbot.cfg.sample'), 'TEST', config_file.read_parser()) self.assertEqual('TEST_LANGUAGE', user_config.language) shutil.rmtree('var/TEST')
def test_set_format(self): user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') user_config.set_format("TEST FORMAT") self.assertEqual('TEST FORMAT', user_config.format) config_file = UserConfigFile('var', 'TEST') user_config = UserConfig.load(Config('calbot.cfg.sample'), 'TEST', config_file.read_parser()) self.assertEqual('TEST FORMAT', user_config.format) shutil.rmtree('var/TEST')
def test_set_advance(self): user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') user_config.set_advance(['1', '3', '2', '3']) self.assertEqual([3, 2, 1], user_config.advance) config_file = UserConfigFile('var', 'TEST') user_config = UserConfig.load(Config('calbot.cfg.sample'), 'TEST', config_file.read_parser()) self.assertEqual([3, 2, 1], user_config.advance) shutil.rmtree('var/TEST')
def test_save_calendar(self): calendar_config = CalendarConfig.new( UserConfig.new(Config('calbot.cfg.sample'), 'TEST'), '1', 'file://{}/test/test.ics'.format(os.path.dirname(__file__)), 'TEST') calendar = Calendar(calendar_config) calendar_config.save_calendar(calendar) config_file = CalendarsConfigFile('var', 'TEST') calendar_config = CalendarConfig.load( UserConfig.new(Config('calbot.cfg.sample'), 'TEST'), config_file.read_parser(), '1') self.assertEqual('Тест', calendar_config.name) shutil.rmtree('var/TEST')
def test_calendar_save_error(self): calendar_config = CalendarConfig.new( UserConfig.new(Config('calbot.cfg.sample'), 'TEST'), '1', 'file://{}/test/test.ics'.format(os.path.dirname(__file__)), 'TEST') now = datetime.datetime.utcnow() calendar_config.save_error(Exception('TEST ERROR')) config_file = CalendarsConfigFile('var', 'TEST') calendar_config = CalendarConfig.load( UserConfig.new(Config('calbot.cfg.sample'), 'TEST'), config_file.read_parser(), '1') self.assertTrue(parse(calendar_config.last_process_at) > now) self.assertEqual('TEST ERROR', calendar_config.last_process_error) shutil.rmtree('var/TEST')
def test_read_calendar(self): config = CalendarConfig.new( UserConfig.new(Config('calbot.cfg.sample'), 'TEST'), '1', 'file://{}/test/test.ics'.format(os.path.dirname(__file__)), 'TEST') calendar = Calendar(config) self.assertEqual(pytz.timezone('Asia/Omsk'), calendar.timezone) self.assertEqual('Тест', calendar.name) self.assertEqual('Just a test calendar', calendar.description) event = calendar.all_events[0] self.assertEqual(datetime.date(2016, 6, 24), event.date) self.assertEqual( datetime.time(6, 0, 0, tzinfo=pytz.timezone('Asia/Omsk')), event.time) self.assertEqual('Событие по-русски', event.title) event = calendar.all_events[1] self.assertEqual(datetime.date(2016, 6, 23), event.date) self.assertEqual( datetime.time(6, 0, 0, tzinfo=pytz.timezone('Asia/Omsk')), event.time) self.assertEqual('Event title', event.title) event = calendar.all_events[2] self.assertEqual(datetime.date(2017, 1, 4), event.date) self.assertEqual( datetime.time(10, 0, 0, tzinfo=pytz.timezone('Asia/Omsk')), event.time) self.assertEqual('Daily event', event.title) event = calendar.all_events[3] self.assertTrue( event.date > datetime.date.today(), event.date) # this event repeats daily somewhere in future self.assertEqual( datetime.time(10, 0, 0, tzinfo=pytz.timezone('Asia/Omsk')), event.time) self.assertEqual('Daily event', event.title)
def main(): if len(sys.argv) > 1: configfile = sys.argv[1] else: configfile = os.path.join(os.path.dirname(__file__), 'calbot.cfg') config = Config(configfile) run_bot(config)
def test_default_user_confg(self): user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') self.assertEqual('var', user_config.vardir) self.assertEqual('TEST', user_config.id) self.assertEqual(DEFAULT_FORMAT, user_config.format) self.assertIsNone(user_config.language) self.assertEqual([48, 24], user_config.advance)
def test_read_repeated_event_exdate(self): timezone = pytz.timezone('Asia/Omsk') config = CalendarConfig.new( UserConfig.new(Config('calbot.cfg.sample'), 'TEST'), '1', 'file://{}/test/repeat.ics'.format(os.path.dirname(__file__)), 'TEST') calendar = Calendar(config) events = sort_events( list( calendar.read_ical( calendar.url, datetime.datetime(2019, 12, 16, 0, 0, 0, tzinfo=timezone), datetime.datetime(2020, 1, 19, 23, 59, 59, tzinfo=timezone)))) event = events[5] self.assertEqual(datetime.date(2019, 12, 18), event.date) self.assertEqual(datetime.time(19, 0, 0, tzinfo=timezone), event.time) self.assertEqual('Дата Ужин (OML)', event.title) self.assertRegex(event.description, r'Пиццот') event = events[6] self.assertEqual(datetime.date(2020, 1, 15), event.date) self.assertEqual(datetime.time(19, 0, 0, tzinfo=timezone), event.time) self.assertEqual('Дата Ужин (OML)', event.title) self.assertRegex(event.description, r'Пиццот')
def test_format_event_real_html(self): component = Component() component.add('summary', 'Встреча ML-клуба') component.add('location', 'ул. Таубе, 5, Омск, Омская обл., Россия, 644037') component.add( 'description', '10 февраля в 11:00 пройдет 5-я встреча <a href="https://vk.com/mlomsk">ML клуба</a> в офисе <a href="https://vk.com/7bits">7bits</a>, Таубе 5. Регистрация на встречу: <a href="https://vk.com/away.php?to=http%3A%2F%2Fmlomsk.1der.link%2Fmeetup%2Fsignup&post=-141957789_74&cc_key=" target="_blank">mlomsk.1der.link/meetup/signup</a>.<br><br>В этот раз у нас будет 2 доклада:' ) timezone = pytz.timezone('Asia/Omsk') component.add( 'dtstart', datetime.datetime(2018, 2, 10, 11, 0, 0, tzinfo=timezone)) event = Event.from_vevent(component, timezone) user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') user_config.language = 'ru_RU.UTF-8' result = format_event(user_config, event) self.assertEqual( 'Встреча ML-клуба\nСуббота, 10 февраля 2018, 11:00 Asia/Omsk\nул. Таубе, 5, Омск, Омская обл., Россия, 644037\n10 февраля в 11:00 пройдет 5-я встреча ML клуба (https://vk.com/mlomsk) в офисе 7bits (https://vk.com/7bits), Таубе 5. Регистрация на встречу: mlomsk.1der.link/meetup/signup.\n\nВ этот раз у нас будет 2 доклада:', result)
def test_format_date_only_event(self): timezone = pytz.UTC component = _get_component() component.add('dtstart', datetime.date(2016, 6, 23)) event = Event.from_vevent(component, timezone, datetime.time(10, 0)) user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') result = format_event(user_config, event) self.assertEqual( 'summary\nThursday, 23 June 2016\nlocation\ndescription', result)
def test_format_event_blanks(self): component = Component() component.add('dtstart', datetime.datetime(2018, 2, 3, 13, 3, 4, tzinfo=pytz.UTC)) event = Event.from_vevent(component, pytz.UTC) user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') user_config.language = 'ru_RU.UTF-8' result = format_event(user_config, event) self.assertEqual( 'None\nСуббота, 03 февраля 2018, 13:03 UTC\nNone\nNone', result)
def test_format_event(self): component = _get_component() component.add( 'dtstart', datetime.datetime(2016, 6, 23, 19, 50, 35, tzinfo=pytz.UTC)) event = Event.from_vevent(component, pytz.UTC) user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') result = format_event(user_config, event) self.assertEqual( 'summary\nThursday, 23 June 2016, 19:50 UTC\nlocation\ndescription', result)
def test_format_event_ru(self): component = _get_component() component.add( 'dtstart', datetime.datetime(2016, 6, 23, 19, 50, 35, tzinfo=pytz.UTC)) event = Event.from_vevent(component, pytz.UTC) user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') user_config.language = 'ru_RU.UTF-8' result = format_event(user_config, event) self.assertEqual( 'summary\nЧетверг, 23 июня 2016, 19:50 UTC\nlocation\ndescription', result)
def test_update_stats(self): config = Config('calbot.cfg.sample') update_stats(config) stats1 = get_stats(config) calendar_config = CalendarConfig.new( UserConfig.new(config, 'TEST'), '1', 'file://{}/test/test.ics'.format(os.path.dirname(__file__)), 'TEST') calendar = Calendar(calendar_config) calendar_config.save_calendar(calendar) update_stats(config) stats2 = get_stats(config) self.assertEqual(1, (stats2.users - stats1.users)) self.assertEqual(1, (stats2.calendars - stats1.calendars)) self.assertEqual(stats2.events, stats1.events) shutil.rmtree('var/TEST')
def test_format_event_html(self): component = Component() component.add('summary', '<b>summary</b>') component.add('location', '<i>location</i>') component.add( 'description', '<b>description</b><br><br> <a href="link.html">link</a>') component.add('dtstart', datetime.datetime(2018, 2, 3, 13, 3, 4, tzinfo=pytz.UTC)) event = Event.from_vevent(component, pytz.UTC) user_config = UserConfig.new(Config('calbot.cfg.sample'), 'TEST') user_config.language = 'ru_RU.UTF-8' result = format_event(user_config, event) self.assertEqual( 'summary\nСуббота, 03 февраля 2018, 13:03 UTC\nlocation\ndescription\n\n link (link.html)', result)
def test_read_repeated_event_until(self): timezone = pytz.timezone('Asia/Omsk') config = CalendarConfig.new( UserConfig.new(Config('calbot.cfg.sample'), 'TEST'), '1', 'file://{}/test/repeat.ics'.format(os.path.dirname(__file__)), 'TEST') calendar = Calendar(config) events = sort_events( list( calendar.read_ical( calendar.url, datetime.datetime(2019, 1, 21, 0, 0, 0, tzinfo=timezone), datetime.datetime(2019, 2, 10, 23, 59, 59, tzinfo=timezone)))) event = events[2] self.assertEqual(datetime.date(2019, 1, 23), event.date) self.assertEqual(datetime.time(19, 0, 0, tzinfo=timezone), event.time) self.assertEqual('Дата ужин (OML)', event.title) self.assertRegex(event.description, r'Бутерbrot') event = events[5] self.assertEqual(datetime.date(2019, 2, 6), event.date) self.assertEqual(datetime.time(19, 0, 0, tzinfo=timezone), event.time) self.assertEqual('Дата ужин (OML)', event.title) self.assertRegex(event.description, r'Розы Морозы') events = sort_events( list( calendar.read_ical( calendar.url, datetime.datetime(2019, 3, 4, 0, 0, 0, tzinfo=timezone), datetime.datetime(2019, 3, 24, 23, 59, 59, tzinfo=timezone)))) event = events[3] self.assertEqual(datetime.date(2019, 3, 6), event.date) self.assertEqual(datetime.time(19, 0, 0, tzinfo=timezone), event.time) self.assertEqual('Дата ужин (OML)', event.title) self.assertRegex(event.description, r'Розы Морозы') event = events[6] self.assertEqual(datetime.date(2019, 3, 20), event.date) self.assertEqual(datetime.time(19, 0, 0, tzinfo=timezone), event.time) self.assertEqual('Дата ужин (OML)', event.title) self.assertRegex(event.description, r'Пиццот')
def test_read_repeated_event_override(self): timezone = pytz.timezone('Asia/Omsk') config = CalendarConfig.new( UserConfig.new(Config('calbot.cfg.sample'), 'TEST'), '1', 'file://{}/test/repeat.ics'.format(os.path.dirname(__file__)), 'TEST') calendar = Calendar(config) self.assertEqual(timezone, calendar.timezone) events = sort_events( list( calendar.read_ical( calendar.url, datetime.datetime(2020, 3, 23, 0, 0, 0, tzinfo=timezone), datetime.datetime(2020, 4, 26, 23, 59, 59, tzinfo=timezone)))) event = events[5] self.assertEqual(datetime.date(2020, 3, 25), event.date) self.assertEqual(datetime.time(19, 0, 0, tzinfo=timezone), event.time) self.assertEqual('Дата Ужин (OML)', event.title) self.assertRegex(event.description, r'Пиццот') event = events[6] self.assertEqual(datetime.date(2020, 4, 8), event.date) self.assertEqual(datetime.time(19, 0, 0, tzinfo=timezone), event.time) self.assertEqual('Дата Ужин (OML)', event.title) self.assertRegex(event.description, r'discord') event = events[7] self.assertEqual(datetime.date(2020, 4, 22), event.date) self.assertEqual(datetime.time(19, 0, 0, tzinfo=timezone), event.time) self.assertEqual('Дата Ужин (OML)', event.title) self.assertRegex(event.description, r'Пиццот') ids = set(map(lambda e: e.id, events)) self.assertEqual(len(events), len(ids)) # all ids must be unique
def test_default_calendar_config(self): calendar_config = CalendarConfig.new( UserConfig.new(Config('calbot.cfg.sample'), 'TEST'), '1', 'file://{}/test/test.ics'.format(os.path.dirname(__file__)), 'TEST') self.assertEqual([48, 24], calendar_config.advance)