<commit_before>"""Models tests."""

import datetime

import unittest2 as unittest

import six

from domain_models import models
from domain_models import fields
from domain_models import collections
from domain_models import errors


class BaseModelsTests(unittest.TestCase):
    """Basic model tests."""

    def test_set_and_get_attrs(self):
        """Test setting and getting of domain model attributes."""
        class User(models.DomainModel):
            """Test user domain model."""

            id = fields.Int()
            email = fields.String()
            first_name = fields.String()
            last_name = fields.String()
            gender = fields.String()
            birth_date = fields.String()

        user1 = User()
        user1.id = 1
        user1.email = '<EMAIL>'
        user1.first_name = 'John'
        user1.last_name = 'Smith'
        user1.gender = 'male'
        user1.birth_date = '05/04/1988'

        user2 = User()
        user2.id = 2
        user2.email = '<EMAIL>'
        user2.first_name = 'Jane'
        user2.last_name = 'Smith'
        user2.gender = 'female'
        user2.birth_date = '05/04/1985'

        self.assertEqual(user1.id, 1)
        self.assertEqual(user1.email, '<EMAIL>')
        self.assertEqual(user1.first_name, 'John')
        self.assertEqual(user1.last_name, 'Smith')
        self.assertEqual(user1.gender, 'male')
        self.assertEqual(user1.birth_date, '05/04/1988')

        self.assertEqual(user2.id, 2)
        self.assertEqual(user2.email, '<EMAIL>')
        self.assertEqual(user2.first_name, 'Jane')
        self.assertEqual(user2.last_name, 'Smith')
        self.assertEqual(user2.gender, 'female')
        self.assertEqual(user2.birth_date, '05/04/1985')

    def test_data_attr(self):
        """Test model's __data__ attribute."""
        class User(models.DomainModel):
            """Test user domain model."""

            id = fields.Int()
            email = fields.String()
            first_name = fields.String()
            last_name = fields.String()
            gender = fields.String()
            birth_date = fields.String()

        user1 = User()
        user1.id = 1
        user1.email = '<EMAIL>'
        user1.first_name = 'John'
        user1.last_name = 'Smith'
        user1.gender = 'male'
        user1.birth_date = '05/04/1988'

        user2 = User()
        user2.id = 2
        user2.email = '<EMAIL>'
        user2.first_name = 'Jane'
        user2.last_name = 'Smith'
        user2.gender = 'female'
        user2.birth_date = '05/04/1985'

        self.assertEquals(user1.__data__, dict(id=1,
                                               email='<EMAIL>',
                                               first_name='John',
                                               last_name='Smith',
                                               gender='male',
                                               birth_date='05/04/1988'))
        self.assertEquals(user2.__data__, dict(id=2,
                                               email='<EMAIL>',
                                               first_name='Jane',
                                               last_name='Smith',
                                               gender='female',
                                               birth_date='05/04/1985'))

    def test_not_valid_unique_key_field(self):
        """Test that error is raised when unique key is not correct."""
        with self.assertRaises(errors.Error):
            class Model(models.DomainModel):
                """Test model."""

                __unique_key__ = fields.Field()

    def test_not_valid_unique_key_object(self):
        """Test that error is raised when unique key is not correct."""
        with self.assertRaises(errors.Error):
            class Model(models.DomainModel):
                """Test model."""

                __unique_key__ = object()

    def test_not_valid_unique_key_scalar(self):
        """Test that error is raised when unique key is not correct."""
        with self.assertRaises(errors.Error):
            class Model(models.DomainModel):
                """Test model."""

                __unique_key__ = 1

    def test_not_valid_view_key_field(self):
        """Test that error is raised when view key is not correct."""
        with self.assertRaises(errors.Error):
            class Model(models.DomainModel):
                """Test model."""

                __view_key__ = fields.Field()

    def test_not_valid_view_key_object(self):
        """Test that error is raised when view key is not correct."""
        with self.assertRaises(errors.Error):
            class Model(models.DomainModel):
                """Test model."""

                __view_key__ = object()

    def test_not_valid_view_key_scalar(self):
        """Test that error is raised when view key is not correct."""
        with self.assertRaises(errors.Error):
            class Model(models.DomainModel):
                """Test model."""

                __view_key__ = 1

    def test_field_could_not_be_rebound_in_same_model(self):
        """Test that field could not be rebound."""
        with self.assertRaises(errors.Error):
            class Model(models.DomainModel):
                """Test model."""

                field = fields.Field()
                another_field = field

    def test_field_could_not_be_rebound_in_different_model(self):
        """Test that field could not be rebound."""
        class Model1(models.DomainModel):
            """Test model."""

            field = fields.Field()

        with self.assertRaises(errors.Error):
            class Model2(models.DomainModel):
                """Test model."""

                field = Model1.field

    def test_get_method_on_undefined(self):
        """Test method get of Model."""

        class Model(models.DomainModel):
            """Test model."""
            field = fields.Int()

        model = Model()

        with self.assertRaises(AttributeError):
            model.get('undefined')

    def test_get_method_on_int(self):
        """Test method get on Int of Model."""
        valid_defaults = [0, 3, 5.5, False, True]

        class Model(models.DomainModel):
            """Test model."""
            field = fields.Int()

        model = Model(field=2)
        for value in valid_defaults:
            self.assertEqual(model.get('field', default=value), 2)

        model = Model()
        self.assertEqual(model.get('field'), None)
        for value in valid_defaults:
            self.assertEqual(model.get('field', default=value), int(value))

        for value in ['', u'baz']:
            with self.assertRaises(ValueError):
                model.get('field', value)
                self.fail("Failed with {0}".format(value))

        with self.assertRaises(TypeError):
            model.get('field', object())

    def test_get_method_on_string(self):
        """Test method get on String of Model."""
        valid_defaults = ['', 'baz', u'baz', False, True, 1, 2.3]

        class Model(models.DomainModel):
            """Test model."""
            field = fields.String()

        model = Model(field='foobar')
        for value in valid_defaults:
            self.assertEqual(model.get('field', default=value), 'foobar')

        model = Model()
        self.assertEqual(model.get('field'), None)
        for value in valid_defaults:
            self.assertEqual(model.get('field', default=value), str(value))

    def test_get_method_on_bool(self):
        """Test method get on Bool of Model."""

        class Model(models.DomainModel):
            """Test model."""
            field = fields.Bool()

        model = Model(field=True)
        self.assertEqual(model.get('field'), True)
        self.assertEqual(model.get('field', default=False), True)

        model = Model(field=False)
        self.assertEqual(model.get('field'), False)
        self.assertEqual(model.get('field', default=True), False)

        model = Model()
        self.assertEqual(model.get('field'), None)
        self.assertEqual(model.get('field', default=False), False)
        self.assertEqual(model.get('field', default=True), True)

    def test_get_method_on_float(self):
        """Test method get on Float of Model."""
        valid_defaults = [7.5, 7, '7.5', '7', 0, '0.0', .9, '.9', False, True]

        class Model(models.DomainModel):
            """Test model."""
            field = fields.Float()

        model = Model(field=5.5)
        self.assertEqual(model.get('field'), 5.5)
        for value in valid_defaults:
            self.assertEqual(model.get('field', default=value), 5.5)

        model = Model()
        self.assertEqual(model.get('field'), None)
        for value in valid_defaults:
            self.assertEqual(model.get('field', default=value), float(value))

        for value in ['', 'baz', u'baz']:
            with self.assertRaises(ValueError):
                model.get('field', value)
                self.fail("Failed with {0}".format(value))

        with self.assertRaises(TypeError):
            model.get('field', object())

    def test_get_method_on_date(self):
        """Test method get on Date of Model."""
        once = datetime.date(year=1986, month=4, day=26)
        today = datetime.date.today()

        class Model(models.DomainModel):
            """Test model."""
            field = fields.Date()

        model = Model(field=today)
        self.assertEqual(model.get('field'), today)
        self.assertEqual(model.get('field', once), today)

        model = Model()
        self.assertEqual(model.get('field'), None)
        self.assertEqual(model.get('field', once), once)

        for value in ['', 'baz', u'baz', 0, 3, 0.7, '.5', False, True]:
            with self.assertRaises(TypeError):
                model.get('field', value)
                self.fail("Failed with {0}".format(value))

    def test_get_method_on_datetime(self):
        """Test method get on Date of Model."""
        once = datetime.datetime(year=1986, month=4, day=26)
        now = datetime.datetime.now()

        class Model(models.DomainModel):
            """Test model."""
            field = fields.DateTime()

        model = Model(field=now)
        self.assertEqual(model.get('field'), now)
        self.assertEqual(model.get('field', once), now)

        model = Model()
        self.assertEqual(model.get('field'), None)
        self.assertEqual(model.get('field', once), once)

        for value in ['', 'baz', u'baz', 0, 3, 0.7, '.5', False, True]:
            with self.assertRaises(TypeError):
                model.get('field', value)
                self.fail("Failed with {0}".format(value))

    def test_get_method_on_binary(self):
        """Test method get on Binary of Model."""
        self.skipTest("Test is not implemented yet")

    def test_get_method_on_model(self):
        """Test method get on Model of Model."""
        self.skipTest("Test is not implemented yet")

    def test_get_method_on_collection(self):
        """Test method get on Collection of Model."""
        self.skipTest("Test is not implemented yet")


