Kirill Simonov avatar Kirill Simonov committed 9a0007f

Added a sample django application for testing django addon.

Comments (0)

Files changed (10)

test/code/test_django_sandbox/__init__.py

+
+from .settings import DATABASES
+
+engine = {
+        'pgsql': 'postgresql_psycopg2',
+        'sqlite': 'sqlite3',
+}.get(sandbox.engine, sandbox.engine)
+DATABASES['default']['ENGINE'] += engine
+DATABASES['default']['NAME'] = sandbox.database
+if sandbox.username is not None:
+    DATABASES['default']['USER'] = sandbox.username
+if sandbox.password is not None:
+    DATABASES['default']['PASSWORD'] = sandbox.password
+if sandbox.host is not None:
+    DATABASES['default']['HOST'] = sandbox.host
+if sandbox.port is not None:
+    DATABASES['default']['PORT'] = str(sandbox.port)
+
+def createdb():
+    if sandbox.engine == 'mysql':
+        DATABASES['default']['OPTIONS'] = {
+                'init_command': 'SET storage_engine=INNODB',
+        }
+    from datetime import datetime
+    from django.core.management import call_command
+    from .polls.models import Poll, Choice
+    call_command('syncdb', verbosity=0)
+    p = Poll(question="What's up?", pub_date=datetime(2011, 01, 01))
+    p.save()
+    p.choice_set.create(choice='Not much', votes=10)
+    p.choice_set.create(choice='The sky', votes=20)
+    p.choice_set.create(choice='Just hacking again', votes=30)
+
+def dropdb():
+    from django.db import models, connections, transaction, DEFAULT_DB_ALIAS
+    from django.core.management import sql, color
+    app = models.get_app('polls')
+    connection = connections[DEFAULT_DB_ALIAS]
+    sql_list = sql.sql_delete(app, color.no_style(), connection)
+    cursor = connection.cursor()
+    for sql in sql_list:
+        cursor.execute(sql)
+    transaction.commit_unless_managed(using=DEFAULT_DB_ALIAS)
+
Add a comment to this file

test/code/test_django_sandbox/polls/__init__.py

Empty file added.

test/code/test_django_sandbox/polls/models.py

+
+from django.db import models
+
+class Poll(models.Model):
+    question = models.CharField(max_length=200)
+    pub_date = models.DateTimeField('date published')
+
+class Choice(models.Model):
+    poll = models.ForeignKey(Poll)
+    choice = models.CharField(max_length=200)
+    votes = models.IntegerField()
+

test/code/test_django_sandbox/settings.py

+
+DEBUG = True
+
+TEMPLATE_DEBUG = DEBUG
+
+ADMINS = ()
+
+MANAGERS = ADMINS
+
+DATABASES = {
+    'default': {
+        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
+        'NAME': '',                      # Or path to database file if using sqlite3.
+        'USER': '',                      # Not used with sqlite3.
+        'PASSWORD': '',                  # Not used with sqlite3.
+        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
+        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
+    }
+}
+
+TIME_ZONE = 'America/Chicago'
+
+LANGUAGE_CODE = 'en-us'
+
+SITE_ID = 1
+
+USE_I18N = True
+
+USE_L10N = True
+
+MEDIA_ROOT = ''
+
+MEDIA_URL = ''
+
+STATIC_ROOT = ''
+
+STATIC_URL = '/static/'
+
+ADMIN_MEDIA_PREFIX = '/static/admin/'
+
+STATICFILES_DIRS = ()
+
+STATICFILES_FINDERS = ()
+
+SECRET_KEY = '^'
+
+TEMPLATE_LOADERS = ()
+
+MIDDLEWARE_CLASSES = ()
+
+ROOT_URLCONF = 'test_django_sandbox.urls'
+
+TEMPLATE_DIRS = ()
+
+INSTALLED_APPS = (
+    'test_django_sandbox.polls',
+)
+

test/code/test_django_sandbox/urls.py

+
+from django.conf.urls.defaults import patterns
+
+urlpatterns = patterns('')
+

test/input/addon.yaml

       tweak.cors: { origin: null }
   - uri: /school
 
