示例#1
0
class Migration(migrations.Migration):

    initial = True

    dependencies = []

    operations = [
        migrations.CreateModel(
            name='question',
            fields=[
                ('id',
                 models.AutoField(auto_created=True,
                                  primary_key=True,
                                  serialize=False,
                                  verbose_name='ID')),
                ('question', models.CharField(max_length=4000)),
                ('expected_answer',
                 django_mysql.models.JSONField(default=dict)),
                ('created_at',
                 django_forcedfields.TimestampField(auto_now_add=True)),
                ('updated_at',
                 django_forcedfields.TimestampField(auto_now=True)),
            ],
            options={
                'db_table': 'question',
            },
        ),
    ]
示例#2
0
class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('mobileusers', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='complaint',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('geo_tag', models.TextField()),
                ('description', models.CharField(max_length=300)),
                ('created_at', django_forcedfields.TimestampField(auto_now_add=True)),
                ('updated_at', django_forcedfields.TimestampField(auto_now=True)),
                ('image', models.CharField(max_length=300, null=True)),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='mobileusers.mobileuser')),
            ],
            options={
                'db_table': 'complaint',
            },
        ),
    ]
示例#3
0
class Migration(migrations.Migration):

    initial = True

    dependencies = []

    operations = [
        migrations.CreateModel(
            name='mobileuser',
            fields=[
                ('google_id',
                 models.CharField(max_length=30,
                                  primary_key=True,
                                  serialize=False)),
                ('email', models.CharField(max_length=250, unique=True)),
                ('nickname', models.CharField(max_length=300)),
                ('phoneNo', models.CharField(max_length=100, null=True)),
                ('image', models.CharField(max_length=300, null=True)),
                ('nic', models.CharField(max_length=12, null=True,
                                         unique=True)),
                ('created_at',
                 django_forcedfields.TimestampField(auto_now_add=True)),
                ('updated_at',
                 django_forcedfields.TimestampField(auto_now=True)),
            ],
            options={
                'db_table': 'user',
            },
        ),
    ]
示例#4
0
class Migration(migrations.Migration):

    initial = True

    dependencies = []

    operations = [
        migrations.CreateModel(
            name='hotel',
            fields=[
                ('id',
                 models.AutoField(auto_created=True,
                                  primary_key=True,
                                  serialize=False,
                                  verbose_name='ID')),
                ('hotel_name', models.CharField(max_length=1000)),
                ('geo_tag', models.TextField()),
                ('address', models.CharField(max_length=3000)),
                ('created_at',
                 django_forcedfields.TimestampField(auto_now_add=True)),
                ('updated_at',
                 django_forcedfields.TimestampField(auto_now=True)),
            ],
            options={
                'db_table': 'hotel',
            },
        ),
    ]
示例#5
0
class Document(django.db.models.Model):
    """
    A document that will be monitored for changes.

    The natural key is the module and the document name. This does allow for
    possible document duplication but it is primarily the user's
    responsibility to check when installing new snapshot jobs via plugin modules
    or even manually through direct record insertion.

    """

    document_id = django.db.models.AutoField(primary_key=True)
    module = django.db.models.CharField(
        blank=False,
        default=None,
        max_length=255,
        null=False,
        help_text=(
            'The name of the module from which this record was installed.'))
    name = django.db.models.CharField(blank=False,
                                      default=None,
                                      max_length=255,
                                      null=False,
                                      help_text='The name of the document.')
    updated_timestamp = forcedfields.TimestampField(auto_now=True)
    languages = django.db.models.ManyToManyField('Language',
                                                 through='DocumentsLanguages')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'document'
        unique_together = ('module', 'name')
示例#6
0
    def test_field_argument_check(self):
        """
        Ensure keyword argument rules are enforced.

        For example, check that mutual exclusivity constraints between auto_now and auto_now_update
        are enforced. New field classes are defined here since the test models defined in
        tests.utils include only valid field argument permutations.

        For some reason, model and field check() methods are not called during test database setup
        or when dynamically creating and migrating model classes. I don't currently know where and
        when the checks are run or when the checks framework raises the returned errors. I'm just
        going to manually call the check() method here.

        I concatenate a test iteration count to the dynamic class name to prevent Django from
        issuing a warning "RuntimeWarning: Model 'tests.testmodel' was already registered." This
        error is caused by attempting to re-register a symbol in the current namespace so I ensure
        that each new invalid field class defined has a unique name.

        Note:
            "Validation" covers actual model attribute values, not the field class instance
            arguments. "Checks" cover the field class arguments and field class state.

        See:
            https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#what-does-a-field-class-do
            https://docs.djangoproject.com/en/dev/topics/checks/

        """
        check_tests = {
            'fields.E160': {
                'auto_now': True,
                'auto_now_add': True
            },
            'django_forcedfields.E160': {
                'auto_now': True,
                'auto_now_update': True
            }
        }

        check_test_count = 0
        for check_error_id, kwargs in check_tests.items():
            test_model_members = {
                'ts_field_1': django_forcedfields.TimestampField(**kwargs),
                '__module__': __name__
            }
            test_model_class = type('TestModel' + str(check_test_count),
                                    (django.db.models.Model, ),
                                    test_model_members)
            model_instance = test_model_class(ts_field_1='2000-01-01 00:00:01')
            check_results = model_instance.check()

            with self.subTest(field_args=', '.join(kwargs.keys())):
                self.assertEqual(len(check_results), 1)
                self.assertEqual(check_results[0].id, check_error_id)

            check_test_count = check_test_count + 1
