def test_clean_expired_access_codes(self):
        # add some access codes
        with transaction.manager:
            user1 = User(first_name='John1', last_name='Doe')
            user2 = User(first_name='John2', last_name='Doe')
            user3 = User(first_name='John3', last_name='Doe')
            app = Application(name='Test Application')
            user1.applications.append(app)

            # access code that expires yesterday
            ac1 = AccessCode(
                code='123',
                expiration=datetime.datetime(2015, 7, 13, 10, 0, 0),
                application=app,
                user=user1,
            )

            # access code that expires one second ago
            ac2 = AccessCode(
                code='456',
                expiration=datetime.datetime(2015, 7, 14, 19, 59, 59),
                application=app,
                user=user2,
            )

            # access code that expires in 2 hours
            ac3 = AccessCode(
                code='789',
                expiration=datetime.datetime(2015, 7, 14, 22, 0, 0),
                application=app,
                user=user3,
            )

            Session.add(user1)
            Session.add(user2)
            Session.add(user3)
            Session.add(app)
            Session.add(ac1)
            Session.add(ac2)
            Session.add(ac3)

        self.assertEqual(Session.query(AccessCode).count(), 3)

        sys.argv = ['notused', self.conf_file_path]
        sys.stdout = StringIO()
        result = clean_access_codes()
        self.assertEqual(result, None)
        stdout = sys.stdout.getvalue()
        expected_output = """2 access codes were cleaned
"""
        self.assertEqual(stdout, expected_output)

        self.assertEqual(Session.query(AccessCode).count(), 1)
        try:
            access_code = Session.query(AccessCode).filter(
                AccessCode.code == '789').one()
        except NoResultFound:
            access_code = None

        self.assertNotEqual(access_code, None)
    def test_no_arguments(self):
        # Replace sys argv and stdout
        sys.argv = []
        sys.stdout = StringIO()

        # Call clean access codes with no arguments
        result = clean_access_codes()
        self.assertEqual(result, 2)
        stdout = sys.stdout.getvalue()
        self.assertEqual(stdout, 'You must provide at least one argument\n')