├── .gitignore ├── .travis.yml ├── LICENSE ├── Makefile ├── README.md ├── flexible_src ├── app.yaml ├── main.py └── requirements.txt ├── requirements.txt ├── resources ├── Makefile ├── autogenerated ├── dot-git-hooks-pre-commit ├── get_current_sdk_version.sh ├── requirements.txt └── requirements_flex.txt └── src ├── app.yaml ├── main.py └── tests ├── __init__.py ├── persons.json └── sanity_test.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | 3 | # C extensions 4 | *.so 5 | 6 | # Packages 7 | *.egg 8 | *.egg-info 9 | dist 10 | build 11 | eggs 12 | parts 13 | bin 14 | var 15 | sdist 16 | develop-eggs 17 | .installed.cfg 18 | lib 19 | lib64 20 | __pycache__ 21 | 22 | # Installer logs 23 | pip-log.txt 24 | 25 | # Unit test / coverage reports 26 | .coverage 27 | .tox 28 | nosetests.xml 29 | 30 | # Translations 31 | *.mo 32 | 33 | # Mr Developer 34 | .mr.developer.cfg 35 | .project 36 | .pydevproject 37 | 38 | # Emacs 39 | *# 40 | .#* 41 | *~ 42 | 43 | # Egg and SDK download caches 44 | cache 45 | 46 | # The virtual environments 47 | .env* 48 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "2.7" 4 | addons: 5 | apt: 6 | packages: 7 | - gnulib 8 | - python-dev 9 | - python3-dev 10 | # command to run tests 11 | script: "make travis" 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2014 Ricardo L. A. Banffy 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | include resources/Makefile 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | testable_appengine 2 | ================== 3 | 4 | A testable Python skeleton application for Google App Engine and 5 | AppScale environments. 6 | 7 | ![Demo] 8 | (https://raw.githubusercontent.com/wiki/rbanffy/testable_appengine/screencap.gif) 9 | 10 | Why? 11 | ---- 12 | 13 | Writing properly testable Python applications to run on Google App 14 | Engine is not trivial. Keeping the different application environments 15 | segregated is also an issue if all environments share the same SDK and 16 | Python environment. This template helps you build your application in 17 | such way it's contained in a folder, along with its virtualenvironment 18 | and separate copy of the SDK. It also preinstall ipdb and pudb 19 | debuggers, nose, pytest and webtest testing tools, 20 | appengine-fixture-loader (to load test or bootstrap data), and flake8 to 21 | do style checks on your code. 22 | 23 | This tree was extracted from a larger project that makes use of it and 24 | further evolved on its own, with the enhancements ported back. 25 | 26 | ![nosetests running] 27 | (https://raw.githubusercontent.com/wiki/rbanffy/testable_appengine/screenshot.png) 28 | 29 | Overall Structure 30 | ----------------- 31 | 32 | When you clone the Git repo, you'll have a tree structure not unlike 33 | the one below. This is the starting point for yout app. 34 | 35 | ├── build (will be created when you run `make venv`) 36 | ├── cache (will be created when you run `make venv`) 37 | ├── LICENSE 38 | ├── Makefile (includes resourecs/Makefile) 39 | ├── README.md (this file) 40 | ├── requirements.txt 41 | ├── resources 42 | │   ├── autogenerated 43 | │   ├── get_current_sdk_version.sh 44 | │   ├── Makefile 45 | │   └── requirements.txt (includes ../requirements.txt) 46 | ├── src (contains a sample application) 47 | │   ├── app.yaml 48 | │   └── main.py 49 | └── tests 50 | ├── __init__.py 51 | ├── persons.json 52 | └── sanity_test.py 53 | 54 | Setting up your development environment 55 | --------------------------------------- 56 | 57 | There is a makefile in the root directory. Running `make venv` will 58 | build a virtualenv in the `.env` folder, download the App Engine SDK, 59 | build the appropriate .pth files for your machine and install all 60 | requirements from the `resources/requirements.txt` file. If you wish to 61 | use a Python environment you provide yourself, you can make the 62 | virtualenv youself and run `make libraries` with VENV pointing to your 63 | virtualenv (as in `VENV=.my_env make libraries`) 64 | 65 | The actual makefile is under resources. Your own makefile (with your own 66 | targets) should include it and extend it like the included Makefile 67 | does. 68 | 69 | Please check the makefile before using it, as it may not make sense for 70 | your environment. It was tested on Ubuntu, Fedora, OSX (10.7+, provided 71 | you have functional pip and virtualenv utilities) and Windows 8.1 under 72 | [Cygwin](http://www.cygwin.com/) and Windows 10 under Ubuntu on 73 | Windows. If you are developing under Windows and don't use Cygwin or the 74 | Windows Subsystem for Linux, you are suffering more than you need for no 75 | good reason. 76 | 77 | ![Under Windows+Cygwin] 78 | (https://raw.githubusercontent.com/wiki/rbanffy/testable_appengine/windows.png) 79 | 80 | ![Under Ubuntu on Windows] 81 | (https://raw.githubusercontent.com/wiki/rbanffy/testable_appengine/ubuntu_on_windows.png) 82 | 83 | When done, you can activate your virtualenv with the usual `source 84 | .env/bin/activate` or your favorite virtualenv tool. The tests will not 85 | function outside the local virtualenv. From the virtualenv, invoking 86 | dev_appserver.py and appcfg.py will use the versions in the SDK 87 | downloaded during install (check the `Makefile` for current version). 88 | 89 | Your app should go in the local folder. Run your application using the 90 | dev_appserver.py script from within the virtualenv, as in 91 | `dev_appserver.py .`. Deploying ignores the files that do not belong to 92 | your application (the license, this README, cache, tests, resources and 93 | so on). 94 | 95 | Tests 96 | ----- 97 | 98 | There are tests in the tests folder. The sanity_test.py checks whether 99 | everything is sane after you set up the environment. 100 | 101 | Debugging 102 | --------- 103 | 104 | Debugging works like you'd expect. You just use pdb, ipdb or pudb (ipdb 105 | and pudb seem to have a tendency not to work under the dev_appserver, 106 | but pdb is just fine). Rule is, use ipbd or pudb if you want to debug 107 | something that's called by a test (run something like `pudb 108 | .env/bin/nosetests`), pdb for a live web application. Worst case 109 | scenario is that you just need to edit a little more. You can also 110 | easily use iPython to explore your ideas before you commit them to code. 111 | 112 | ![ipython prompt and ipdb in a test] 113 | (https://raw.githubusercontent.com/wiki/rbanffy/testable_appengine/using_ipdb.png) 114 | 115 | ![Using pudb] 116 | (https://raw.githubusercontent.com/wiki/rbanffy/testable_appengine/pudb.png) 117 | 118 | Contributing 119 | ------------ 120 | 121 | File a bug report (enhancement suggestions are fine too), fork it, PEP-8 122 | it, test it, and, if it works, send a pull request. When in doubt, get 123 | in touch. We'll figure out what needs to be done. 124 | 125 | ![This app is proudly checked with Travis](https://api.travis-ci.org/rbanffy/testable_appengine.svg) 126 | -------------------------------------------------------------------------------- /flexible_src/app.yaml: -------------------------------------------------------------------------------- 1 | runtime: python 2 | vm: true 3 | entrypoint: gunicorn -b :$PORT main:app 4 | 5 | runtime_config: 6 | python_version: 2 7 | -------------------------------------------------------------------------------- /flexible_src/main.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | from flask import Flask 4 | 5 | app = Flask(__name__) 6 | 7 | @app.route('/') 8 | def hello(): 9 | return 'Hello world' 10 | 11 | if __name__ == '__main__': 12 | app.run(host='127.0.0.1', port=8080, debug=True) 13 | -------------------------------------------------------------------------------- /flexible_src/requirements.txt: -------------------------------------------------------------------------------- 1 | flask==1.0 2 | google-cloud==0.20.0 3 | gunicorn==19.6.0 4 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rbanffy/testable_appengine/62d0222416b7efb5aa45512366394d322be960cd/requirements.txt -------------------------------------------------------------------------------- /resources/Makefile: -------------------------------------------------------------------------------- 1 | # Override VERSION if not defined. 2 | VERSION?=$(shell resources/get_current_sdk_version.sh) 3 | 4 | # Override VENV if not defined. 5 | VENV?=.env 6 | VENV_FLEX?=.env_flex 7 | 8 | help: 9 | @echo "Please use \`make ' where is one of" 10 | @echo " setup to setup the virtualenv and git hook." 11 | @echo " venv to build the working virtual environment for the" 12 | @echo " standard environment application, and to install" 13 | @echo " requirements for deployment." 14 | @echo " venv_flex to build the working virtual environment for the" 15 | @echo " flexible environment application, and to install" 16 | @echo " requirements for deployment." 17 | @echo " pycodestyle to run pycodestyle on the code and tests folders." 18 | @echo " pyflakes to run pyflakes on the code and tests folders." 19 | @echo " clean_venv to remove the created virtualenv folders." 20 | @echo " clean_cache to remove the downloaded files/pip cache folder." 21 | @echo " travis runs all tests for the standard environment" 22 | @echo " application (Travis CI hook)." 23 | 24 | # Consciously avoiding "all" target because we may want to use it for building 25 | # the actual product rather than a sane testing environment. 26 | setup: venv 27 | ln ./resources/dot-git-hooks-pre-commit .git/hooks/pre-commit 28 | chmod +x .git/hooks/pre-commit 29 | 30 | venv: virtualenv_std requirements_std appenginesdk 31 | 32 | venv_flex: virtualenv_flex requirements_flex 33 | 34 | # Creates the virtualenv, adds `src` to the python path. 35 | virtualenv_std: 36 | virtualenv $(CURDIR)/$(VENV) 37 | cp $(CURDIR)/resources/autogenerated $(CURDIR)/$(VENV)/lib/python2.7/site-packages/src.pth 38 | echo "$(CURDIR)/src/" >> $(CURDIR)/$(VENV)/lib/python2.7/site-packages/src.pth 39 | 40 | # Creates the virtualenv for the Flexible runtime with Python 3 41 | virtualenv_flex: 42 | virtualenv $(CURDIR)/$(VENV_FLEX) 43 | 44 | # Install all modules that will be used during development. 45 | requirements_std: 46 | $(CURDIR)/$(VENV)/bin/pip install --cache-dir $(CURDIR)/cache -r $(CURDIR)/resources/requirements.txt 47 | 48 | # Install all modules that will be used during development - ignore 49 | # cache because Travis doesn't seem to like it. 50 | requirements_flex: 51 | $(CURDIR)/$(VENV_FLEX)/bin/pip install -r $(CURDIR)/resources/requirements_flex.txt 52 | $(CURDIR)/$(VENV_FLEX)/bin/pip install -r $(CURDIR)/flexible_src/requirements.txt 53 | 54 | # Download the current Appengine SDK, unpack it in the virtualenv's 55 | # lib directory and add it and its patches to the gae.pth file that'll 56 | # set up the virtualenv's path. 57 | appenginesdk: virtualenv_std directories 58 | @wget -c https://storage.googleapis.com/appengine-sdks/featured/google_appengine_$(VERSION).zip -O $(CURDIR)/cache/google_appengine_$(VERSION).zip 59 | @unzip -q -o $(CURDIR)/cache/google_appengine_$(VERSION).zip -d $(CURDIR)/$(VENV)/lib 60 | @ln -svf $(CURDIR)/$(VENV)/lib/google_appengine/*.py $(CURDIR)/$(VENV)/bin/ 61 | @cp $(CURDIR)/resources/autogenerated $(CURDIR)/$(VENV)/lib/python2.7/site-packages/gae.pth 62 | @echo "$(CURDIR)/$(VENV)/lib/google_appengine/" >> $(CURDIR)/$(VENV)/lib/python2.7/site-packages/gae.pth 63 | @echo "import dev_appserver; dev_appserver.fix_sys_path()" >> $(CURDIR)/$(VENV)/lib/python2.7/site-packages/gae.pth 64 | 65 | # This creates directories that will be used during setup processes. 66 | directories: 67 | mkdir -p $(CURDIR)/build $(CURDIR)/cache 68 | 69 | # A useful target for running pycodestyle your source tree. Do a pep8 for everything under `src` and `tests`. 70 | pycodestyle: 71 | find $(CURDIR)/src/ -name *.py -exec $(CURDIR)/$(VENV)/bin/pycodestyle {} \; 72 | find $(CURDIR)/src/tests/ -name *.py -exec $(CURDIR)/$(VENV)/bin/pycodestyle {} \; 73 | 74 | # The same for pyflakes. 75 | pyflakes: 76 | find $(CURDIR)/src/ -name *.py -exec $(CURDIR)/$(VENV)/bin/pyflakes {} \; 77 | find $(CURDIR)/src/tests/ -name *.py -exec $(CURDIR)/$(VENV)/bin/pyflakes {} \; 78 | 79 | clean_dirs: 80 | rm -rf $(CURDIR)/build/* 81 | 82 | clean_cache: 83 | rm -rf $(CURDIR)/cache/* 84 | 85 | # Also avoiding the "clean" target for the reasons described at the "venv" target. 86 | # Deletes the virtualenv. 87 | clean_venv: clean_dirs 88 | rm -rf $(CURDIR)/$(VENV) 89 | rm -rf $(CURDIR)/$(VENV_FLEX) 90 | 91 | travis: venv 92 | $(CURDIR)/$(VENV)/bin/nosetests src 93 | 94 | travis_flex: venv_flex 95 | $(CURDIR)/$(VENV_FLEX)/bin/nosetests flexible_src 96 | -------------------------------------------------------------------------------- /resources/autogenerated: -------------------------------------------------------------------------------- 1 | # This file was auto-generated. Avoid changing it 2 | -------------------------------------------------------------------------------- /resources/dot-git-hooks-pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | make pyflakes 4 | -------------------------------------------------------------------------------- /resources/get_current_sdk_version.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | LAST_KNOWN_GOOD_VERSION="1.9.40" 4 | 5 | if [ -f $(which curl) ] 6 | then 7 | VERSION=$(curl -s https://storage.googleapis.com/appengine-sdks/featured/VERSION | grep release | awk -F '\"' '{print $2}') 8 | else 9 | VERSION=$(wget -q -O - https://storage.googleapis.com/appengine-sdks/featured/VERSION | grep release | awk -F '\"' '{print $2}') 10 | fi 11 | 12 | if [ VERSION="0.0.0" ] 13 | then 14 | VERSION=$LAST_KNOWN_GOOD_VERSION 15 | fi 16 | 17 | echo $VERSION 18 | -------------------------------------------------------------------------------- /resources/requirements.txt: -------------------------------------------------------------------------------- 1 | -r ../requirements.txt 2 | appengine-fixture-loader 3 | flake8 4 | ipdb 5 | nose 6 | pudb 7 | pytest 8 | rednose 9 | webtest 10 | -------------------------------------------------------------------------------- /resources/requirements_flex.txt: -------------------------------------------------------------------------------- 1 | ipdb 2 | nose 3 | pytest 4 | -------------------------------------------------------------------------------- /src/app.yaml: -------------------------------------------------------------------------------- 1 | runtime: python27 2 | api_version: 1 3 | threadsafe: yes 4 | 5 | handlers: 6 | - url: /.* 7 | script: main.application 8 | 9 | libraries: 10 | - name: jinja2 11 | version: latest 12 | 13 | skip_files: 14 | - ^(.*/)?#.*#$ 15 | - ^(.*/)?.*~$ 16 | - ^(.*/)?.*\.py[co]$ 17 | - ^(.*/)?.*/RCS/.*$ 18 | - ^(.*/)?\..*$ 19 | - Makefile 20 | - build 21 | - resources 22 | - cache 23 | - tests 24 | - LICENSE 25 | - README.md 26 | - tests 27 | -------------------------------------------------------------------------------- /src/main.py: -------------------------------------------------------------------------------- 1 | import webapp2 2 | import os 3 | 4 | DEBUG = os.environ.get('SERVER_SOFTWARE', '').startswith('Dev') 5 | 6 | 7 | class SampleIndex(webapp2.RequestHandler): 8 | """Stub request handler""" 9 | 10 | def get(self): 11 | """Handles a get to /""" 12 | self.response.headers['Content-Type'] = 'text/plain' 13 | self.response.out.write("helloworld") 14 | 15 | 16 | application = webapp2.WSGIApplication([ 17 | ('/', SampleIndex), 18 | ], debug=DEBUG) 19 | -------------------------------------------------------------------------------- /src/tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rbanffy/testable_appengine/62d0222416b7efb5aa45512366394d322be960cd/src/tests/__init__.py -------------------------------------------------------------------------------- /src/tests/persons.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "born": "1968-03-03T00:00:00", 4 | "first_name": "John", 5 | "last_name": "Doe", 6 | "favorite_movies": [ 7 | "2001", 8 | "The Day The Earth Stood Still (1951)" 9 | ], 10 | "snores": false, 11 | "sleeptime": "23:00", 12 | "started_school": "1974-02-15", 13 | "thermostat_set_to": 18.34, 14 | "userid": 1 15 | }, 16 | { 17 | "born": "1970-04-27T00:00:00", 18 | "first_name": "Jane", 19 | "last_name": "Doe", 20 | "favorite_movies": [ 21 | "2001", 22 | "Superman" 23 | ], 24 | "snores": false, 25 | "sleeptime": "22:30:30", 26 | "started_school": "1978-08-01", 27 | "thermostat_set_to": 23, 28 | "userid": 2 29 | }, 30 | { 31 | "born": "1999-09-19T00:00:00", 32 | "first_name": "Alice", 33 | "last_name": "Schneier", 34 | "favorite_movies": [ 35 | "2001", 36 | "Superman" 37 | ], 38 | "snores": true, 39 | "sleeptime": "22:00", 40 | "started_school": "1985-08-01", 41 | "thermostat_set_to": 18.34, 42 | "userid": 3 43 | }, 44 | { 45 | "born": "1980-05-25T00:00:00", 46 | "first_name": "Bob", 47 | "last_name": "Schneier", 48 | "favorite_movies": [ 49 | "2001", 50 | "Superman" 51 | ], 52 | "snores": true, 53 | "sleeptime": "22:00", 54 | "started_school": "1985-08-01", 55 | "thermostat_set_to": 18.34, 56 | "userid": -5 57 | } 58 | ] 59 | -------------------------------------------------------------------------------- /src/tests/sanity_test.py: -------------------------------------------------------------------------------- 1 | """ 2 | Ensure the environment is sane 3 | """ 4 | 5 | # Do not add your own tests to this file. This file is intended for the sole 6 | # purpose of confirming your environment was properly set-up 7 | 8 | import datetime 9 | import os 10 | import unittest 11 | import webtest 12 | 13 | # The test will error out if we can't import these items 14 | from google.appengine.api import memcache 15 | from google.appengine.ext import ndb 16 | from google.appengine.ext import testbed 17 | 18 | from appengine_fixture_loader.loader import load_fixture 19 | 20 | # If your project has a src/main.py file, it'll check if it imports 21 | try: 22 | from src import main 23 | TEST_HANDLER = True 24 | except ImportError: 25 | TEST_HANDLER = False 26 | 27 | 28 | class Person(ndb.Model): 29 | """Our sample class""" 30 | first_name = ndb.StringProperty() 31 | last_name = ndb.StringProperty() 32 | born = ndb.DateTimeProperty() 33 | userid = ndb.IntegerProperty() 34 | thermostat_set_to = ndb.FloatProperty() 35 | snores = ndb.BooleanProperty() 36 | started_school = ndb.DateProperty() 37 | sleeptime = ndb.TimeProperty() 38 | favorite_movies = ndb.JsonProperty() 39 | processed = ndb.BooleanProperty(default=False) 40 | 41 | 42 | class SanityTest(unittest.TestCase): 43 | """A simple sanity test. Everything here should past if the environment was 44 | properly set up.""" 45 | 46 | def setUp(self): 47 | self.testbed = testbed.Testbed() 48 | self.testbed.activate() 49 | self.testbed.init_datastore_v3_stub() 50 | self.testbed.init_memcache_stub() 51 | 52 | def tearDown(self): 53 | self.testbed.deactivate() 54 | 55 | def test_sanity(self): 56 | """ 57 | Tests the sanity of the unit testing framework and if we can import all 58 | we need to work 59 | """ 60 | self.assertTrue(True) 61 | 62 | 63 | if TEST_HANDLER: 64 | class HandlerTest(unittest.TestCase): 65 | """This test only triggers if we are testing for the sample 66 | application, not your own.""" 67 | 68 | def setUp(self): 69 | self.testapp = webtest.TestApp(main.application) 70 | 71 | def test_sample_request(self): 72 | """Test a GET / and check a 200 status""" 73 | response = self.testapp.get('/') 74 | self.assertEqual(response.status_int, 200) 75 | 76 | 77 | class MemcacheTest(unittest.TestCase): 78 | """Tests if we can use the memcache from the testbed""" 79 | 80 | def setUp(self): 81 | self.testbed = testbed.Testbed() 82 | self.testbed.activate() 83 | self.testbed.init_datastore_v3_stub() 84 | self.testbed.init_memcache_stub() 85 | self.loaded_data = load_fixture( 86 | os.path.join( 87 | os.path.dirname(__file__), 'persons.json'), Person) 88 | 89 | def tearDown(self): 90 | self.testbed.deactivate() 91 | 92 | def test_memcache(self): 93 | """Tests memcache""" 94 | memcache.set('test_key', 'contents') 95 | self.assertEqual(memcache.get('test_key'), 'contents') 96 | 97 | 98 | class LoaderTest(unittest.TestCase): 99 | """Tests if we can load a JSON file""" 100 | def setUp(self): 101 | self.testbed = testbed.Testbed() 102 | self.testbed.activate() 103 | self.testbed.init_datastore_v3_stub() 104 | self.testbed.init_memcache_stub() 105 | self.loaded_data = load_fixture( 106 | os.path.join( 107 | os.path.dirname(__file__), 'persons.json'), Person) 108 | 109 | def tearDown(self): 110 | self.testbed.deactivate() 111 | 112 | def test_loaded_count(self): 113 | """Make sure we got 4 objects from the JSON file""" 114 | self.assertEqual(len(self.loaded_data), 4) 115 | 116 | def test_loaded_types(self): 117 | """Make sure all objects we loaded are instances of Person""" 118 | self.assertTrue(all([type(p) == Person for p in self.loaded_data])) 119 | 120 | def test_loaded(self): 121 | """Check whether the attributes we imported match the JSON contents""" 122 | # Test if the first record got in 123 | person = Person.query(Person.first_name == 'John').get() 124 | self.assertEqual(person.first_name, 'John') 125 | self.assertEqual(person.last_name, 'Doe') 126 | self.assertEqual(person.born, datetime.datetime(1968, 3, 3)) 127 | self.assertEqual(person.thermostat_set_to, 18.34) 128 | self.assertFalse(person.processed) 129 | 130 | # Test for the third one 131 | person = Person.query(Person.last_name == 'Schneier' and 132 | Person.first_name == 'Alice').get() 133 | self.assertEqual(person.first_name, 'Alice') 134 | self.assertEqual(person.last_name, 'Schneier') 135 | self.assertEqual(person.born, datetime.datetime(1999, 9, 19)) 136 | self.assertTrue(person.snores) 137 | self.assertFalse(person.processed) 138 | 139 | # Test for the last one 140 | person = Person.query( 141 | Person.born == datetime.datetime(1980, 5, 25, 0, 0, 0)).get() 142 | self.assertEqual(person.first_name, 'Bob') 143 | self.assertEqual(person.last_name, 'Schneier') 144 | self.assertEqual(person.born, datetime.datetime(1980, 5, 25)) 145 | self.assertFalse(person.processed) 146 | 147 | 148 | class ProcessedLoaderTest(unittest.TestCase): 149 | """Tests if we can load a JSON file and post-process it""" 150 | def setUp(self): 151 | 152 | def process(p): 153 | p.processed = True 154 | 155 | self.testbed = testbed.Testbed() 156 | self.testbed.activate() 157 | self.testbed.init_datastore_v3_stub() 158 | self.testbed.init_memcache_stub() 159 | self.loaded_data = load_fixture( 160 | os.path.join( 161 | os.path.dirname(__file__), 'persons.json'), 162 | Person, 163 | post_processor=process 164 | ) 165 | 166 | def tearDown(self): 167 | self.testbed.deactivate() 168 | 169 | def test_loaded_count(self): 170 | """Make sure we got 4 objects from the JSON file""" 171 | self.assertEqual(len(self.loaded_data), 4) 172 | 173 | def test_loaded_types(self): 174 | """Make sure all objects we loaded were processed""" 175 | self.assertTrue(all([p.processed for p in self.loaded_data])) 176 | 177 | 178 | if __name__ == '__main__': 179 | unittest.main() 180 | --------------------------------------------------------------------------------