예제 #1
0
    def test_update_rotating_tables(self):
        from autopush.db import get_month
        conf = AutopushConfig(hostname="example.com", resolve_hostname=True)
        db = DatabaseManager.from_config(conf,
                                         resource=autopush.tests.boto_resource)
        db.create_initial_message_tables()

        # Erase the tables it has on init, and move current month back one
        last_month = get_month(-1)
        db.current_month = last_month.month
        db.message_tables = [
            make_rotating_tablename("message", delta=-1),
            make_rotating_tablename("message", delta=0)
        ]

        # Create the next month's table, just in case today is the day before
        # a new month, in which case the lack of keys will cause an error in
        # update_rotating_tables
        next_month = get_month(1)
        assert next_month.month not in db.message_tables

        # Get the deferred back
        e = Deferred()
        d = db.update_rotating_tables()

        def check_tables(result):
            assert db.current_month == get_month().month
            assert len(db.message_tables) == 2

        d.addCallback(check_tables)
        d.addBoth(lambda x: e.callback(True))
        return e
예제 #2
0
    def test_no_rotation(self):
        today = datetime.date.today()
        next_month = today.month + 1
        next_year = today.year
        if next_month > 12:  # pragma: nocover
            next_month = 1
            next_year += 1
        tomorrow = datetime.datetime(year=next_year, month=next_month, day=1)
        conf = AutopushConfig(hostname="example.com",
                              resolve_hostname=True,
                              allow_table_rotation=False)
        resource = autopush.tests.boto_resource
        db = DatabaseManager.from_config(conf, resource=resource)
        db._tomorrow = Mock(return_value=tomorrow)
        db.create_initial_message_tables()
        assert len(db.message_tables) == 1
        assert db.message_tables[0] == resource.get_latest_message_tablename(
            prefix=conf.message_table.tablename)

        def check_tables(result):
            assert len(db.message_tables) == 1
            assert db.message_tables[0] ==  \
                resource.get_latest_message_tablename(
                    prefix=conf.message_table.tablename
                )

        dd = db.update_rotating_tables()
        dd.addCallback(check_tables)
        return dd
예제 #3
0
    def test_update_not_needed(self):
        from autopush.db import get_month
        conf = AutopushConfig(hostname="google.com", resolve_hostname=True)
        db = DatabaseManager.from_config(conf)
        db.create_initial_message_tables()

        # Erase the tables it has on init, and move current month back one
        db.message_tables = {}

        # Create the next month's table, just in case today is the day before
        # a new month, in which case the lack of keys will cause an error in
        # update_rotating_tables
        next_month = get_month(1)
        db.message_tables[next_month.month] = None

        # Get the deferred back
        e = Deferred()
        d = db.update_rotating_tables()

        def check_tables(result):
            assert len(db.message_tables) == 1

        d.addCallback(check_tables)
        d.addBoth(lambda x: e.callback(True))
        return e
예제 #4
0
    def test_update_rotating_tables_month_end(self):
        """Test that rotating adds next months table

        This test is intended to ensure that if the next day is a new
        month, then update_rotating_tables realizes this and add's
        the new table to the message_tables.

        A pre-requisite is that today cannot be the last day of
        the current month. Therefore, we first sub in _tomorrow to
        ensure it always appears as next month, and then remove
        the new table create_initial_tables made so we can observe
        update_rotating_tables add the new one.

        Note that sorting message table keys to find the last month
        does *not work* since the month digit is not zero-padded.

        """
        today = datetime.date.today()
        next_month = today.month + 1
        next_year = today.year
        if next_month > 12:  # pragma: nocover
            next_month = 1
            next_year += 1
        tomorrow = datetime.datetime(year=next_year,
                                     month=next_month,
                                     day=1)

        conf = AutopushConfig(
            hostname="example.com", resolve_hostname=True)
        db = DatabaseManager.from_config(
            conf,
            resource=autopush.tests.boto_resource)
        db._tomorrow = Mock(return_value=tomorrow)
        db.create_initial_message_tables()

        # We should have 3 tables, one for next/this/last month
        assert len(db.message_tables) == 3

        # Grab next month's table name and remove it
        next_month = get_rotating_message_tablename(
            conf.message_table.tablename,
            delta=1,
            boto_resource=db.resource
        )
        db.message_tables.pop(db.message_tables.index(next_month))

        # Get the deferred back
        d = db.update_rotating_tables()

        def check_tables(result):
            assert len(db.message_tables) == 3
            assert next_month in db.message_tables

        d.addCallback(check_tables)
        return d
예제 #5
0
 def test_new_month(self):
     today = datetime.date.today()
     next_month = today.month + 1
     next_year = today.year
     if next_month > 12:  # pragma: nocover
         next_month = 1
         next_year += 1
     tomorrow = datetime.datetime(year=next_year, month=next_month, day=1)
     db = test_db()
     db._tomorrow = Mock()
     db._tomorrow.return_value = tomorrow
     db.create_initial_message_tables()
     assert len(db.message_tables) == 3
예제 #6
0
    def test_update_not_needed(self):
        conf = AutopushConfig(hostname="google.com", resolve_hostname=True)
        db = DatabaseManager.from_config(conf,
                                         resource=autopush.tests.boto_resource)
        db.create_initial_message_tables()

        # Erase the tables it has on init, and move current month back one
        db.message_tables = []

        # Get the deferred back
        e = Deferred()
        d = db.update_rotating_tables()

        def check_tables(result):
            assert len(db.message_tables) == 1

        d.addCallback(check_tables)
        d.addBoth(lambda x: e.callback(True))
        return e