Example #1
0
 def add_task_for_repo(repo, task_name, action, **kwargs):
     cleanup = test_handler.initialize_handler(tasks.CleanUpInTestMode,
                                               action,
                                               repo=repo,
                                               params=kwargs)
     cleanup.set_listener(listener)
     cleanup.get()
Example #2
0
    def test_sms_render_person(self):
        handler = test_handler.initialize_handler(api.HandleSMS,
                                                  'api/handle_sms')

        person = model.Person.create_original('haiti',
                                              full_name='John Smith',
                                              latest_status='believed_alive',
                                              sex='male',
                                              age='30',
                                              home_city='Los Angeles',
                                              home_state='California',
                                              entry_date=datetime.datetime(
                                                  2010, 1, 1))
        assert (handler.render_person(person) == 'John Smith / '
                'Someone has received information that this person is alive / '
                'male / 30 / From: Los Angeles California')

        person = model.Person.create_original('haiti',
                                              full_name='John Smith',
                                              entry_date=datetime.datetime(
                                                  2010, 1, 1))
        assert handler.render_person(person) == 'John Smith'

        person = model.Person.create_original('haiti',
                                              full_name='John Smith',
                                              home_state='California',
                                              entry_date=datetime.datetime(
                                                  2010, 1, 1))
        assert handler.render_person(person) == 'John Smith / From: California'
Example #3
0
    def test_sms_render_person(self):
        handler = test_handler.initialize_handler(
            api.HandleSMS, 'api/handle_sms')

        person = model.Person.create_original(
            'haiti',
            full_name='John Smith',
            latest_status='believed_alive',
            sex='male',
            age='30',
            home_city='Los Angeles',
            home_state='California',
            entry_date=datetime.datetime(2010, 1, 1))
        assert (handler.render_person(person) ==
            'John Smith / '
            'Someone has received information that this person is alive / '
            'male / 30 / From: Los Angeles California')

        person = model.Person.create_original(
            'haiti',
            full_name='John Smith',
            entry_date=datetime.datetime(2010, 1, 1))
        assert handler.render_person(person) == 'John Smith'

        person = model.Person.create_original(
            'haiti',
            full_name='John Smith',
            home_state='California',
            entry_date=datetime.datetime(2010, 1, 1))
        assert handler.render_person(person) == 'John Smith / From: California'
Example #4
0
 def get_handler(self):
     # The handler can't be initialized until after the config is set up.
     return test_handler.initialize_handler(
         handler_class=tasks.NotifyManyUnreviewedNotes,
         action=tasks.NotifyManyUnreviewedNotes.ACTION,
         repo='haiti',
         environ=None,
         params=None)
Example #5
0
    def test_clean_up_in_test_mode_multi_tasks(self):
        """Test the clean up in test mode when it is broken into multiple
        tasks."""
        class Listener(object):
            def before_deletion(self, person):
                # This will be implemented later using mock.
                assert False

        tasks.CleanUpInTestMode.DELETION_AGE_SECONDS = 2 * 3600  # 2 hours
        utcnow = datetime.datetime(2010, 1, 1, 7, 0, 0)
        set_utcnow_for_test(utcnow)
        self.mox = mox.Mox()
        cleanup = \
            test_handler.initialize_handler(tasks.CleanUpInTestMode,
                                            tasks.CleanUpInTestMode.ACTION)
        listener = Listener()
        cleanup.set_listener(listener)

        # Simulates add_task_for_repo() because it doesn't work in unit tests.
        def add_task_for_repo(repo, task_name, action, **kwargs):
            cleanup = test_handler.initialize_handler(tasks.CleanUpInTestMode,
                                                      action,
                                                      repo=repo,
                                                      params=kwargs)
            cleanup.set_listener(listener)
            cleanup.get()

        self.mox.StubOutWithMock(cleanup, 'add_task_for_repo')
        (cleanup.add_task_for_repo('haiti',
                                   mox.IsA(str),
                                   mox.IsA(str),
                                   utcnow=str(
                                       calendar.timegm(utcnow.utctimetuple())),
                                   cursor=mox.IsA(str),
                                   queue_name=mox.IsA(str)).WithSideEffects(
                                       add_task_for_repo).MultipleTimes())

        def raise_deadline_exceeded_error(_):
            raise runtime.DeadlineExceededError()

        self.mox.StubOutWithMock(listener, 'before_deletion')
        listener.before_deletion(self.key_p1)
        listener.before_deletion(
            self.key_p2).WithSideEffects(raise_deadline_exceeded_error)
        listener.before_deletion(self.key_p2)

        self.mox.ReplayAll()

        config.set(test_mode=True, repo='haiti')
        # This should run multiple tasks and finally deletes all records.
        cleanup.get()
        assert db.get(self.key_p1) is None
        assert db.get(self.key_p2) is None

        self.mox.UnsetStubs()
        self.mox.VerifyAll()