示例#7
0
    def test_field_deconstruction(self):
        """
        Test the custom field's deconstruct() method.

        See:
            https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#field-deconstruction

        """
        test_field = django_forcedfields.TimestampField(auto_now_add=True,
                                                        auto_now_update=True,
                                                        null=True)
        name, path, args, kwargs = test_field.deconstruct()
        reconstructed_test_field = django_forcedfields.TimestampField(
            *args, **kwargs)

        self.assertEqual(test_field.auto_now,
                         reconstructed_test_field.auto_now)
        self.assertEqual(test_field.auto_now_update,
                         reconstructed_test_field.auto_now_update)
        self.assertEqual(test_field.null, reconstructed_test_field.null)
示例#8
0
    def test_field_argument_check(self):
        """
        Ensure keyword argument rules are enforced.

        For some reason, model and field check() methods are not called during
        test database setup or when dynamically creating and migrating model
        classes. I don't currently know where and when the checks are run or
        when the checks framework raises the returned errors.

        I'm just going to manually call the check() method here.

        I concatenate a test iteration count to the dynamic class name to
        prevent Django from issuing a warning "RuntimeWarning: Model
        'tests.testmodel' was already registered."

        Note:
            Validation covers actual model attribute values, not the field class
            instance arguments. Checks cover the field class arguments and
            field class state.

        See:
            https://docs.djangoproject.com/en/dev/topics/checks/

        """
        check_tests = {
            'fields.E160': {
                'auto_now': True,
                'auto_now_add': True
            },
            'django_forcedfields.E160': {
                'auto_now': True,
                'auto_now_update': True
            }
        }

        check_test_count = 0
        for check_error_id, kwargs in check_tests.items():
            test_model_members = {
                'ts_field_1': django_forcedfields.TimestampField(**kwargs),
                '__module__': __name__
            }
            test_model_class = type('TestModel' + str(check_test_count),
                                    (django.db.models.Model, ),
                                    test_model_members)
            model_instance = test_model_class(ts_field_1='2000-01-01 00:00:01')
            check_results = model_instance.check()

            with self.subTest(field_args=', '.join(kwargs.keys())):
                self.assertEqual(len(check_results), 1)
                self.assertEqual(check_results[0].id, check_error_id)

            check_test_count = check_test_count + 1
示例#9
0
class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('hotel', '0004_auto_20191028_0507'),
        ('mobileusers', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='review',
            fields=[
                ('id',
                 models.AutoField(auto_created=True,
                                  primary_key=True,
                                  serialize=False,
                                  verbose_name='ID')),
                ('qa', models.TextField()),
                ('geo_tag', models.TextField()),
                ('device_signature', models.TextField()),
                ('created_at',
                 django_forcedfields.TimestampField(auto_now_add=True)),
                ('updated_at',
                 django_forcedfields.TimestampField(auto_now=True)),
                ('hotel',
                 models.ForeignKey(
                     on_delete=django.db.models.deletion.DO_NOTHING,
                     to='hotel.hotel')),
                ('user',
                 models.ForeignKey(
                     on_delete=django.db.models.deletion.DO_NOTHING,
                     to='mobileusers.mobileuser')),
            ],
            options={
                'db_table': 'reviews',
            },
        ),
    ]
示例#10
0
    def _test_db_type_postgresql(self):
        """
        Test output of custom field db_type method with the PostgreSQL backend.

        Internal component of the custom field's db_type method return values.

        """
        connection = django.db.connections[test_utils.ALIAS_POSTGRESQL]
        for config in test_utils.TS_FIELD_TEST_CONFIGS:
            current_kwargs_string = ', '.join(config.kwargs_dict.keys())

            with self.subTest(arguments=current_kwargs_string):
                test_field = django_forcedfields.TimestampField(
                    **config.kwargs_dict)
                self.assertEqual(test_field.db_type(connection),
                                 config.db_type_postgresql)
