Browse Source

Usando pytest (setup e testes antigos convertidos)

producao
Marcio Mazza 10 years ago
parent
commit
64cbe9f0a9
  1. 2
      pytest.ini
  2. 8
      setup.py
  3. 155
      sigi/apps/diagnosticos/templatetags/smart_if.py
  4. 166
      sigi/apps/diagnosticos/templatetags/test_smart_if.py
  5. 2
      sigi/settings/base.py
  6. 16
      sigi/settings/test.py

2
pytest.ini

@ -0,0 +1,2 @@
[pytest]
DJANGO_SETTINGS_MODULE=sigi.settings.test

8
setup.py

@ -0,0 +1,8 @@
# py.test precisa deste arquivo para que o projeto seja instalado no virtualenv com:
# "pip install -e ."
# vide http://pytest-django.readthedocs.org/en/latest/faq.html#i-see-an-error-saying-could-not-import-myproject-settings
#
from distutils.core import setup
setup(name='sigi', version='2.x')

155
sigi/apps/diagnosticos/templatetags/smart_if.py

@ -7,7 +7,6 @@ greater than and less than operators. Some common case examples::
{% if articles|length >= 5 %}...{% endif %} {% if articles|length >= 5 %}...{% endif %}
{% if "ifnotequal tag" != "beautiful" %}...{% endif %} {% if "ifnotequal tag" != "beautiful" %}...{% endif %}
""" """
import unittest
from django import template from django import template
@ -79,154 +78,6 @@ class In(BaseCalc):
return var1 in var2 return var1 in var2
#==============================================================================
# Tests
#==============================================================================
class TestVar(object):
"""
A basic self-resolvable object similar to a Django template variable. Used
to assist with tests.
"""
def __init__(self, value):
self.value = value
def resolve(self, context):
return self.value
class SmartIfTests(unittest.TestCase):
def setUp(self):
self.true = TestVar(True)
self.false = TestVar(False)
self.high = TestVar(9000)
self.low = TestVar(1)
def assertCalc(self, calc, context=None):
"""
Test a calculation is True, also checking the inverse "negate" case.
"""
context = context or {}
self.assert_(calc.resolve(context))
calc.negate = not calc.negate
self.assertFalse(calc.resolve(context))
def assertCalcFalse(self, calc, context=None):
"""
Test a calculation is False, also checking the inverse "negate" case.
"""
context = context or {}
self.assertFalse(calc.resolve(context))
calc.negate = not calc.negate
self.assert_(calc.resolve(context))
def test_or(self):
self.assertCalc(Or(self.true))
self.assertCalcFalse(Or(self.false))
self.assertCalc(Or(self.true, self.true))
self.assertCalc(Or(self.true, self.false))
self.assertCalc(Or(self.false, self.true))
self.assertCalcFalse(Or(self.false, self.false))
def test_and(self):
self.assertCalc(And(self.true, self.true))
self.assertCalcFalse(And(self.true, self.false))
self.assertCalcFalse(And(self.false, self.true))
self.assertCalcFalse(And(self.false, self.false))
def test_equals(self):
self.assertCalc(Equals(self.low, self.low))
self.assertCalcFalse(Equals(self.low, self.high))
def test_greater(self):
self.assertCalc(Greater(self.high, self.low))
self.assertCalcFalse(Greater(self.low, self.low))
self.assertCalcFalse(Greater(self.low, self.high))
def test_greater_or_equal(self):
self.assertCalc(GreaterOrEqual(self.high, self.low))
self.assertCalc(GreaterOrEqual(self.low, self.low))
self.assertCalcFalse(GreaterOrEqual(self.low, self.high))
def test_in(self):
list_ = TestVar([1, 2, 3])
invalid_list = TestVar(None)
self.assertCalc(In(self.low, list_))
self.assertCalcFalse(In(self.low, invalid_list))
def test_parse_bits(self):
var = IfParser([True]).parse()
self.assert_(var.resolve({}))
var = IfParser([False]).parse()
self.assertFalse(var.resolve({}))
var = IfParser([False, 'or', True]).parse()
self.assert_(var.resolve({}))
var = IfParser([False, 'and', True]).parse()
self.assertFalse(var.resolve({}))
var = IfParser(['not', False, 'and', 'not', False]).parse()
self.assert_(var.resolve({}))
var = IfParser(['not', 'not', True]).parse()
self.assert_(var.resolve({}))
var = IfParser([1, '=', 1]).parse()
self.assert_(var.resolve({}))
var = IfParser([1, 'not', '=', 1]).parse()
self.assertFalse(var.resolve({}))
var = IfParser([1, 'not', 'not', '=', 1]).parse()
self.assert_(var.resolve({}))
var = IfParser([1, '!=', 1]).parse()
self.assertFalse(var.resolve({}))
var = IfParser([3, '>', 2]).parse()
self.assert_(var.resolve({}))
var = IfParser([1, '<', 2]).parse()
self.assert_(var.resolve({}))
var = IfParser([2, 'not', 'in', [2, 3]]).parse()
self.assertFalse(var.resolve({}))
var = IfParser([1, 'or', 1, '=', 2]).parse()
self.assert_(var.resolve({}))
def test_boolean(self):
var = IfParser([True, 'and', True, 'and', True]).parse()
self.assert_(var.resolve({}))
var = IfParser([False, 'or', False, 'or', True]).parse()
self.assert_(var.resolve({}))
var = IfParser([True, 'and', False, 'or', True]).parse()
self.assert_(var.resolve({}))
var = IfParser([False, 'or', True, 'and', True]).parse()
self.assert_(var.resolve({}))
var = IfParser([True, 'and', True, 'and', False]).parse()
self.assertFalse(var.resolve({}))
var = IfParser([False, 'or', False, 'or', False]).parse()
self.assertFalse(var.resolve({}))
var = IfParser([False, 'or', True, 'and', False]).parse()
self.assertFalse(var.resolve({}))
var = IfParser([False, 'and', True, 'or', False]).parse()
self.assertFalse(var.resolve({}))
def test_invalid(self):
self.assertRaises(ValueError, IfParser(['not']).parse)
self.assertRaises(ValueError, IfParser(['==']).parse)
self.assertRaises(ValueError, IfParser([1, 'in']).parse)
self.assertRaises(ValueError, IfParser([1, '>', 'in']).parse)
self.assertRaises(ValueError, IfParser([1, '==', 'not', 'not']).parse)
self.assertRaises(ValueError, IfParser([1, 2]).parse)
OPERATORS = { OPERATORS = {
'=': (Equals, True), '=': (Equals, True),
'==': (Equals, True), '==': (Equals, True),
@ -288,7 +139,7 @@ class IfParser(object):
return self.pos >= self.len return self.pos >= self.len
def create_var(self, value): def create_var(self, value):
return TestVar(value) raise NotImplementedError()
def get_bool_var(self): def get_bool_var(self):
""" """
@ -406,7 +257,3 @@ def smart_if(parser, token):
else: else:
nodelist_false = None nodelist_false = None
return SmartIfNode(var, nodelist_true, nodelist_false) return SmartIfNode(var, nodelist_true, nodelist_false)
if __name__ == '__main__':
unittest.main()

166
sigi/apps/diagnosticos/templatetags/test_smart_if.py

@ -0,0 +1,166 @@
# -*- coding: utf-8 -*-
import pytest
from smart_if import Or, And, Equals, Greater, GreaterOrEqual, In, IfParser
class TestVar(object):
"""
A basic self-resolvable object similar to a Django template variable. Used
to assist with tests.
"""
def __init__(self, value):
self.value = value
def resolve(self, context):
return self.value
class TestIfParser(IfParser):
def create_var(self, value):
return TestVar(value)
class ValueHolder(object):
pass
VALORES = ValueHolder()
VALORES.true = TestVar(True)
VALORES.false = TestVar(False)
VALORES.high = TestVar(9000)
VALORES.low = TestVar(1)
def assertCalc(calc, context=None):
"""
Test a calculation is True, also checking the inverse "negate" case.
"""
context = context or {}
assert calc.resolve(context)
calc.negate = not calc.negate
assert not calc.resolve(context)
def assertCalcFalse(calc, context=None):
"""
Test a calculation is False, also checking the inverse "negate" case.
"""
context = context or {}
assert not calc.resolve(context)
calc.negate = not calc.negate
assert calc.resolve(context)
def test_or():
assertCalc(Or(VALORES.true))
assertCalcFalse(Or(VALORES.false))
assertCalc(Or(VALORES.true, VALORES.true))
assertCalc(Or(VALORES.true, VALORES.false))
assertCalc(Or(VALORES.false, VALORES.true))
assertCalcFalse(Or(VALORES.false, VALORES.false))
def test_and():
assertCalc(And(VALORES.true, VALORES.true))
assertCalcFalse(And(VALORES.true, VALORES.false))
assertCalcFalse(And(VALORES.false, VALORES.true))
assertCalcFalse(And(VALORES.false, VALORES.false))
def test_equals():
assertCalc(Equals(VALORES.low, VALORES.low))
assertCalcFalse(Equals(VALORES.low, VALORES.high))
def test_greater():
assertCalc(Greater(VALORES.high, VALORES.low))
assertCalcFalse(Greater(VALORES.low, VALORES.low))
assertCalcFalse(Greater(VALORES.low, VALORES.high))
def test_greater_or_equal():
assertCalc(GreaterOrEqual(VALORES.high, VALORES.low))
assertCalc(GreaterOrEqual(VALORES.low, VALORES.low))
assertCalcFalse(GreaterOrEqual(VALORES.low, VALORES.high))
def test_in():
list_ = TestVar([1, 2, 3])
invalid_list = TestVar(None)
assertCalc(In(VALORES.low, list_))
assertCalcFalse(In(VALORES.low, invalid_list))
def test_parse_bits():
var = TestIfParser([True]).parse()
assert var.resolve({})
var = TestIfParser([False]).parse()
assert not var.resolve({})
var = TestIfParser([False, 'or', True]).parse()
assert var.resolve({})
var = TestIfParser([False, 'and', True]).parse()
assert not var.resolve({})
var = TestIfParser(['not', False, 'and', 'not', False]).parse()
assert var.resolve({})
var = TestIfParser(['not', 'not', True]).parse()
assert var.resolve({})
var = TestIfParser([1, '=', 1]).parse()
assert var.resolve({})
var = TestIfParser([1, 'not', '=', 1]).parse()
assert not var.resolve({})
var = TestIfParser([1, 'not', 'not', '=', 1]).parse()
assert var.resolve({})
var = TestIfParser([1, '!=', 1]).parse()
assert not var.resolve({})
var = TestIfParser([3, '>', 2]).parse()
assert var.resolve({})
var = TestIfParser([1, '<', 2]).parse()
assert var.resolve({})
var = TestIfParser([2, 'not', 'in', [2, 3]]).parse()
assert not var.resolve({})
var = TestIfParser([1, 'or', 1, '=', 2]).parse()
assert var.resolve({})
def test_boolean():
var = TestIfParser([True, 'and', True, 'and', True]).parse()
assert var.resolve({})
var = TestIfParser([False, 'or', False, 'or', True]).parse()
assert var.resolve({})
var = TestIfParser([True, 'and', False, 'or', True]).parse()
assert var.resolve({})
var = TestIfParser([False, 'or', True, 'and', True]).parse()
assert var.resolve({})
var = TestIfParser([True, 'and', True, 'and', False]).parse()
assert not var.resolve({})
var = TestIfParser([False, 'or', False, 'or', False]).parse()
assert not var.resolve({})
var = TestIfParser([False, 'or', True, 'and', False]).parse()
assert not var.resolve({})
var = TestIfParser([False, 'and', True, 'or', False]).parse()
assert not var.resolve({})
def test_invalid():
pytest.raises(ValueError, TestIfParser(['not']).parse)
pytest.raises(ValueError, TestIfParser(['==']).parse)
pytest.raises(ValueError, TestIfParser([1, 'in']).parse)
pytest.raises(ValueError, TestIfParser([1, '>', 'in']).parse)
pytest.raises(ValueError, TestIfParser([1, '==', 'not', 'not']).parse)
pytest.raises(ValueError, TestIfParser([1, 2]).parse)

2
sigi/settings/base.py

@ -111,3 +111,5 @@ MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/' MEDIA_URL = '/media/'
SESSION_EXPIRE_AT_BROWSER_CLOSE = True SESSION_EXPIRE_AT_BROWSER_CLOSE = True
TEST_RUNNER = None

16
sigi/settings/test.py

@ -0,0 +1,16 @@
from base import *
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '0$ip1fb5xtq%a=)-k_4r^(#jn0t^@+*^kihkxkozg-mip7+w3+'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'sigi_test',
'USER': 'sigi',
'PASSWORD': 'sigi',
'HOST': 'localhost',
'PORT': '5432',
}
}
Loading…
Cancel
Save