Example #6
0
    def test_clean_up_in_test_mode_multi_tasks(self):
        """Test the clean up in test mode when it is broken into multiple
        tasks."""

        class Listener(object):
            def before_deletion(self, person):
                # This will be implemented later using mock.
                assert False

        tasks.CleanUpInTestMode.DELETION_AGE_SECONDS = 2 * 3600  # 2 hours
        utcnow = datetime.datetime(2010, 1, 1, 7, 0, 0)
        set_utcnow_for_test(utcnow)
        self.mox = mox.Mox()
        cleanup = \
            test_handler.initialize_handler(tasks.CleanUpInTestMode,
                                            tasks.CleanUpInTestMode.ACTION)
        listener = Listener()
        cleanup.set_listener(listener)

        # Simulates add_task_for_repo() because it doesn't work in unit tests.
        def add_task_for_repo(repo, task_name, action, **kwargs):
            cleanup = test_handler.initialize_handler(
                tasks.CleanUpInTestMode, action, repo=repo, params=kwargs)
            cleanup.set_listener(listener)
            cleanup.get()

        self.mox.StubOutWithMock(cleanup, 'add_task_for_repo')
        (cleanup.add_task_for_repo(
                'haiti',
                mox.IsA(str),
                mox.IsA(str),
                utcnow=str(calendar.timegm(utcnow.utctimetuple())),
                cursor=mox.IsA(str),
                queue_name=mox.IsA(str)).
            WithSideEffects(add_task_for_repo).MultipleTimes())

        def raise_deadline_exceeded_error(_):
            raise runtime.DeadlineExceededError()

        self.mox.StubOutWithMock(listener, 'before_deletion')
        listener.before_deletion(self.key_p1)
        listener.before_deletion(self.key_p2).WithSideEffects(
            raise_deadline_exceeded_error)
        listener.before_deletion(self.key_p2)

        self.mox.ReplayAll()

        config.set(test_mode=True, repo='haiti')
        # This should run multiple tasks and finally deletes all records.
        cleanup.get()
        assert db.get(self.key_p1) is None
        assert db.get(self.key_p2) is None

        self.mox.UnsetStubs()
        self.mox.VerifyAll()
Example #7
0
    def test_get_photo_url(self):
        entity = model.Photo.create('haiti', image_data='xyz')
        entity.put()
        id = entity.key().name().split(':')[1]

        os.environ['HTTP_HOST'] = 'example.appspot.com'
        ph = test_handler.initialize_handler(photo.Handler,
                                             'photo',
                                             environ=os.environ)
        self.assertEquals('http://example.appspot.com/haiti/photo?id=%s' % id,
                          photo.get_photo_url(entity, ph))
Example #8
0
    def test_get_photo_url(self):
        entity = model.Photo.create('haiti', image_data='xyz')
        entity.put()
        id = entity.key().name().split(':')[1]

        os.environ['HTTP_HOST'] = 'example.appspot.com'
        ph = test_handler.initialize_handler(
            photo.Handler, 'photo', environ=os.environ)
        self.assertEquals(
            'http://example.appspot.com/haiti/photo?id=%s' % id,
            photo.get_photo_url(entity, ph))
