<commit_before>import trafaret as t
from pymongo import DESCENDING
from conftest import BaseTest
from flaskext.mongoobject import Model


class BaseModel(Model):
    __abstract__ = True
    structure = t.Dict({
        'name': t.String,
        'quantity': t.Int,
        'attrs': t.Mapping(t.String, t.Or(t.Int, t.Float, t.String)),
    }).allow_extra('*')
    i18n = ['name', 'attrs']
    indexes = ['id']

class i18nModel(BaseModel):
    __collection__ = "i18ntests"
    inc_id = True
    structure = t.Dict({
        'list_attrs': t.List(t.String)
    }).allow_extra('*')
    i18n = ['list_attrs']
    indexes = [('quantity', DESCENDING), 'name' ]


class TestValidation(BaseTest):

    model = i18nModel

    def setUp(self):
        super(TestValidation, self).setUp()
        self.db.register(self.model)

    def test_validate_translated_attrs(self):
        try:
            self.model.create({'name': 1, 'quantity': 1})
            assert False
        except t.DataError:
            assert True

        try:
            self.model.create({'name': 'Name', 'quantity': 1,
                               'attrs':{'feature': {}, 'revision': 1},
                               'list_attrs':[]})
            assert False
        except t.DataError:
            assert True

        result = self.model.create({'name': 'Name', 'quantity': 1,
                                    'attrs':{'feature': 'ice', 'revision': 1},
                                    'list_attrs':['one', 'two']})

        try:
            result.update(attrs={'featre':[1, 2, 3]})
            assert False
        except t.DataError:
            assert True

    def test_translate(self):
        result = self.model.get_or_create({'name': 'Name', 'quantity': 1,
                                    'attrs':{'feature': 'ice', 'revision': 1},
                                    'list_attrs':['one', 'two']}, _lang='en')
        assert result.name == 'Name'
        result._lang = 'fr'
        result.update({'name': 'Nom'})

        # attr name translated but not feature and list_attrs:
        assert result.name == 'Nom'
        assert result.attrs.feature == 'ice'
        assert result.list_attrs == ['one', 'two']

        result.update({'attrs': {'feature': 'glace', 'revision': 1}})
        result.update({'list_attrs': ['un', 'deux']})

        result = self.model.query.find_one({'name': 'Nom'}, _lang='fr')
        assert result.attrs.feature == 'glace'
        assert result.list_attrs == ['un', 'deux']

        result = self.model.query.find({'attrs.feature': 'ice'})[0]
        assert result.attrs.feature == 'ice'
        assert result.name == 'Name'
        assert result.list_attrs == ['one', 'two']
        assert not self.model.query.find({'attrs.feature': 'something else'}).count()<commit_msg>Added test for i18n get_or_create<commit_after>import trafaret as t
from pymongo import DESCENDING
from conftest import BaseTest
from flaskext.mongoobject import Model


class BaseModel(Model):
    __abstract__ = True
    structure = t.Dict({
        'name': t.String,
        'quantity': t.Int,
        'attrs': t.Mapping(t.String, t.Or(t.Int, t.Float, t.String)),
    }).allow_extra('*')
    i18n = ['name', 'attrs']
    indexes = ['id']

class i18nModel(BaseModel):
    __collection__ = "i18ntests"
    inc_id = True
    structure = t.Dict({
        'list_attrs': t.List(t.String)
    }).allow_extra('*')
    i18n = ['list_attrs']
    indexes = [('quantity', DESCENDING), 'name' ]


class TestValidation(BaseTest):

    model = i18nModel

    def setUp(self):
        super(TestValidation, self).setUp()
        self.db.register(self.model)

    def test_validate_translated_attrs(self):
        try:
            self.model.create({'name': 1, 'quantity': 1})
            assert False
        except t.DataError:
            assert True

        try:
            self.model.create({'name': 'Name', 'quantity': 1,
                               'attrs':{'feature': {}, 'revision': 1},
                               'list_attrs':[]})
            assert False
        except t.DataError:
            assert True

        result = self.model.create({'name': 'Name', 'quantity': 1,
                                    'attrs':{'feature': 'ice', 'revision': 1},
                                    'list_attrs':['one', 'two']})

        try:
            result.update(attrs={'featre':[1, 2, 3]})
            assert False
        except t.DataError:
            assert True

    def test_translate(self):
        result = self.model.get_or_create({'name': 'Name', 'quantity': 1,
                                    'attrs':{'feature': 'ice', 'revision': 1},
                                    'list_attrs':['one', 'two']}, _lang='en')
        assert result.name == 'Name'
        result._lang = 'fr'
        result.update({'name': 'Nom'})

        # attr name translated but not feature and list_attrs:
        assert result.name == 'Nom'
        assert result.attrs.feature == 'ice'
        assert result.list_attrs == ['one', 'two']

        result.update({'attrs': {'feature': 'glace', 'revision': 1}})
        result.update({'list_attrs': ['un', 'deux']})

        result = self.model.query.find_one({'name': 'Nom'}, _lang='fr')
        assert result.attrs.feature == 'glace'
        assert result.list_attrs == ['un', 'deux']

        result = self.model.query.find({'attrs.feature': 'ice'})[0]
        assert result.attrs.feature == 'ice'
        assert result.name == 'Name'
        assert result.list_attrs == ['one', 'two']
        assert not self.model.query.find({'attrs.feature': 'something else'}).count()

        assert self.model.get_or_create({'name': 'Nom', 'quantity': 1,
                                         'attrs': {'feature': 'glace', 'revision': 1},
                                         'list_attrs': ['un', 'deux']}, _lang='fr')
        assert self.model.query.count() == 1
{'fraction_non_alphanumeric': 0.16039335367921329, 'fraction_numerical': 0.008646998982706003, 'mean_word_length': 2.0266803488968703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1054421', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1739, 'n_words': 396}