+# TWEAK.DJANGO - adapt to Django
+- py: has-django
+  ifdef: [sqlite, pgsql, mysql, oracle]
+  code: |
+    try:
+        import django
+        if django.VERSION > (1, 3):
+            state.toggles.add('django')
+    except ImportError:
+        pass
+- title: tweak.django
+  ifdef: django
+  tests:
+  # Addon description
+#  - ctl: [ext, tweak.django]
+
+  # Load `test_django_sandbox` and deploy the database
+  - py: add-module-path
+    code: |
+      import __builtin__, sys, os, os.path
+      path = os.path.join(os.getcwd(), "test/code")
+      sys.path.insert(0, path)
+      __builtin__.sandbox = state.saves['sandbox'][0]
+      os.environ['DJANGO_SETTINGS_MODULE'] = 'test_django_sandbox.settings'
+      from test_django_sandbox import createdb
+      createdb()
+
+  # Test Django-generated database
+  - load: sandbox
+  - uri: /polls_poll{question, pub_date, count(polls_choice)}
+  - uri: /polls_choice{poll.question, choice, votes}
+
+  # Restore the original `sys.path`
+  - py: remove-module-path
+    code: |
+      from test_django_sandbox import dropdb
+      dropdb()
+      import __builtin__, sys, os, os.path
+      path = os.path.join(os.getcwd(), "test/code")
+      sys.path.remove(path)
+      del os.environ['DJANGO_SETTINGS_MODULE']
+      for name in sorted(sys.modules):
+          if name.startswith(('test_django_sandbox', 'django')):
+              del sys.modules[name]
+      del __builtin__.sandbox
+
 # TWEAK.INET - IPv4 data type
 - title: tweak.inet
   ifdef: pgsql

test/output/mysql.yaml

                     `school`.`campus`
              FROM `school`
              ORDER BY 1 ASC
+      - py: has-django
+        stdout: ''
+      - id: tweak.django
+        tests:
+        - py: add-module-path
+          stdout: ''
+        - uri: /polls_poll{question, pub_date, count(polls_choice)}
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |2
+             | polls_poll                                    |
+             +-----------------------------------------------+
+             | question   | pub_date   | count(polls_choice) |
+            -+------------+------------+---------------------+-
+             | What's up? | 2011-01-01 |                   3 |
+                                                       (1 row)
+
+             ----
+             /polls_poll{question,pub_date,count(polls_choice)}
+             SELECT `polls_poll`.`question`,
+                    `polls_poll`.`pub_date`,
+                    COALESCE(`polls_choice`.`count`, 0)
+             FROM `polls_poll`
+                  LEFT OUTER JOIN (SELECT COUNT(TRUE) AS `count`,
+                                          `polls_choice`.`poll_id`
+                                   FROM `polls_choice`
+                                   GROUP BY 2) AS `polls_choice`
+                                  ON (`polls_poll`.`id` = `polls_choice`.`poll_id`)
+             ORDER BY `polls_poll`.`id` ASC
+        - uri: /polls_choice{poll.question, choice, votes}
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |2
+             | polls_choice                            |
+             +-----------------------------------------+
+             | question   | choice             | votes |
+            -+------------+--------------------+-------+-
+             | What's up? | Not much           |    10 |
+             | What's up? | The sky            |    20 |
+             | What's up? | Just hacking again |    30 |
+                                                (3 rows)
+
+             ----
+             /polls_choice{poll.question,choice,votes}
+             SELECT `polls_poll`.`question`,
+                    `polls_choice`.`choice`,
+                    `polls_choice`.`votes`
+             FROM `polls_choice`
+                  INNER JOIN `polls_poll`
+                             ON (`polls_choice`.`poll_id` = `polls_poll`.`id`)
+             ORDER BY `polls_choice`.`id` ASC
+        - py: remove-module-path
+          stdout: ''
       - id: tweak.meta
         tests:
         - ctl: [ext, tweak.meta]

test/output/oracle.yaml

                     "SCHOOL"."CAMPUS"
              FROM "SCHOOL"
              ORDER BY 1 ASC
+      - py: has-django
+        stdout: ''
+      - id: tweak.django
+        tests:
+        - py: add-module-path
+          stdout: ''
+        - uri: /polls_poll{question, pub_date, count(polls_choice)}
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |2
+             | polls_poll                                             |
+             +--------------------------------------------------------+
+             | question   | pub_date            | count(polls_choice) |
+            -+------------+---------------------+---------------------+-
+             | What's up? | 2011-01-01 00:00:00 |                   3 |
+                                                                (1 row)
+
+             ----
+             /polls_poll{question,pub_date,count(polls_choice)}
+             SELECT "POLLS_POLL"."QUESTION",
+                    "POLLS_POLL"."PUB_DATE",
+                    COALESCE("POLLS_CHOICE"."count", 0)
+             FROM "POLLS_POLL"
+                  LEFT OUTER JOIN (SELECT COUNT(1) AS "count",
+                                          "POLLS_CHOICE"."POLL_ID"
+                                   FROM "POLLS_CHOICE"
+                                   GROUP BY "POLLS_CHOICE"."POLL_ID") "POLLS_CHOICE"
+                                  ON ("POLLS_POLL"."ID" = "POLLS_CHOICE"."POLL_ID")
+             ORDER BY "POLLS_POLL"."ID" ASC
+        - uri: /polls_choice{poll.question, choice, votes}
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |2
+             | polls_choice                            |
+             +-----------------------------------------+
+             | question   | choice             | votes |
+            -+------------+--------------------+-------+-
+             | What's up? | Not much           |    10 |
+             | What's up? | The sky            |    20 |
+             | What's up? | Just hacking again |    30 |
+                                                (3 rows)
+
+             ----
+             /polls_choice{poll.question,choice,votes}
+             SELECT "POLLS_POLL"."QUESTION",
+                    "POLLS_CHOICE"."CHOICE",
+                    "POLLS_CHOICE"."VOTES"
+             FROM "POLLS_CHOICE"
+                  INNER JOIN "POLLS_POLL"
+                             ON ("POLLS_CHOICE"."POLL_ID" = "POLLS_POLL"."ID")
+             ORDER BY "POLLS_CHOICE"."ID" ASC
+        - py: remove-module-path
+          stdout: ''
       - id: tweak.meta
         tests:
         - ctl: [ext, tweak.meta]