Example #9
0
 def setUp(self):
     self.testbed = testbed.Testbed()
     self.testbed.activate()
     self.testbed.init_user_stub()
     self.testbed.init_datastore_v3_stub()
     self.testbed.init_memcache_stub()
     self.testbed.init_mail_stub()
     # root_path must be set the the location of queue.yaml.
     # Otherwise, only the 'default' queue will be available.
     path_to_app = os.path.join(os.path.dirname(__file__), '../app')
     self.testbed.init_taskqueue_stub(root_path=path_to_app)
     model.Repo(key_name='haiti').put()
     self.handler = test_handler.initialize_handler(
         handler_class=tasks.NotifyManyUnreviewedNotes,
         action=tasks.NotifyManyUnreviewedNotes.ACTION,
         repo='haiti', environ=None, params=None)
Example #10
0
 def testPingIndexer(self):
     # We set it to "really" ping index servers for the test. Since we're
     # also mocking out requests.get though, it won't really succeed in
     # pinging anything.
     config.set(ping_sitemap_indexers=True)
     with patch('requests.get') as requests_mock:
         handler = test_handler.initialize_handler(
             sitemap.SiteMapPing,
             action='sitemap/ping',
             repo='global',
             params={'search_engine': 'google'})
         handler.get()
         assert len(requests_mock.call_args_list) == 1
         call_args, _ = requests_mock.call_args_list[0]
         assert call_args[0] == ('https://www.google.com/ping?sitemap='
                                 'https%3A//localhost/global/sitemap')
Example #11
0
 def setUp(self):
     self.testbed = testbed.Testbed()
     self.testbed.activate()
     self.testbed.init_user_stub()
     self.testbed.init_datastore_v3_stub()
     self.testbed.init_memcache_stub()
     self.testbed.init_mail_stub()
     # root_path must be set the the location of queue.yaml.
     # Otherwise, only the 'default' queue will be available.
     path_to_app = os.path.join(os.path.dirname(__file__), '../app')
     self.testbed.init_taskqueue_stub(root_path=path_to_app)
     model.Repo(key_name='haiti').put()
     self.handler = test_handler.initialize_handler(
         handler_class=tasks.NotifyManyUnreviewedNotes,
         action=tasks.NotifyManyUnreviewedNotes.ACTION,
         repo='haiti',
         environ=None,
         params=None)
Example #12
0
 def test_upload_photo(self):
     person, note = self.setup_person_and_note()
     photo_url = 'http://www.example.com/photo.jpg'
     photo_response = mock.MagicMock()
     photo_response.content = open('tests/testdata/tiny_image.png').read()
     params = {
         'text': 'here is some text',
         'author_name': 'Max',
         'author_email': '*****@*****.**',
         'id': person.key().name().split(':')[1],
         'note_photo_url': photo_url,
     }
     handler = test_handler.initialize_handler(add_note.Handler,
                                               'add_note',
                                               params=params)
     with mock.patch('requests.get') as mock_requests_get:
         mock_requests_get.return_value = photo_response
         handler.post()
         # make sure create.py's create_photo_from_input was called
         mock_requests_get.assert_called_once_with(photo_url)