示例#11
0
    def test_db_type(self):
        """
        Test output of the field's overridden "db_type" method.

        The Field.db_type() method is responsible for generating most of the SQL field's DDL such
        as field data type, defaults, ON UPDATE clauses, etc. Ensure that db_type is generating
        accurate DDL for the given field kwarg combination.

        """
        for test_config in test_utils.TS_TEST_CONFIGS:
            test_field = django_forcedfields.TimestampField(
                **test_config.kwargs_dict)
            test_kwargs_string = ', '.join(test_config.kwargs_dict.keys())
            for alias, expected_output in test_config.db_type_dict.items():
                connection = django.db.connections[alias]
                connection_engine = connection.settings_dict['ENGINE']
                with self.subTest(backend=connection_engine,
                                  kwargs=test_kwargs_string):
                    self.assertEqual(test_field.db_type(connection),
                                     expected_output)
示例#12
0
class DocumentsLanguages(django.db.models.Model):
    """
    A many-to-many relationship between a document and a language.

    Represents a "document instance." This is the core of this app. The app
    will use this model to get a snapshot job's URI

    Experience has taught me the value of using an auto-generated primary key on
    records that describe a relationship. In fact, look no further than the
    Snapshot model for an illustration of the PK utility. Don't even bother
    asking me to remove the AutoField.

    is_enabled is a boolean value. When true, it indicates that the document, in
    the given language, is being polled with each job execution. This field is
    stored in the DB as a tinyint using eight bytes since MariaDB/MySQL stores
    bit fields in an integer type field (tinyint) anyway.

    """

    documents_languages_id = django.db.models.AutoField(primary_key=True)
    document_id = django.db.models.ForeignKey(
        Document,
        db_column='document_id',
        on_delete=django.db.models.PROTECT,
        verbose_name='document')
    language_id = django.db.models.ForeignKey(
        Language,
        db_column='language_id',
        on_delete=django.db.models.PROTECT,
        verbose_name='language')
    url = django.db.models.URLField(blank=False,
                                    default=None,
                                    max_length=255,
                                    null=False)
    is_enabled = django.db.models.BooleanField(default=True)
    updated_timestamp = forcedfields.TimestampField(auto_now=True)

    class Meta:
        db_table = 'documents_languages'
        unique_together = ('document_id', 'language_id')
        verbose_name = 'document instance'
示例#13
0
_THIS_MODULE = sys.modules[__name__]

# Dynamically generate FixedCharField test models.
for test_config in test_utils.FC_TEST_CONFIGS:
    model_class_name = test_utils.get_fc_model_class_name(
        **test_config.kwargs_dict)
    model_class_attributes = {
        test_utils.FC_FIELD_ATTRNAME:
        django_forcedfields.FixedCharField(**test_config.kwargs_dict),
        '__module__':
        __name__
    }
    model_class = type(model_class_name, (django.db.models.Model, ),
                       model_class_attributes)
    setattr(_THIS_MODULE, model_class_name, model_class)

# Dynamically generate TimestampField test models.
for config in test_utils.TS_TEST_CONFIGS:
    model_class_name = test_utils.get_ts_model_class_name(**config.kwargs_dict)
    model_class_attributes = {
        test_utils.TS_FIELD_ATTRNAME:
        django_forcedfields.TimestampField(**config.kwargs_dict),
        test_utils.TS_UPDATE_FIELD_ATTRNAME:
        django.db.models.SmallIntegerField(null=True),
        '__module__':
        __name__
    }
    model_class = type(model_class_name, (django.db.models.Model, ),
                       model_class_attributes)
    setattr(_THIS_MODULE, model_class_name, model_class)
示例#14
0
# temp tables that could be used, there is no way to verify the correct table
# structure as temp tables are not visible in the information_schema. I could
# parse a SHOW CREATE TABLE <table_name> as it does acknowledge temp tables but
# that just seemed too smelly, brittle, and desperate. Another option was to use
# django.test.TransactionTestCase to avoid transactions altogether but, based on
# my experience, this greatly slows down tests although I'm still considering
# it. Note that PostgreSQL supports transactional DDL statements but
# unfortunately I can't just test PostgreSQL.
#
# Option three, while less appealing to me due to the vast number of models
# cluttering up the models module and making test DB setup and teardown slower,
# seems to work with Django's test infrastructure rather than against it. I'll
# use it for now.
#
# See:
# https://github.com/django/django/blob/master/django/db/backends/base/schema.py
# https://docs.python.org/3/library/functions.html#type
_THIS_MODULE = sys.modules[__name__]
for config in test_utils.TS_FIELD_TEST_CONFIGS:
    test_model_class_name = test_utils.get_ts_model_class_name(
        **config.kwargs_dict)
    test_model_class_members = {
        test_utils.TS_FIELD_TEST_ATTRNAME: django_forcedfields.TimestampField(
            **config.kwargs_dict),
        test_utils.UPDATE_FIELD_TEST_ATTRNAME: \
            django.db.models.SmallIntegerField(null=True),
        '__module__': __name__}
    test_model_class = type(test_model_class_name, (django.db.models.Model, ),
                            test_model_class_members)
    setattr(_THIS_MODULE, test_model_class_name, test_model_class)