class ModelReprTests(unittest.TestCase):
    """Tests for model Pythonic representation."""

    def test_repr(self):
        """Test model __repr__()."""
        class User(models.DomainModel):
            """Test user domain model."""

            id = fields.Int()
            email = fields.String()
            first_name = fields.String()
            last_name = fields.String()
            gender = fields.String()
            birth_date = fields.String()

        user = User()
        user.id = 1
        user.email = '<EMAIL>'
        user.first_name = 'John'
        user.last_name = 'Smith'
        user.gender = 'male'
        user.birth_date = '05/04/1988'

        user_repr = repr(user)

        self.assertIn('test_models.User', user_repr)
        self.assertIn('id=1', user_repr)
        self.assertIn('email=\'<EMAIL>\'', user_repr)
        self.assertIn('first_name={0}'.format(repr('John')), user_repr)
        self.assertIn('last_name={0}'.format(repr('Smith')), user_repr)
        self.assertIn('gender=\'male\'', user_repr)
        self.assertIn('birth_date=\'05/04/1988\'', user_repr)
<commit_msg>Issue #8: Test coverage first<commit_after>"""Models tests."""

import datetime

import unittest2 as unittest

import six

from domain_models import models
from domain_models import fields
from domain_models import collections
from domain_models import errors


class BaseModelsTests(unittest.TestCase):
    """Basic model tests."""

    def test_set_and_get_attrs(self):
        """Test setting and getting of domain model attributes."""
        class User(models.DomainModel):
            """Test user domain model."""

            id = fields.Int()
            email = fields.String()
            first_name = fields.String()
            last_name = fields.String()
            gender = fields.String()
            birth_date = fields.String()

        user1 = User()
        user1.id = 1
        user1.email = '<EMAIL>'
        user1.first_name = 'John'
        user1.last_name = 'Smith'
        user1.gender = 'male'
        user1.birth_date = '05/04/1988'

        user2 = User()
        user2.id = 2
        user2.email = '<EMAIL>'
        user2.first_name = 'Jane'
        user2.last_name = 'Smith'
        user2.gender = 'female'
        user2.birth_date = '05/04/1985'

        self.assertEqual(user1.id, 1)
        self.assertEqual(user1.email, '<EMAIL>')
        self.assertEqual(user1.first_name, 'John')
        self.assertEqual(user1.last_name, 'Smith')
        self.assertEqual(user1.gender, 'male')
        self.assertEqual(user1.birth_date, '05/04/1988')

        self.assertEqual(user2.id, 2)
        self.assertEqual(user2.email, '<EMAIL>')
        self.assertEqual(user2.first_name, 'Jane')
        self.assertEqual(user2.last_name, 'Smith')
        self.assertEqual(user2.gender, 'female')
        self.assertEqual(user2.birth_date, '05/04/1985')

    def test_data_attr(self):
        """Test model's __data__ attribute."""

        class User(models.DomainModel):
            """Test user domain model."""

            id = fields.Int()
            email = fields.String()
            first_name = fields.String()
            last_name = fields.String()
            gender = fields.String()
            birth_date = fields.String()

        user1 = User()
        user1.id = 1
        user1.email = '<EMAIL>'
        user1.first_name = 'John'
        user1.last_name = 'Smith'
        user1.gender = 'male'
        user1.birth_date = '05/04/1988'

        user2 = User()
        user2.id = 2
        user2.email = '<EMAIL>'
        user2.first_name = 'Jane'
        user2.last_name = 'Smith'
        user2.gender = 'female'
        user2.birth_date = '05/04/1985'

        self.assertEquals(user1.__data__, dict(id=1,
                                               email='<EMAIL>',
                                               first_name='John',
                                               last_name='Smith',
                                               gender='male',
                                               birth_date='05/04/1988'))
        self.assertEquals(user2.__data__, dict(id=2,
                                               email='<EMAIL>',
                                               first_name='Jane',
                                               last_name='Smith',
                                               gender='female',
                                               birth_date='05/04/1985'))

    def test_not_valid_unique_key_field(self):
        """Test that error is raised when unique key is not correct."""
        with self.assertRaises(errors.Error):
            class Model(models.DomainModel):
                """Test model."""

                __unique_key__ = fields.Field()

    def test_not_valid_unique_key_object(self):
        """Test that error is raised when unique key is not correct."""
        with self.assertRaises(errors.Error):
            class Model(models.DomainModel):
                """Test model."""

                __unique_key__ = object()

    def test_not_valid_unique_key_scalar(self):
        """Test that error is raised when unique key is not correct."""
        with self.assertRaises(errors.Error):
            class Model(models.DomainModel):
                """Test model."""

                __unique_key__ = 1

    def test_not_valid_view_key_field(self):
        """Test that error is raised when view key is not correct."""
        with self.assertRaises(errors.Error):
            class Model(models.DomainModel):
                """Test model."""

                __view_key__ = fields.Field()

    def test_not_valid_view_key_object(self):
        """Test that error is raised when view key is not correct."""
        with self.assertRaises(errors.Error):
            class Model(models.DomainModel):
                """Test model."""

                __view_key__ = object()

    def test_not_valid_view_key_scalar(self):
        """Test that error is raised when view key is not correct."""
        with self.assertRaises(errors.Error):
            class Model(models.DomainModel):
                """Test model."""

                __view_key__ = 1

    def test_field_could_not_be_rebound_in_same_model(self):
        """Test that field could not be rebound."""
        with self.assertRaises(errors.Error):
            class Model(models.DomainModel):
                """Test model."""

                field = fields.Field()
                another_field = field

    def test_field_could_not_be_rebound_in_different_model(self):
        """Test that field could not be rebound."""
        class Model1(models.DomainModel):
            """Test model."""

            field = fields.Field()

        with self.assertRaises(errors.Error):
            class Model2(models.DomainModel):
                """Test model."""

                field = Model1.field

    def test_get_method_on_undefined(self):
        """Test method get of Model."""

        class Model(models.DomainModel):
            """Test model."""
            field = fields.Int()

        model = Model()

        with self.assertRaises(AttributeError):
            model.get('undefined')

    def test_get_method_on_int(self):
        """Test method get on Int of Model."""
        valid_defaults = [0, 3, 5.5, False, True]

        class Model(models.DomainModel):
            """Test model."""
            field = fields.Int()

        model = Model(field=2)
        for value in valid_defaults:
            self.assertEqual(model.get('field', default=value), 2)

        model = Model()
        self.assertEqual(model.get('field'), None)
        for value in valid_defaults:
            self.assertEqual(model.get('field', default=value), int(value))

        for value in ['', u'baz']:
            with self.assertRaises(ValueError):
                model.get('field', value)
                self.fail("Failed with {0}".format(value))

        with self.assertRaises(TypeError):
            model.get('field', object())

    def test_get_method_on_string(self):
        """Test method get on String of Model."""
        valid_defaults = ['', 'baz', u'baz', False, True, 1, 2.3]

        class Model(models.DomainModel):
            """Test model."""
            field = fields.String()

        model = Model(field='foobar')
        for value in valid_defaults:
            self.assertEqual(model.get('field', default=value), 'foobar')

        model = Model()
        self.assertEqual(model.get('field'), None)
        for value in valid_defaults:
            self.assertEqual(model.get('field', default=value), str(value))

    def test_get_method_on_bool(self):
        """Test method get on Bool of Model."""

        class Model(models.DomainModel):
            """Test model."""
            field = fields.Bool()

        model = Model(field=True)
        self.assertEqual(model.get('field'), True)
        self.assertEqual(model.get('field', default=False), True)

        model = Model(field=False)
        self.assertEqual(model.get('field'), False)
        self.assertEqual(model.get('field', default=True), False)

        model = Model()
        self.assertEqual(model.get('field'), None)
        self.assertEqual(model.get('field', default=False), False)
        self.assertEqual(model.get('field', default=True), True)

    def test_get_method_on_float(self):
        """Test method get on Float of Model."""
        valid_defaults = [7.5, 7, '7.5', '7', 0, '0.0', .9, '.9', False, True]

        class Model(models.DomainModel):
            """Test model."""
            field = fields.Float()

        model = Model(field=5.5)
        self.assertEqual(model.get('field'), 5.5)
        for value in valid_defaults:
            self.assertEqual(model.get('field', default=value), 5.5)

        model = Model()
        self.assertEqual(model.get('field'), None)
        for value in valid_defaults:
            self.assertEqual(model.get('field', default=value), float(value))

        for value in ['', 'baz', u'baz']:
            with self.assertRaises(ValueError):
                model.get('field', value)
                self.fail("Failed with {0}".format(value))

        with self.assertRaises(TypeError):
            model.get('field', object())

    def test_get_method_on_date(self):
        """Test method get on Date of Model."""
        once = datetime.date(year=1986, month=4, day=26)
        today = datetime.date.today()

        class Model(models.DomainModel):
            """Test model."""
            field = fields.Date()

        model = Model(field=today)
        self.assertEqual(model.get('field'), today)
        self.assertEqual(model.get('field', once), today)

        model = Model()
        self.assertEqual(model.get('field'), None)
        self.assertEqual(model.get('field', once), once)

        for value in ['', 'baz', u'baz', 0, 3, 0.7, '.5', False, True]:
            with self.assertRaises(TypeError):
                model.get('field', value)
                self.fail("Failed with {0}".format(value))

    def test_get_method_on_datetime(self):
        """Test method get on Date of Model."""
        once = datetime.datetime(year=1986, month=4, day=26)
        now = datetime.datetime.now()

        class Model(models.DomainModel):
            """Test model."""
            field = fields.DateTime()

        model = Model(field=now)
        self.assertEqual(model.get('field'), now)
        self.assertEqual(model.get('field', once), now)

        model = Model()
        self.assertEqual(model.get('field'), None)
        self.assertEqual(model.get('field', once), once)

        for value in ['', 'baz', u'baz', 0, 3, 0.7, '.5', False, True]:
            with self.assertRaises(TypeError):
                model.get('field', value)
                self.fail("Failed with {0}".format(value))

    def test_get_method_on_binary(self):
        """Test method get on Binary of Model."""
        self.skipTest("Test is not implemented yet")

    def test_get_method_on_model(self):
        """Test method get on Model of Model."""
        self.skipTest("Test is not implemented yet")

    def test_get_method_on_collection(self):
        """Test method get on Collection of Model."""
        self.skipTest("Test is not implemented yet")

    def test_get_data_method(self):
        class Photo(models.DomainModel):
            id = fields.Int()
            url = fields.String()

        class Profile(models.DomainModel):
            id = fields.Int()
            name = fields.String()
            main_photo = fields.Model(Photo)
            photos = fields.Collection(Photo)
            birth_date = fields.Date()
            sequence = fields.Collection(fields.Int)

        photo1 = Photo(id=1, url='http://boonya.info/wat.jpg?1')
        photo2 = Photo(id=2, url='http://boonya.info/wat.jpg?2')
        profile = Profile(id=1, name='John', main_photo=photo1,
                          photos=[photo1, photo2],
                          sequence=[1, 1, 2, 3, 5, 8, 13],
                          birth_date=datetime.date(year=1986, month=4,
                                                   day=26))

        self.assertDictEqual(profile.get_data(), {
            'id': 1,
            'name': 'John',
            'main_photo': {'id': 1,
                           'url': 'http://boonya.info/wat.jpg?1'},
            'photos': [
                {'id': 1, 'url': 'http://boonya.info/wat.jpg?1'},
                {'id': 2, 'url': 'http://boonya.info/wat.jpg?2'}
            ],
            'sequence': [1, 1, 2, 3, 5, 8, 13],
            'birth_date': datetime.date(year=1986, month=4, day=26)
        })


