How to run app engine unit tests in pycharm? - google-app-engine

I'm trying to run the following test ile
from google import appengine
from google.appengine.ext import testbed
from google.appengine.ext import ndb
import unittest
import passwordproperty
class MyTestCase(unittest.TestCase):
def setUp(self):
self.tb = testbed.Testbed()
self.tb.setup_env()
self.tb.activate()
self.tb.init_datastore_v3_stub()
def tearDown(self):
self.tb.deactivate()
... tests ...
if __name__ == '__main__':
unittest.main()
But when I try to run this file, I get the following error:
google.appengine.tools.devappserver2.wsgi_server.BindError: Unable to bind localhost:8080
So it looked like something was almready using port 8080, so I ran lsof -i :8080, which came up empty, and navigating to localhost without a port also doesn't give me anything. What's going on?

In the run/debug configuration create project of "python tests" and set the values like working directory , class (class name of the test class) and it will work

Related

Google App Engine : import myfile gives 502 bad gateway

App runs fine with just the app.yaml, main.py and requiremets.txt (Flask)
I also have a mymodule.py file.
If, in main.py, I do
import mymodule
or
from mymodule import myfunc
I get the 502 Bad Gateway
runs fine locally though..
Please follow the official documentation on :
Specifying Private Dependencies
To use private dependencies:
1.Run pip install -t lib my_module to copy dependencies into a local
folder named lib.
2.Add an empty __init__.py file to the lib directory to make it a
module.
3.Import the module in your app. For example:
import lib.my_module

Flask-SQLAlchemy (mssql+pyodbc) and Existing Models with Blueprint

I have this following Flask application/folder structure in python3.6 Enviroment:
site/
__init__.py
models.py
static/
templates/
dashboard/
index.html
login/
index.html
views/
__init__.py
login.py
config.py
run.py
My files looks like this:
run.py
from site import app
app.run()
config.py
class BaseConfig(object):
DEBUG = False
TESTING = False
WFT_CSRF_ENABLED = True
class DevConfig(BaseConfig):
DEBUG = True
SECRET_KEY = 'dev'
SQLALCHEMY_DATABASE_URI = 'DRIVER={ODBC Driver 11 for SQL Server};SERVER=myServer;DATABASE=myDB;TRUSTED_CONNECTION=yes'
SQLALCHEMY_TRACK_MODIFICATIONS = False
site/init.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object('config.DevConfig')
db = SQLAlchemy(app)
from site.views import login
app.register_blueprint(login.mod)
site/views/login.py
from flask import Blueprint, render_template
mod = Blueprint('login', __name__)
#mod.route('/')
def index():
return render_template('login/index.html')
site/models.py
class User(db.Model):
__table__ = db.Model.metadata.tables['users']
def __repr__(self):
return '<User %r>' % self.username
what i am trying to do is to connect and use a existing model in my ms-sql database which is called user.
I am very new with Flask, Flask-SQLAlchemy, Blueprint and Python and I am stucked on how to use the db.Model in my login.py. How to achieved this in a Blueprint paradigm?
Any help and direction will be much appriciated and thanks for the help in advanced!
regards,
maki
you can add the following lines to your login.py:
from site import db
from site.models import User
to add an new user to the database you can use this :
new_user = User()
db.session.add(new_user)
db.session.commit()
and add this in your user table :
_tablename__ = 'user'
you will need to change you database URI in the config by following the syntax suggested in the flask_sqlalchemy doc
replace what you have by this :
SQLALCHEMY_DATABASE_URI : mysql://username:password#server_adress/db_name
for user authentification i can suggest you flask_login

How to stop Google App Engine Launcher logging the whole html content?

