<commit_before>#-*- coding:utf-8 -*-

from __future__ import division
from __future__ import absolute_import
from __future__ import with_statement
from __future__ import print_function
from __future__ import unicode_literals

from datetime import datetime
from uuid import UUID

from flaskext.zodb import (Model, List, Mapping, BTree,
                           Timestamp, UUID4, current_db,
                           PersistentList, PersistentMapping, OOBTree)

from stutuz.tests import TestBase
from stutuz.extensions import db


class TestModel(Model):

    sequence = List
    mapping = Mapping
    btree = BTree
    timestamp = Timestamp
    id = UUID4
    something_else = None


class ZODB(TestBase):

    def test_model_attributes(self):
        """Model instantiates factories when Model instantiated"""

        instance = TestModel()
        self.assert_is_instance(instance.sequence, PersistentList)
        self.assert_is_instance(instance.mapping, PersistentMapping)
        self.assert_is_instance(instance.btree, OOBTree)
        self.assert_is_instance(instance.timestamp, datetime)
        self.assert_is_instance(instance.id, UUID)
        self.assert_is(instance.something_else, None)

    def test_model_kwargs(self):
        """Model init sets attributes with kwargs"""

        instance = TestModel(sequence=(1, 2, 3), mapping={'foo': 'bar'},
                             btree={'bar': 'foo'})
        self.assert_is_instance(instance.sequence, PersistentList)
        self.assert_is_instance(instance.mapping, PersistentMapping)
        self.assert_is_instance(instance.btree, OOBTree)
        self.assert_sequence_equal(instance.sequence, (1, 2, 3))
        self.assert_is(instance.something_else, None)

        instance = TestModel(other='foo', something_else=123)
        self.assert_equal(instance.other, 'foo')
        self.assert_equal(instance.something_else, 123)

    def test_read(self):
        """Views can read from the database"""

        with db() as root:
            root['_test'] = 'Victory!'

        @self.app.route('/_test/')
        def read_value():
            return db['_test']

        rv = self.client.get('/_test/')
        self.assert_equal(rv.data, 'Victory!')

    def test_write(self):
        """Views can write to the database"""

        @self.app.route('/_test/<value>')
        def write_value(value):
            db['_test'] = value

        self.client.get('/_test/Written!')

        with db() as root:
            self.assert_equal(root['_test'], 'Written!')

    def test_local_proxy(self):
        """current_db proxies to the ZODB instance"""

        self.assert_is(current_db.app, self.app)
        self.assert_equal(self.app.extensions['zodb'], current_db)
        self.assert_is(self.app.extensions['zodb'],
                       current_db._get_current_object())<commit_msg>Port zodb tests to Attest<commit_after>#-*- coding:utf-8 -*-

from __future__ import division
from __future__ import absolute_import
from __future__ import with_statement
from __future__ import print_function
from __future__ import unicode_literals

from datetime import datetime
from uuid import UUID

from flask import current_app
from flaskext.zodb import (Model, List, Mapping, BTree,
                           Timestamp, UUID4, current_db,
                           PersistentList, PersistentMapping, OOBTree)
from attest import Assert as _

from stutuz.tests.tools import flask_tests
from stutuz.extensions import db


class TestModel(Model):

    sequence = List
    mapping = Mapping
    btree = BTree
    timestamp = Timestamp
    id = UUID4
    something_else = None


suite = flask_tests()


@suite.test
def model_attributes(client):
    """Model instantiates factories when Model instantiated"""

    instance = _(TestModel())
    assert instance.sequence.__class__.is_(PersistentList)
    assert instance.mapping.__class__.is_(PersistentMapping)
    assert instance.btree.__class__.is_(OOBTree)
    assert instance.timestamp.__class__.is_(datetime)
    assert instance.id.__class__.is_(UUID)
    assert instance.something_else.is_(None)


@suite.test
def model_kwargs():
    """Model init sets attributes with kwargs"""

    instance = _(TestModel(sequence=(1, 2, 3),
                           mapping={'foo': 'bar'},
                           btree={'bar': 'foo'}))
    assert instance.sequence.__class__.is_(PersistentList)
    assert instance.mapping.__class__.is_(PersistentMapping)
    assert instance.btree.__class__.is_(OOBTree)
    assert instance.sequence ==  [1, 2, 3]
    assert instance.something_else.is_(None)

    instance = _(TestModel(other='foo', something_else=123))
    assert instance.other == 'foo'
    assert instance.something_else == 123


@suite.test
def read(client):
    """Views can read from the database"""

    with db() as root:
        root['_test'] = 'Victory!'

    @current_app.route('/_test/')
    def read_value():
        return db['_test']

    response = client.get('/_test/')
    assert response.data == 'Victory!'


@suite.test
def write(client):
    """Views can write to the database"""

    @current_app.route('/_test/<value>')
    def write_value(value):
        db['_test'] = value

    client.get('/_test/Written!')

    with db() as root:
        assert _(root['_test']) == 'Written!'


@suite.test
def local_proxy():
    """current_db proxies to the ZODB instance"""

    assert _(current_db.app).is_(current_app._get_current_object())
    assert _(current_app.extensions['zodb']) == current_db
    assert _(current_app.extensions['zodb']).is_(
             current_db._get_current_object())
{'fraction_non_alphanumeric': 0.10994297933000713, 'fraction_numerical': 0.005345687811831789, 'mean_word_length': 3.0881281864530226, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3786939', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1615, 'n_words': 386}