Local testing support for Google App Engine

If you run your code on Google’s servers or under dev_appserver, the api’s are already configured.

To run outside this environment, you need to install a test environment and api stubs.

Methods
Classes and Modules
Class Public methods
app_dir()

The application directory, or ’.’ if not set. Composite index definitions are written to “#{app_dir}/WEB-INF/”.

# File lib/appengine-apis/testing.rb, line 121
      def app_dir
        file = factory.getApplicationDirectory
        file && file.path
      end
app_dir=(dir)

Sets the application directory. Should be called before creating stubs.

Composite index definitions are written to “#{app_dir}/WEB-INF/”.

# File lib/appengine-apis/testing.rb, line 130
      def app_dir=(dir)
        factory.setApplicationDirectory(java.io.File.new(dir))
      end
boot(app_dir=nil)

Loads stub API implementations if no API implementation is currently configured.

Sets up a datastore saved to disk in app_dir. app_dir defaults to ENV[‘APPLICATION_ROOT’] or ’.’ if not specified.

Does nothing is APIs are already configured (e.g. in production).

As a shortcut you can use

  require 'appengine-apis/local_boot'
# File lib/appengine-apis/testing.rb, line 184
      def boot(app_dir=nil)
        if AppEngine::ApiProxy.current_environment &&
            AppEngine::ApiProxy.getDelegate
          return
        end
        app_dir ||= ENV['APPLICATION_ROOT'] || '.'
        unless AppEngine::ApiProxy.current_environment
          env = install_test_env
          appid = get_app_id(app_dir)
          env.appid = appid if appid
        end
        unless AppEngine::ApiProxy.getDelegate
          self.app_dir = app_dir
          install_api_stubs
        end
      end
get_app_id(app_dir)

Looks for app.yaml or WEB-INF/appengine-web.xml in app_dir and parses the application id.

# File lib/appengine-apis/testing.rb, line 203
      def get_app_id(app_dir)
        require 'appengine-rack'
        app_id = AppEngine::Rack.app.id
        return app_id if app_id
        aeweb_path = File.join(app_dir, 'WEB-INF', 'appengine-web.xml')
        if File.exist?(aeweb_path)
          require 'rexml/document'
          aeweb = REXML::Document.new(File.new(aeweb_path))
          return aeweb.root.elements['application'].text
        end
      end
install_api_stubs()

Install stub apis. The datastore will be written to the disk inside app_dir.

You could potentially use this to run under a ruby web server instead of dev_appserver. In that case you will need to install and configure a test environment for each request.

# File lib/appengine-apis/testing.rb, line 160
      def install_api_stubs
        current_delegate = ApiProxy.getDelegate
        current_delegate.stop if current_delegate.respond_to? :stop

        self.app_dir = '.' if app_dir.nil?
        delegate = factory.create
        ApiProxy::setDelegate(delegate)
        at_exit do
          delegate.stop
        end
        delegate
      end
install_test_datastore()

Install stub apis and force all datastore operations to use an in-memory datastore.

You may call this multiple times to reset to a new in-memory datastore.

# File lib/appengine-apis/testing.rb, line 138
      def install_test_datastore
        delegate = install_api_stubs
        lds = Java.ComGoogleAppengineApiDatastoreDev.LocalDatastoreService
        
        delegate.set_property(
            lds::NO_STORAGE_PROPERTY, "true")
        delegate.set_property(
            lds::MAX_QUERY_LIFETIME_PROPERTY,
            java.lang.Integer::MAX_VALUE.to_s)
        delegate.set_property(
            lds::MAX_TRANSACTION_LIFETIME_PROPERTY,
            java.lang.Integer::MAX_VALUE.to_s)
        ApiProxy::setDelegate(delegate)
        delegate
      end
install_test_env()

Install a test environment for the current thread.

You must call this before making any api calls.

Note that Google’s production and local environments are single threaded. You may run into problems if you use multiple threads.

# File lib/appengine-apis/testing.rb, line 104
      def install_test_env
        env = TestEnv.new
        ApiProxy::setEnvironmentForCurrentThread(env)
        env
      end