# coding: utf-8 from attest import assert_hook, Tests # pylint: disable=W0611 import itertools from django_attest import TestContext import django_tables2 as tables from .app.models import Person, Occupation models = Tests() models.context(TestContext()) class PersonTable(tables.Table): first_name = tables.Column() last_name = tables.Column() occupation = tables.Column() @models.test def boundrows_iteration(): occupation = Occupation.objects.create(name='Programmer') Person.objects.create(first_name='Bradley', last_name='Ayers', occupation=occupation) Person.objects.create(first_name='Chris', last_name='Doble', occupation=occupation) table = PersonTable(Person.objects.all()) records = [row.record for row in table.rows] expecteds = Person.objects.all() for expected, actual in itertools.izip(expecteds, records):
table.as_html() @general.test def bound_columns_should_support_indexing(): class SimpleTable(tables.Table): a = tables.Column() b = tables.Column() table = SimpleTable([]) Assert('b') == table.columns[1].name Assert('b') == table.columns['b'].name linkcolumn = Tests() linkcolumn.context(TransactionTestContext()) @linkcolumn.test def unicode(): """Test LinkColumn""" # test unicode values + headings class UnicodeTable(tables.Table): first_name = tables.LinkColumn('person', args=[A('pk')]) last_name = tables.LinkColumn('person', args=[A('pk')], verbose_name=u'äÚ¨´ˆÁ˜¨ˆ˜˘Ú…Ò˚ˆπ∆ˆ´') dataset = [ {'pk': 1, 'first_name': u'Brädley', 'last_name': u'∆yers'}, {'pk': 2, 'first_name': u'Chr…s', 'last_name': u'DÒble'}, ] table = UnicodeTable(dataset)
import itertools from django.conf import settings from django.test.client import RequestFactory from django.template import Template, Context import django_tables2 as tables from django_attest import TransactionTestContext from attest import Tests, Assert from .testapp.models import Person, Occupation models = Tests() models.context(TransactionTestContext()) class PersonTable(tables.Table): first_name = tables.Column() last_name = tables.Column() occupation = tables.Column() @models.test def boundrows_iteration(): occupation = Occupation.objects.create(name='Programmer') Person.objects.create(first_name='Bradley', last_name='Ayers', occupation=occupation) Person.objects.create(first_name='Chris', last_name='Doble', occupation=occupation) table = PersonTable(Person.objects.all()) records = [row.record for row in table.rows] expecteds = Person.objects.all() for expected, actual in itertools.izip(expecteds, records): Assert(expected) == actual
# -*- coding: utf-8 -*- from __future__ import absolute_import, unicode_literals import os os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.app.settings' from attest import Tests from django_attest import TestContext, FancyReporter from django_revisionfield.models import Revision from .app.models import Company, Person loader = FancyReporter.test_loader everything = Tests() everything.context(TestContext()) @everything.test def creating_model_instance_should_use_new_revision(): current_revision = Revision.next() person = Person.objects.create(name="Brad") assert person.revision > current_revision @everything.test def saving_model_should_increment_revision(): person = Person.objects.create(name="Brad") revision = person.revision person.name = "Sunny" person.save() assert person.revision > revision
# coding: utf-8 from .app.models import Region from attest import assert_hook, Tests from django_attest import TestContext import django_tables2 as tables from django_tables2.utils import build_request views = Tests() views.context(TestContext()) USING_CBV = hasattr(tables, "SingleTableView") class DispatchHookMixin(object): """ Returns a response *and* reference to the view. """ def dispatch(self, *args, **kwargs): return super(DispatchHookMixin, self).dispatch(*args, **kwargs), self class SimpleTable(tables.Table): class Meta: model = Region @views.test_if(USING_CBV) def view_should_support_pagination_options(): for name in ("Queensland", "New South Wales", "Victoria", "Tasmania"): Region.objects.create(name=name)
# check deletion request = factory.get('/') request.COOKIES.update( ((k, v.value) for k, v in response.cookies.iteritems())) assert 'namespace|name' in request.COOKIES storage = CookieStorage('name', 'namespace') storage.process_request(request) storage.delete() response = HttpResponse('') storage.process_response(response) assert 'namespace|name' not in response.cookies db = Tests() db.context(TestContext()) @db.test def should_complain_if_no_session_and_has_anonymous_user(): storage = DatabaseStorage('name', 'namespace') request = factory.get('/') request.user = AnonymousUser() with Assert.raises(ImproperlyConfigured): storage.process_request(request) @db.test def should_complain_if_no_session_and_has_no_user(): storage = DatabaseStorage('name', 'namespace') request = factory.get('/')
import warnings from attest import Tests, assert_hook, raises from .env import NInt, get_session, init_session, key from sider.types import List from sider.transaction import Transaction from sider.exceptions import CommitError from sider.warnings import PerformanceWarning tests = Tests() tests.context(init_session) @tests.test def iterate(session): view = session.set(key('test_list_iterate'), 'abc', List) assert ['a', 'b', 'c'] == list(view) view = session.set(key('test_listx_iterate'), [1, 2, 3], List(NInt)) assert [1, 2, 3] == list(view) @tests.test def length(session): view = session.set(key('test_list_length'), 'abc', List) assert len(view) == 3 viewx = session.set(key('test_listx_length'), [1, 2, 3], List(NInt)) assert len(viewx) == 3 @tests.test def get(session):
# return classes of an element as a set classes = lambda x: set(x.attrib["class"].split()) assert "sortable" in classes(root.findall('.//thead/tr/th')[0]) assert "sortable" not in classes(root.findall('.//thead/tr/th')[1]) # Now try with an ordered table table = SimpleTable([], order_by="a") root = ET.fromstring(table.as_html()) # return classes of an element as a set assert "sortable" in classes(root.findall('.//thead/tr/th')[0]) assert "asc" in classes(root.findall('.//thead/tr/th')[0]) assert "sortable" not in classes(root.findall('.//thead/tr/th')[1]) linkcolumn = Tests() linkcolumn.context(TestContext()) @linkcolumn.test def unicode(): """Test LinkColumn""" # test unicode values + headings class UnicodeTable(tables.Table): first_name = tables.LinkColumn('person', args=[A('pk')]) last_name = tables.LinkColumn('person', args=[A('pk')], verbose_name=u'äÚ¨´ˆÁ˜¨ˆ˜˘Ú…Ò˚ˆπ∆ˆ´') dataset = [ {
# coding: utf-8 from .app.models import Region from attest import assert_hook, Tests from django_attest import TestContext import django_tables2 as tables from django_tables2.utils import build_request views = Tests() views.context(TestContext()) USING_CBV = hasattr(tables, "SingleTableView") class DispatchHookMixin(object): """ Returns a response *and* reference to the view. """ def dispatch(self, *args, **kwargs): return super(DispatchHookMixin, self).dispatch(*args, **kwargs), self class SimpleTable(tables.Table): class Meta: model = Region @views.test_if(USING_CBV) def view_should_support_pagination_options(): for name in ("Queensland", "New South Wales", "Victoria", "Tasmania"): Region.objects.create(name=name) class SimpleView(DispatchHookMixin, tables.SingleTableView):
try: yield finally: stack.append(item.upper()) def reset(): stack[:] = [] try: yield finally: stack[:] = [] suite = Tests() suite.context(reset) @suite.test def decorator_no_args(): @pusher def foo(): pass assert stack == [] foo() assert stack == ["a", "A"] @suite.test def decorator_with_args():
table.as_html() @general.test def bound_columns_should_support_indexing(): class SimpleTable(tables.Table): a = tables.Column() b = tables.Column() table = SimpleTable([]) Assert('b') == table.columns[1].name Assert('b') == table.columns['b'].name linkcolumn = Tests() linkcolumn.context(TransactionTestContext()) @linkcolumn.test def unicode(): """Test LinkColumn""" # test unicode values + headings class UnicodeTable(tables.Table): first_name = tables.LinkColumn('person', args=[A('pk')]) last_name = tables.LinkColumn('person', args=[A('pk')], verbose_name=u'äÚ¨´ˆÁ˜¨ˆ˜˘Ú…Ò˚ˆπ∆ˆ´') dataset = [ {
from django_attest import TestContext from formwizard.views import WizardView class TestWizard(WizardView): # pylint: ignore=W0223 storage = "formwizard.storage.CookieStorage" steps = ( ("Page 1", Page1), ) template_name = "simple.html" wizard_step_templates = { "Page 1": "custom_step.html" } urlpatterns = patterns('', ('^test/', TestWizard.as_view()), ) tests = Tests() tests.context(TestContext(urls='tests.templates')) @tests.test def wizard_step_templates_should_be_honored(client): response = client.get('/test/') assert "custom_step.html" in [t.name for t in response.templates] assert response.content == "This is an empty custom step.\n\n"
# check deletion request = factory.get('/') request.COOKIES.update(((k, v.value) for k, v in response.cookies.iteritems())) assert 'namespace|name' in request.COOKIES storage = CookieStorage('name', 'namespace') storage.process_request(request) storage.delete() response = HttpResponse('') storage.process_response(response) assert 'namespace|name' not in response.cookies db = Tests() db.context(TestContext()) @db.test def should_complain_if_no_session_and_has_anonymous_user(): storage = DatabaseStorage('name', 'namespace') request = factory.get('/') request.user = AnonymousUser() with Assert.raises(ImproperlyConfigured): storage.process_request(request) @db.test def should_complain_if_no_session_and_has_no_user(): storage = DatabaseStorage('name', 'namespace') request = factory.get('/')
# -*- coding: utf-8 -*- import itertools from django.conf import settings from django.template import Template, Context import django_tables2 as tables from django_attest import TransactionTestContext from attest import Tests, Assert from .app.models import Person, Occupation models = Tests() models.context(TransactionTestContext()) class PersonTable(tables.Table): first_name = tables.Column() last_name = tables.Column() occupation = tables.Column() @models.test def boundrows_iteration(): occupation = Occupation.objects.create(name='Programmer') Person.objects.create(first_name='Bradley', last_name='Ayers', occupation=occupation) Person.objects.create(first_name='Chris', last_name='Doble', occupation=occupation) table = PersonTable(Person.objects.all()) records = [row.record for row in table.rows]
assert "sortable" in classes(root.findall(".//thead/tr/th")[0]) assert "asc" in classes(root.findall(".//thead/tr/th")[0]) assert "sortable" not in classes(root.findall(".//thead/tr/th")[1]) @general.test def empty_values_triggers_default(): class Table(tables.Table): a = tables.Column(empty_values=(1, 2), default="--") table = Table([{"a": 1}, {"a": 2}, {"a": 3}, {"a": 4}]) assert [x["a"] for x in table.rows] == ["--", "--", 3, 4] linkcolumn = Tests() linkcolumn.context(TestContext()) @linkcolumn.test def unicode(): """Test LinkColumn""" # test unicode values + headings class UnicodeTable(tables.Table): first_name = tables.LinkColumn("person", args=[A("pk")]) last_name = tables.LinkColumn("person", args=[A("pk")], verbose_name=u"äÚ¨´ˆÁ˜¨ˆ˜˘Ú…Ò˚ˆπ∆ˆ´") dataset = [ {"pk": 1, "first_name": u"Brädley", "last_name": u"∆yers"}, {"pk": 2, "first_name": u"Chr…s", "last_name": u"DÒble"}, ]
# coding: utf-8 from __future__ import unicode_literals from .app.models import Person from attest import assert_hook, Tests from contextlib import contextmanager from django.db import connections, DEFAULT_DB_ALIAS, signals, transaction from django_attest import TransactionTestContext suite = Tests() suite.context(TransactionTestContext(multi_db=True)) conn = lambda alias: connections[DEFAULT_DB_ALIAS if alias is None else alias] # -- helpers------------------------------------------------------------------- @contextmanager def connect(signal, handler): """ Connect and disconnect a signal receiver using a context manager:: def handler(sender, **kwargs): ... with connect(pre_commit, handler): ... """ signal.connect(handler) try:
# coding: utf-8 from attest import assert_hook, Tests import itertools from django_attest import TestContext import django_tables2 as tables from .app.models import Person, Occupation models = Tests() models.context(TestContext()) class PersonTable(tables.Table): first_name = tables.Column() last_name = tables.Column() occupation = tables.Column() @models.test def boundrows_iteration(): occupation = Occupation.objects.create(name='Programmer') Person.objects.create(first_name='Bradley', last_name='Ayers', occupation=occupation) Person.objects.create(first_name='Chris', last_name='Doble', occupation=occupation) table = PersonTable(Person.objects.all()) records = [row.record for row in table.rows] expecteds = Person.objects.all() for expected, actual in itertools.izip(expecteds, records): assert expected == actual