Example #13
0
    def test_delete_expired(self):
        """Test the flagging and deletion of expired records."""

        def run_delete_expired_task():
            """Runs the DeleteExpired task."""
            test_handler.initialize_handler(tasks.DeleteExpired,
                                            tasks.DeleteExpired.ACTION).get()

        def assert_past_due_count(expected):
            actual = len(list(model.Person.past_due_records(repo='haiti')))
            assert actual == expected

        # This test sets up two Person entities, self.p1 and self.p2.
        # self.p1 is deleted in two stages (running DeleteExpired once during
        # the grace period, then again after the grace period); self.p2 is
        # deleted in one stage (running DeleteExpired after the grace period).

        # Initial state: two Persons and one Note, nothing expired yet.
        assert model.Person.all().count() == 2
        assert_past_due_count(0)
        assert model.Note.get('haiti', self.note_id)
        assert db.get(self.photo_key)

        # verify schedule_next_task does the right thing.
        query = model.Person.all()
        query.get()
        cursor = query.cursor()
        self.mox = mox.Mox()
        self.mox.StubOutWithMock(taskqueue, 'add')
        taskqueue.add(method='GET',
                      url='/haiti/tasks/delete_expired',
                      params={'cursor': cursor, 'queue_name': 'expiry'},
                      name=mox.IsA(str))
        self.mox.ReplayAll()
        delexp = test_handler.initialize_handler(tasks.DeleteExpired,
                                                 tasks.DeleteExpired.ACTION)
        delexp.schedule_next_task(query.cursor())
        self.mox.VerifyAll()


        run_delete_expired_task()

        # Confirm that DeleteExpired had no effect.
        assert model.Person.all().count() == 2
        assert_past_due_count(0)
        assert db.get(self.key_p1).source_date == datetime.datetime(2010, 1, 1)
        assert db.get(self.key_p1).entry_date == datetime.datetime(2010, 1, 1)
        assert db.get(self.key_p1).expiry_date == datetime.datetime(2010, 2, 1)
        assert model.Note.get('haiti', self.note_id)
        assert db.get(self.photo_key)

        # Advance to just after the expiry_date of self.p1.
        set_utcnow_for_test(datetime.datetime(2010, 2, 2))

        # self.p1 should now be past due.
        assert model.Person.all().count() == 2
        assert_past_due_count(1)
        assert db.get(self.key_p1).source_date == datetime.datetime(2010, 1, 1)
        assert db.get(self.key_p1).entry_date == datetime.datetime(2010, 1, 1)
        assert db.get(self.key_p1).expiry_date == datetime.datetime(2010, 2, 1)
        assert model.Note.get('haiti', self.note_id)
        assert db.get(self.photo_key)

        self.mox = mox.Mox()
        self.mox.StubOutWithMock(taskqueue, 'add')
        taskqueue.add(queue_name='send-mail',
                      url='/global/admin/send_mail',
                      params=mox.IsA(dict))
        self.mox.ReplayAll()
        run_delete_expired_task()
        self.mox.VerifyAll()

        # Confirm that DeleteExpired set is_expired and updated the timestamps
        # on self.p1, but did not wipe its fields or delete the Note or Photo.
        assert model.Person.all().count() == 1
        assert_past_due_count(1)
        assert db.get(self.key_p1).source_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).entry_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).expiry_date == datetime.datetime(2010, 2, 1)
        assert db.get(self.key_p1).is_expired == True
        assert model.Note.get('haiti', self.note_id) is None  # Note is hidden
        assert db.get(self.n1_1.key())  # but the Note entity still exists
        assert db.get(self.photo_key)

        # Advance past the end of the expiration grace period of self.p1.
        set_utcnow_for_test(datetime.datetime(2010, 2, 5))

        # Confirm that nothing has changed yet.
        assert model.Person.all().count() == 1
        assert_past_due_count(1)
        assert db.get(self.key_p1).source_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).entry_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).expiry_date == datetime.datetime(2010, 2, 1)
        assert db.get(self.key_p1).is_expired == True
        assert model.Note.get('haiti', self.note_id) is None  # Note is hidden
        assert db.get(self.n1_1.key())  # but the Note entity still exists
        assert db.get(self.photo_key)

        run_delete_expired_task()

        # Confirm that the task wiped self.p1 without changing the timestamps,
        # and deleted the related Note and Photo.
        assert model.Person.all().count() == 1
        assert_past_due_count(1)
        assert db.get(self.key_p1).source_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).entry_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).expiry_date == datetime.datetime(2010, 2, 1)
        assert db.get(self.key_p1).is_expired == True
        assert db.get(self.key_p1).given_name is None
        assert model.Note.get('haiti', self.note_id) is None  # Note is hidden
        assert db.get(self.n1_1.key()) is None  # Note entity is actually gone
        assert db.get(self.photo_key) is None  # Photo entity is gone

        # Advance past the end of the expiration grace period for self.p2.
        set_utcnow_for_test(datetime.datetime(2010, 3, 15))

        # Confirm that both records are now counted as past due.
        assert model.Person.all().count() == 1
        assert_past_due_count(2)
        assert db.get(self.key_p1).source_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).entry_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).expiry_date == datetime.datetime(2010, 2, 1)
        assert db.get(self.key_p2).source_date == datetime.datetime(2010, 1, 1)
        assert db.get(self.key_p2).entry_date == datetime.datetime(2010, 1, 1)
        assert db.get(self.key_p2).expiry_date == datetime.datetime(2010, 3, 1)

        run_delete_expired_task()

        # Confirm that the task wiped self.p2 as well.
        assert model.Person.all().count() == 0
        assert_past_due_count(2)
        assert db.get(self.key_p1).is_expired == True
        assert db.get(self.key_p1).given_name is None
        assert db.get(self.key_p1).source_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).entry_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).expiry_date == datetime.datetime(2010, 2, 1)
        assert db.get(self.key_p2).is_expired == True
        assert db.get(self.key_p2).given_name is None
        assert db.get(self.key_p2).source_date == datetime.datetime(2010, 3, 15)
        assert db.get(self.key_p2).entry_date == datetime.datetime(2010, 3, 15)
        assert db.get(self.key_p2).expiry_date == datetime.datetime(2010, 3, 1)