class ModelReprTests(unittest.TestCase):
    """Tests for model Pythonic representation."""

    def test_repr(self):
        """Test model __repr__()."""

        class User(models.DomainModel):
            """Test user domain model."""

            id = fields.Int()
            email = fields.String()
            first_name = fields.String()
            last_name = fields.String()
            gender = fields.String()
            birth_date = fields.String()

        user = User()
        user.id = 1
        user.email = '<EMAIL>'
        user.first_name = 'John'
        user.last_name = 'Smith'
        user.gender = 'male'
        user.birth_date = '05/04/1988'

        user_repr = repr(user)

        self.assertIn('test_models.User', user_repr)
        self.assertIn('id=1', user_repr)
        self.assertIn('email=\'<EMAIL>\'', user_repr)
        self.assertIn('first_name={0}'.format(repr('John')), user_repr)
        self.assertIn('last_name={0}'.format(repr('Smith')), user_repr)
        self.assertIn('gender=\'male\'', user_repr)
        self.assertIn('birth_date=\'05/04/1988\'', user_repr)

{'fraction_non_alphanumeric': 0.13069779986899396, 'fraction_numerical': 0.01679959927561361, 'mean_word_length': 1.9396307622607316, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6499114', 'n_tokens_mistral': 7545, 'n_tokens_neox': 7043, 'n_words': 1623}