Meetup tech salon

AGENda


  • Test in Django

  • Case discuss

testing with django

  • Django supply
    - TestCase (unittest) and doctests
    - Test Client

  • Type of test
    - Unit tests
    - Integration tests

  • Test in Django
    - Testing views, add fixtures
    - Testing POST requests, forms, models, etc.

What & when

  • NOT built-in Python

  • NOT built into Django

  • model custom methods

  • business logic
    custom views, forms, template tags, context processors, middleware, management commands

  • test-first (a.k.a. Test Driven Development) or test-after.
    bug report -> create test, test failure -> fix it -> test pass

tests.py

import datetime
from django.test import TestCase
from polls.models import Poll, Choice

class PollsViewsTestCase(TestCase):
    fixtures = ['polls_views_testdata.json']
    def test_index(self):
        resp = self.client.get('/polls/')
        self.assertEqual(resp.status_code, 200)
        self.assertTrue('latest_poll_list' in resp.context)
        self.assertEqual([poll.pk for poll in
                          resp.context['latest_poll_list']], [1])  

Fabfile.py

URL Test in Fabric

def url_test():
        urls = ('http://host:8000/res/pic/all',
                'http://host:8000/res/pic/87',)
        for u in urls:
            try:
                  resp = urlopen(u)
            except Exception, e:
                  print red_bg("open %s error" % u)
                  abort("test fail, %s" % e)
           if resp.code == 200 and bool(loads(resp.read())):
            print green_bg('test %s pass' % u)
        else:
            abort('test ERROR')  


case discuss

  • emoji / unicode with MySql

    CREATE DATABASE xxx DEFAULT CHARACTER SET utf8mb4
    MySQL 5.0 5.1 只支持3字节的utf8,5.5支持的 utf8mb4

  • +--------+--------------------------------------------------------------------------------------+
    | Database | Create Database |
    +--------+--------------------------------------------------------------------------------------+
    | tablename | CREATE DATABASE `tablename` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin */ |
    +--------+--------------------------------------------------------------------------------------+ 

change django

settings.pyDATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {'charset': 'utf8mb4'},
        'NAME': 'dbname',
        'USER': 'dev',
        'PASSWORD': 'dev',
        'HOST': '',
        'PORT': '',
        },
    }

_mysql_exceptions.OperationalError: (2019, "Can't initialize character set utf8mb4 (path: /usr/share/mysql/charsets/)

views.pyc=MySQLdb.connect('127.0.0.1', user='root', passwd='passwd', charset='utf8', use_unicode=True).cursor()
c.execute("set names utf8mb4")

unicode error

Error 1:
IntegrityError: (1062, "Duplicate entry 'lol' for key 'name'") 
Models.objecsts.get_or_create() ? MySql!
Error 2:
logger.info("Deleted local files of %s" % pic) 
DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0xa1 in position 8: unexpected code byte. You passed in  () 

media dir permission

  • upload fail because uwsgi change
  • deploy layout
    - project data & static data
    - config of nginx & uwsgi. ln or not ln?
    - log files. rotate?
    - permission: develop & www-data

discovery

Vagrant box + virtualbox

Vagrant provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximize the productivity and flexibility of you and your team.

UP AND RUNNING

$ vagrant init precise32 http://files.vagrantup.com/precise32.box
$ vagrant up$ vagrant ssh

resources


Links:
testing in django 1, 2



Copy of Meetup

By Liu Bo

Copy of Meetup

  • 1,504