Example #14
0
 def run_clean_up_in_test_mode_task():
     """Runs the CleanUpInTestMode task."""
     test_handler.initialize_handler(
         tasks.CleanUpInTestMode, tasks.CleanUpInTestMode.ACTION).get()
Example #15
0
    def test_clean_up_in_test_mode(self):
        """Test the clean up in test mode."""
        def run_clean_up_in_test_mode_task():
            """Runs the CleanUpInTestMode task."""
            test_handler.initialize_handler(
                tasks.CleanUpInTestMode, tasks.CleanUpInTestMode.ACTION).get()

        tasks.CleanUpInTestMode.DELETION_AGE_SECONDS = 2 * 3600  # 2 hours

        # entry_date of p3 is 4 hours after p1 and p2.
        self.p3 = model.Person.create_original(
            'haiti',
            first_name='Taro',
            last_name='Google',
            home_street='Roppongi',
            home_city='Minato',
            home_state='Tokyo',
            source_date=datetime.datetime(2010, 1, 1),
            entry_date=datetime.datetime(2010, 1, 1, 4, 0, 0),
            expiry_date=datetime.datetime(2010, 3, 1),
            other='')
        self.key_p3 = db.put(self.p3)
        self.to_delete.append(self.p3)

        # Initial state: three Persons and one Note.
        assert model.Person.all().count() == 3
        assert model.Note.get('haiti', self.note_id)
        assert db.get(self.photo_key)
        assert db.get(self.key_p1).is_expired == False  # still exists
        assert db.get(self.key_p2).is_expired == False
        assert db.get(self.key_p3).is_expired == False

        # verify schedule_next_task does the right thing.
        utcnow = datetime.datetime(2010, 1, 1)
        config.set(test_mode=True, repo='haiti')
        query = model.Person.all()
        query.get()
        self.mox = mox.Mox()
        self.mox.StubOutWithMock(taskqueue, 'add')
        taskqueue.add(method='GET',
                      url='/haiti/tasks/clean_up_in_test_mode',
                      params={
                          'cursor': query.cursor(),
                          'utcnow':
                          str(calendar.timegm(utcnow.utctimetuple())),
                          'queue_name': 'clean_up_in_test_mode',
                      },
                      name=mox.IsA(str))
        self.mox.ReplayAll()
        cleanup = \
            test_handler.initialize_handler(tasks.CleanUpInTestMode,
                                            tasks.CleanUpInTestMode.ACTION)
        cleanup.schedule_next_task(query.cursor(), utcnow)
        self.mox.UnsetStubs()
        self.mox.VerifyAll()

        # Nothing happens if test_mode is False.
        config.set(test_mode=False, repo='haiti')
        set_utcnow_for_test(datetime.datetime(2010, 6, 1))
        run_clean_up_in_test_mode_task()
        assert db.get(self.key_p1).is_expired == False  # still exists
        assert db.get(self.key_p2).is_expired == False
        assert db.get(self.key_p3).is_expired == False

        # Records with entry_date before 2010-01-01 3:00 are deleted.
        config.set(test_mode=True, repo='haiti')
        set_utcnow_for_test(datetime.datetime(2010, 1, 1, 5, 0, 0))
        run_clean_up_in_test_mode_task()
        assert db.get(self.key_p1) is None
        assert db.get(self.key_p2) is None
        assert db.get(self.key_p3).is_expired == False  # still exists

        # All records are deleted.
        config.set(test_mode=True, repo='haiti')
        set_utcnow_for_test(datetime.datetime(2010, 1, 1, 7, 0, 0))
        run_clean_up_in_test_mode_task()
        assert db.get(self.key_p1) is None
        assert db.get(self.key_p2) is None
        assert db.get(self.key_p3) is None