test/output/pgsql.yaml

                     "school"."campus"
              FROM "ad"."school"
              ORDER BY 1 ASC
+      - py: has-django
+        stdout: ''
+      - id: tweak.django
+        tests:
+        - py: add-module-path
+          stdout: ''
+        - uri: /polls_poll{question, pub_date, count(polls_choice)}
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |2
+             | polls_poll                                                   |
+             +--------------------------------------------------------------+
+             | question   | pub_date                  | count(polls_choice) |
+            -+------------+---------------------------+---------------------+-
+             | What's up? | 2011-01-01 01:00:00-05:00 |                   3 |
+                                                                      (1 row)
+
+             ----
+             /polls_poll{question,pub_date,count(polls_choice)}
+             SELECT "polls_poll"."question",
+                    "polls_poll"."pub_date",
+                    COALESCE("polls_choice"."count", 0)
+             FROM "polls_poll"
+                  LEFT OUTER JOIN (SELECT COUNT(TRUE) AS "count",
+                                          "polls_choice"."poll_id"
+                                   FROM "polls_choice"
+                                   GROUP BY 2) AS "polls_choice"
+                                  ON ("polls_poll"."id" = "polls_choice"."poll_id")
+             ORDER BY "polls_poll"."id" ASC
+        - uri: /polls_choice{poll.question, choice, votes}
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |2
+             | polls_choice                            |
+             +-----------------------------------------+
+             | question   | choice             | votes |
+            -+------------+--------------------+-------+-
+             | What's up? | Not much           |    10 |
+             | What's up? | The sky            |    20 |
+             | What's up? | Just hacking again |    30 |
+                                                (3 rows)
+
+             ----
+             /polls_choice{poll.question,choice,votes}
+             SELECT "polls_poll"."question",
+                    "polls_choice"."choice",
+                    "polls_choice"."votes"
+             FROM "polls_choice"
+                  INNER JOIN "polls_poll"
+                             ON ("polls_choice"."poll_id" = "polls_poll"."id")
+             ORDER BY "polls_choice"."id" ASC
+        - py: remove-module-path
+          stdout: ''
       - id: tweak.inet
         tests:
         - ctl: [ext, tweak.inet]

test/output/sqlite.yaml

                     "school"."campus"
              FROM "school"
              ORDER BY 1 ASC
+      - py: has-django
+        stdout: ''
+      - id: tweak.django
+        tests:
+        - py: add-module-path
+          stdout: ''
+        - uri: /polls_poll{question, pub_date, count(polls_choice)}
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |2
+             | polls_poll                                    |
+             +-----------------------------------------------+
+             | question   | pub_date   | count(polls_choice) |
+            -+------------+------------+---------------------+-
+             | What's up? | 2011-01-01 |                   3 |
+                                                       (1 row)
+
+             ----
+             /polls_poll{question,pub_date,count(polls_choice)}
+             SELECT "polls_poll"."question",
+                    "polls_poll"."pub_date",
+                    COALESCE("polls_choice"."count", 0)
+             FROM "polls_poll"
+                  LEFT OUTER JOIN (SELECT COUNT(1) AS "count",
+                                          "polls_choice"."poll_id"
+                                   FROM "polls_choice"
+                                   GROUP BY 2) AS "polls_choice"
+                                  ON ("polls_poll"."id" = "polls_choice"."poll_id")
+             ORDER BY "polls_poll"."id" ASC
+        - uri: /polls_choice{poll.question, choice, votes}
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |2
+             | polls_choice                            |
+             +-----------------------------------------+
+             | question   | choice             | votes |
+            -+------------+--------------------+-------+-
+             | What's up? | Not much           |    10 |
+             | What's up? | The sky            |    20 |
+             | What's up? | Just hacking again |    30 |
+                                                (3 rows)
+
+             ----
+             /polls_choice{poll.question,choice,votes}
+             SELECT "polls_poll"."question",
+                    "polls_choice"."choice",
+                    "polls_choice"."votes"
+             FROM "polls_choice"
+                  INNER JOIN "polls_poll"
+                             ON ("polls_choice"."poll_id" = "polls_poll"."id")
+             ORDER BY "polls_choice"."id" ASC
+        - py: remove-module-path
+          stdout: ''
       - id: tweak.meta
         tests:
         - ctl: [ext, tweak.meta]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.