I use the following code to use webapp2
import webapp2
from webapp2_extras import i18n, jinja2
class Test(webapp2.RequestHandler):
#webapp2.cached_property
def jinja2(self):
# Returns a Jinja2 renderer cached in the app registry.
return jinja2.get_jinja2(app=self.app)
def render(self, _template, **context):
# Renders a template and writes the result to the response.
rv = self.jinja2.render_template(_template, **context)
self.response.write(rv)
def get(self):
self.render('index2.html')
app = webapp2.WSGIApplication([('/',Test)], debug=False)
app.run()
To be simple, we just make index2.html is full of 1000 test
If you run it, you will find that the Google App Engine Launcher log windows is full of test.
It's rather terrible when you are debugging a real website.
But I find that if index2.html contains just few words like 10 test. The test will not appear in logging window.
Any solution?
It looks like you are running two separate instances of your application.
The first is running through the development server, the second is actually running and outputting to the terminal. You can see this by adding a log output to your application. You can see that when you go to your site, the log is written two twice.
You can fix this by removing the line:
app.run()
This is not necessary when running on App Engine. Your app.yaml file references the app directly:
handlers:
- url: .*
script: guestbook.app # Notice the .app here.

How to use use_library('django','1.2')

I'm learning developing in Google App Engine.
This is one of the code from the tutorial, http://code.google.com/appengine/docs/python/gettingstarted/usingwebapp.html
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class MainPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')
application = webapp.WSGIApplication(
[('/', MainPage)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
I've almost identical code. I sometime get warning:
WARNING 2011-06-30 13:10:44,443 init.py:851] You are using the default Django version (0.96). The default Django version will change in an App Engine release in the near future. Please call use_library() to explicitly select a Django version. For more information see http://code.google.com/appengine/docs/python/tools/libraries.html#Django
Can anyone please re factor the above code with use_library(). I'm not sure how to start and where to use use_library and what to do with webapp.
Thanks in advance.
The above code should not require you to call use_library directly.
If you create a new file in the root directory of your application named appengine_config.py and add the following line to it:
# Make webapp.template use django 1.2
webapp_django_version = '1.2'
try putting this code on top of your module :
import os
from google.appengine.dist import use_library
use_library('django', '1.2')
In the current version this is even simpler as third-party libraries are now specified in app.yaml
libraries:
- name: django
version: "1.2"
You can also use webapp2 that includes Django’s templating engine.
import webapp2
from google.appengine.ext.webapp2 import template

Errors being logged for only HTTP and not HTTPS but the response is 200?

I've just deployed an app to App Engine and everything is working great. I'm using Django 1.1.
I've got a page that returns some JSON/JSONP data. Everything works great but my logs are filling up with errors that have no real information in them. I'm ONLY getting these errors when accessing the app using HTTP, NO ERRORS when it's HTTPS.
The client still receives a 200 and the data is returned fine either way. It's just that App Engine is logging an error when the request is HTTP. The errors that are logged all look like the following.
+ 11-16 01:02PM 20.181 /some/url?jsonp=1231234344 200 16ms 8cpu_ms 8api_cpu_ms 0kb....
E 11-16 01:02PM 20.196
E 11-16 01:02PM 20.197
E 11-16 01:02PM 20.197 www.myapplicationname.appspot.com |1| www.myapp
E 11-16 01:02PM 20.197
What does that mean? why does everything work great, but I always get these logs for non-ssl pages only?
update:
This is the only logging reference I have in my code that would involve the route causing the error. It's in my main.py
#import logging, os
import os
# Must set this env var before importing any part of Django
os.environ['DJANGO_SETTINGS_MODULE'] = 'stampinstats.settings'
# django 1.1 is setup here to avoid conficts with default 0.96
import appengine_config
# Force Django to reload its settings
from django.conf import settings
settings._target = None
import logging
import django.core.handlers.wsgi
import django.db
# Google App Engine imports
from google.appengine.ext.webapp import util
def log_exception(sender, **kwargs):
logging.exception('Exception in request:')
sig = django.dispatch.Signal()
sig.connect(log_exception)
sig.disconnect(django.db._rollback_on_exception)
def main():
# Create a Django application for WSGI
application = django.core.handlers.wsgi.WSGIHandler()
# Run the WSGI CGI handler with the application
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
Thanks
Is something in your code calling logging.error()?

Resources