Example #16
0
 def run_delete_expired_task():
     """Runs the DeleteExpired task."""
     test_handler.initialize_handler(tasks.DeleteExpired,
                                     tasks.DeleteExpired.ACTION).get()
Example #17
0
    def test_delete_expired(self):
        """Test the flagging and deletion of expired records."""
        def run_delete_expired_task():
            """Runs the DeleteExpired task."""
            test_handler.initialize_handler(tasks.DeleteExpired,
                                            tasks.DeleteExpired.ACTION).get()

        def assert_past_due_count(expected):
            actual = len(list(model.Person.past_due_records(repo='haiti')))
            assert actual == expected

        # This test sets up two Person entities, self.p1 and self.p2.
        # self.p1 is deleted in two stages (running DeleteExpired once during
        # the grace period, then again after the grace period); self.p2 is
        # deleted in one stage (running DeleteExpired after the grace period).

        # Initial state: two Persons and one Note, nothing expired yet.
        assert model.Person.all().count() == 2
        assert_past_due_count(0)
        assert model.Note.get('haiti', self.note_id)
        assert db.get(self.photo_key)

        # verify schedule_next_task does the right thing.
        query = model.Person.all()
        query.get()
        cursor = query.cursor()
        self.mox = mox.Mox()
        self.mox.StubOutWithMock(taskqueue, 'add')
        taskqueue.add(method='GET',
                      url='/haiti/tasks/delete_expired',
                      params={
                          'cursor': cursor,
                          'queue_name': 'expiry'
                      },
                      name=mox.IsA(str))
        self.mox.ReplayAll()
        delexp = test_handler.initialize_handler(tasks.DeleteExpired,
                                                 tasks.DeleteExpired.ACTION)
        delexp.schedule_next_task(query.cursor())
        self.mox.VerifyAll()

        run_delete_expired_task()

        # Confirm that DeleteExpired had no effect.
        assert model.Person.all().count() == 2
        assert_past_due_count(0)
        assert db.get(self.key_p1).source_date == datetime.datetime(2010, 1, 1)
        assert db.get(self.key_p1).entry_date == datetime.datetime(2010, 1, 1)
        assert db.get(self.key_p1).expiry_date == datetime.datetime(2010, 2, 1)
        assert model.Note.get('haiti', self.note_id)
        assert db.get(self.photo_key)

        # Advance to just after the expiry_date of self.p1.
        set_utcnow_for_test(datetime.datetime(2010, 2, 2))

        # self.p1 should now be past due.
        assert model.Person.all().count() == 2
        assert_past_due_count(1)
        assert db.get(self.key_p1).source_date == datetime.datetime(2010, 1, 1)
        assert db.get(self.key_p1).entry_date == datetime.datetime(2010, 1, 1)
        assert db.get(self.key_p1).expiry_date == datetime.datetime(2010, 2, 1)
        assert model.Note.get('haiti', self.note_id)
        assert db.get(self.photo_key)

        self.mox = mox.Mox()
        self.mox.StubOutWithMock(taskqueue, 'add')
        taskqueue.add(queue_name='send-mail',
                      url='/global/admin/send_mail',
                      params=mox.IsA(dict))
        self.mox.ReplayAll()
        run_delete_expired_task()
        self.mox.VerifyAll()

        # Confirm that DeleteExpired set is_expired and updated the timestamps
        # on self.p1, but did not wipe its fields or delete the Note or Photo.
        assert model.Person.all().count() == 1
        assert_past_due_count(1)
        assert db.get(self.key_p1).source_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).entry_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).expiry_date == datetime.datetime(2010, 2, 1)
        assert db.get(self.key_p1).is_expired == True
        assert model.Note.get('haiti', self.note_id) is None  # Note is hidden
        assert db.get(self.n1_1.key())  # but the Note entity still exists
        assert db.get(self.photo_key)

        # Advance past the end of the expiration grace period of self.p1.
        set_utcnow_for_test(datetime.datetime(2010, 2, 5))

        # Confirm that nothing has changed yet.
        assert model.Person.all().count() == 1
        assert_past_due_count(1)
        assert db.get(self.key_p1).source_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).entry_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).expiry_date == datetime.datetime(2010, 2, 1)
        assert db.get(self.key_p1).is_expired == True
        assert model.Note.get('haiti', self.note_id) is None  # Note is hidden
        assert db.get(self.n1_1.key())  # but the Note entity still exists
        assert db.get(self.photo_key)

        run_delete_expired_task()

        # Confirm that the task wiped self.p1 without changing the timestamps,
        # and deleted the related Note and Photo.
        assert model.Person.all().count() == 1
        assert_past_due_count(1)
        assert db.get(self.key_p1).source_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).entry_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).expiry_date == datetime.datetime(2010, 2, 1)
        assert db.get(self.key_p1).is_expired == True
        assert db.get(self.key_p1).given_name is None
        assert model.Note.get('haiti', self.note_id) is None  # Note is hidden
        assert db.get(self.n1_1.key()) is None  # Note entity is actually gone
        assert db.get(self.photo_key) is None  # Photo entity is gone

        # Advance past the end of the expiration grace period for self.p2.
        set_utcnow_for_test(datetime.datetime(2010, 3, 15))

        # Confirm that both records are now counted as past due.
        assert model.Person.all().count() == 1
        assert_past_due_count(2)
        assert db.get(self.key_p1).source_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).entry_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).expiry_date == datetime.datetime(2010, 2, 1)
        assert db.get(self.key_p2).source_date == datetime.datetime(2010, 1, 1)
        assert db.get(self.key_p2).entry_date == datetime.datetime(2010, 1, 1)
        assert db.get(self.key_p2).expiry_date == datetime.datetime(2010, 3, 1)

        run_delete_expired_task()

        # Confirm that the task wiped self.p2 as well.
        assert model.Person.all().count() == 0
        assert_past_due_count(2)
        assert db.get(self.key_p1).is_expired == True
        assert db.get(self.key_p1).given_name is None
        assert db.get(self.key_p1).source_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).entry_date == datetime.datetime(2010, 2, 2)
        assert db.get(self.key_p1).expiry_date == datetime.datetime(2010, 2, 1)
        assert db.get(self.key_p2).is_expired == True
        assert db.get(self.key_p2).given_name is None
        assert db.get(self.key_p2).source_date == datetime.datetime(
            2010, 3, 15)
        assert db.get(self.key_p2).entry_date == datetime.datetime(2010, 3, 15)
        assert db.get(self.key_p2).expiry_date == datetime.datetime(2010, 3, 1)
Example #18
0
 def initialize_handler(self, handler_class):
     return test_handler.initialize_handler(handler_class,
                                            handler_class.ACTION)
Example #19
0
 def add_task_for_repo(repo, task_name, action, **kwargs):
     test_handler.initialize_handler(
         cleanup, action, repo=repo, params=kwargs)
     cleanup.get()
Example #20
0
 def add_task_for_repo(repo, task_name, action, **kwargs):
     cleanup = test_handler.initialize_handler(
         tasks.CleanUpInTestMode, action, repo=repo, params=kwargs)
     cleanup.set_listener(listener)
     cleanup.get()
Example #21
0
 def run_delete_expired_task():
     """Runs the DeleteExpired task."""
     test_handler.initialize_handler(tasks.DeleteExpired,
                                     tasks.DeleteExpired.ACTION).get()
Example #22
0
 def run_clean_up_in_test_mode_task():
     """Runs the CleanUpInTestMode task."""
     test_handler.initialize_handler(tasks.CleanUpInTestMode,
                                     tasks.CleanUpInTestMode.ACTION
                                     ).get()
Example #23
0
    def test_clean_up_in_test_mode(self):
        """Test the clean up in test mode."""

        def run_clean_up_in_test_mode_task():
            """Runs the CleanUpInTestMode task."""
            test_handler.initialize_handler(tasks.CleanUpInTestMode,
                                            tasks.CleanUpInTestMode.ACTION
                                            ).get()

        tasks.CleanUpInTestMode.DELETION_AGE_SECONDS = 2 * 3600  # 2 hours

        # entry_date of p3 is 4 hours after p1 and p2.
        self.p3 = model.Person.create_original(
            'haiti',
            first_name='Taro',
            last_name='Google',
            home_street='Roppongi',
            home_city='Minato',
            home_state='Tokyo',
            source_date=datetime.datetime(2010, 1, 1),
            entry_date=datetime.datetime(2010, 1, 1, 4, 0, 0),
            expiry_date=datetime.datetime(2010, 3, 1),
            other='')
        self.key_p3 = db.put(self.p3)
        self.to_delete.append(self.p3)

        # Initial state: three Persons and one Note.
        assert model.Person.all().count() == 3
        assert model.Note.get('haiti', self.note_id)
        assert db.get(self.photo_key)
        assert db.get(self.key_p1).is_expired == False  # still exists
        assert db.get(self.key_p2).is_expired == False
        assert db.get(self.key_p3).is_expired == False

        # verify schedule_next_task does the right thing.
        utcnow = datetime.datetime(2010, 1, 1)
        config.set(test_mode=True, repo='haiti')
        query = model.Person.all()
        query.get()
        self.mox = mox.Mox()
        self.mox.StubOutWithMock(taskqueue, 'add')
        taskqueue.add(method='GET',
                      url='/haiti/tasks/clean_up_in_test_mode',
                      params={
                          'cursor': query.cursor(),
                          'utcnow': str(calendar.timegm(utcnow.utctimetuple())),
                          'queue_name': 'clean_up_in_test_mode',
                      },
                      name=mox.IsA(str))
        self.mox.ReplayAll()
        cleanup = \
            test_handler.initialize_handler(tasks.CleanUpInTestMode,
                                            tasks.CleanUpInTestMode.ACTION)
        cleanup.schedule_next_task(query.cursor(), utcnow)
        self.mox.UnsetStubs()
        self.mox.VerifyAll()

        # Nothing happens if test_mode is False.
        config.set(test_mode=False, repo='haiti')
        set_utcnow_for_test(datetime.datetime(2010, 6, 1))
        run_clean_up_in_test_mode_task()
        assert db.get(self.key_p1).is_expired == False  # still exists
        assert db.get(self.key_p2).is_expired == False
        assert db.get(self.key_p3).is_expired == False

        # Records with entry_date before 2010-01-01 3:00 are deleted.
        config.set(test_mode=True, repo='haiti')
        set_utcnow_for_test(datetime.datetime(2010, 1, 1, 5, 0, 0))
        run_clean_up_in_test_mode_task()
        assert db.get(self.key_p1) is None
        assert db.get(self.key_p2) is None
        assert db.get(self.key_p3).is_expired == False  # still exists

        # All records are deleted.
        config.set(test_mode=True, repo='haiti')
        set_utcnow_for_test(datetime.datetime(2010, 1, 1, 7, 0, 0))
        run_clean_up_in_test_mode_task()
        assert db.get(self.key_p1) is None
        assert db.get(self.key_p2) is None
        assert db.get(self.key_p3) is None
Example #24
0
 def initialize_handler(self, handler):
     test_handler.initialize_handler(handler, handler.ACTION)
     return handler
Example #25
0
 def initialize_handler(self, handler_class):
     return test_handler.initialize_handler(
         handler_class, handler_class.ACTION)