Skip to content

Commit

Permalink
Merge pull request #28 from uncovertruth/feature/test_more
Browse files Browse the repository at this point in the history
Add more tests and some bug fixes
  • Loading branch information
tsuyukimakoto authored Jun 3, 2017
2 parents 5c33345 + 6efc41a commit 4e413b4
Show file tree
Hide file tree
Showing 9 changed files with 423 additions and 196 deletions.
2 changes: 1 addition & 1 deletion horizon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

__author__ = """UNCOVER TRUTH Inc."""
__email__ = '[email protected]'
__version__ = '0.0.5'
__version__ = '0.1.0'
21 changes: 13 additions & 8 deletions horizon/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from django.utils.functional import cached_property

from .manager import HorizontalManager
from .settings import get_config
from .utils import (
get_config_from_model,
get_group_from_model,
get_key_field_name_from_model,
get_or_create_index,
Expand Down Expand Up @@ -58,6 +58,7 @@ def check(cls, **kwargs):
errors.extend(clash_errors)
return errors

errors.extend(cls._check_horizontal_group(**kwargs))
errors.extend(cls._check_horizontal_key(**kwargs))
return errors

Expand All @@ -84,7 +85,7 @@ def _check_horizontal_meta(cls, **kwargs):

@classmethod
def _check_horizontal_group(cls, **kwargs):
if get_group_from_model(cls) in get_config()['GROUPS']:
if get_config_from_model(cls):
return []
return [
checks.Error(
Expand Down Expand Up @@ -127,15 +128,19 @@ def _get_unique_checks(self, exclude=None):
if name in exclude:
continue
if f.unique:
unique_fields_for_exclude.append(name)
unique_fields_for_exclude.append(
(model_class, (name, )),
)
unique_fields_with_horizontal_key.append(
(model_class, (get_key_field_name_from_model(self), name)),
)

unique_checks, date_checks = super(AbstractHorizontalModel, self)._get_unique_checks(
exclude=exclude + unique_fields_for_exclude,
)
return unique_checks + unique_fields_with_horizontal_key, date_checks
unique_checks, date_checks = super(AbstractHorizontalModel, self) \
._get_unique_checks(exclude=exclude)
for unique_check in unique_checks:
if unique_check in unique_fields_for_exclude:
continue
unique_fields_with_horizontal_key.append(unique_check)
return unique_fields_with_horizontal_key, date_checks

@cached_property
def _horizontal_key(self):
Expand Down
2 changes: 2 additions & 0 deletions horizon/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def get_key_field_name_from_model(model):


def get_config_from_group(horizontal_group):
if horizontal_group not in get_config()['GROUPS']:
return {}
return get_config()['GROUPS'][horizontal_group]


Expand Down
89 changes: 46 additions & 43 deletions tests/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-06-03 01:40
# Generated by Django 1.11.1 on 2017-06-03 04:22
from __future__ import unicode_literals

from django.conf import settings
Expand All @@ -19,12 +19,16 @@ class Migration(migrations.Migration):

operations = [
migrations.CreateModel(
name='AbstractModel',
name='ConcreteModel',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('pizza', models.CharField(max_length=15)),
('pizza', models.CharField(max_length=15, unique=True)),
('potate', models.CharField(max_length=15, unique=True)),
('coke', models.CharField(max_length=15, unique=True)),
('user', models.ForeignKey(db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
],
options={
'abstract': False,
'horizontal_group': 'b',
'horizontal_key': 'user',
},
Expand All @@ -33,23 +37,19 @@ class Migration(migrations.Migration):
],
),
migrations.CreateModel(
name='AnotherGroup',
name='HorizontalMetadata',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('egg', models.CharField(max_length=15)),
('sushi', models.CharField(default=None, max_length=15, null=True, unique=True)),
('user', models.ForeignKey(db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('group', models.CharField(max_length=15)),
('key', models.CharField(max_length=32)),
('index', models.PositiveSmallIntegerField()),
],
options={
'horizontal_group': 'b',
'horizontal_key': 'user',
'abstract': False,
},
managers=[
('objects', horizon.manager.HorizontalManager()),
],
),
migrations.CreateModel(
name='HorizonChild',
name='ManyModel',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
],
Expand All @@ -62,10 +62,11 @@ class Migration(migrations.Migration):
],
),
migrations.CreateModel(
name='HorizonParent',
name='OneModel',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('spam', models.CharField(max_length=15)),
('egg', models.CharField(default=None, max_length=15, null=True)),
('user', models.ForeignKey(db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
],
options={
Expand All @@ -77,54 +78,56 @@ class Migration(migrations.Migration):
],
),
migrations.CreateModel(
name='HorizontalMetadata',
name='ProxyBaseModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('group', models.CharField(max_length=15)),
('key', models.CharField(max_length=32)),
('index', models.PositiveSmallIntegerField()),
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('sushi', models.CharField(max_length=15, unique=True)),
],
options={
'abstract': False,
'horizontal_group': 'b',
'horizontal_key': 'user',
},
managers=[
('objects', horizon.manager.HorizontalManager()),
],
),
migrations.CreateModel(
name='ConcreteModel',
name='ProxiedModel',
fields=[
('abstractmodel_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='tests.AbstractModel')),
('potate', models.CharField(max_length=15)),
('proxybasemodel_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='tests.ProxyBaseModel')),
('tempura', models.CharField(max_length=15, unique=True)),
('karaage', models.CharField(max_length=15, unique=True)),
],
options={
'abstract': False,
'horizontal_group': None,
'horizontal_key': None,
},
bases=('tests.abstractmodel',),
bases=('tests.proxybasemodel',),
managers=[
('objects', horizon.manager.HorizontalManager()),
],
),
migrations.AlterUniqueTogether(
name='horizontalmetadata',
unique_together=set([('group', 'key')]),
),
migrations.AddField(
model_name='horizonchild',
name='parent',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='tests.HorizonParent'),
),
migrations.AddField(
model_name='horizonchild',
model_name='proxybasemodel',
name='user',
field=models.ForeignKey(db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='abstractmodel',
model_name='manymodel',
name='one',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='tests.OneModel'),
),
migrations.AddField(
model_name='manymodel',
name='user',
field=models.ForeignKey(db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL),
),
migrations.AlterUniqueTogether(
name='anothergroup',
unique_together=set([('user', 'egg')]),
name='horizontalmetadata',
unique_together=set([('group', 'key')]),
),
migrations.AlterUniqueTogether(
name='proxiedmodel',
unique_together=set([('tempura', 'karaage')]),
),
migrations.AlterUniqueTogether(
name='concretemodel',
unique_together=set([('pizza', 'coke')]),
),
]
37 changes: 26 additions & 11 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ class HorizontalMetadata(AbstractHorizontalMetadata):
pass


class HorizonParent(AbstractHorizontalModel):
class OneModel(AbstractHorizontalModel):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.DO_NOTHING,
db_constraint=False,
)
spam = models.CharField(max_length=15)
egg = models.CharField(max_length=15, null=True, default=None)

objects = HorizontalManager() # For Django<1.10

Expand All @@ -31,13 +32,13 @@ class Meta(object):
horizontal_key = 'user'


class HorizonChild(AbstractHorizontalModel):
class ManyModel(AbstractHorizontalModel):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.DO_NOTHING,
db_constraint=False,
)
parent = models.ForeignKey(HorizonParent, on_delete=models.CASCADE)
one = models.ForeignKey(OneModel, on_delete=models.CASCADE)

objects = HorizontalManager() # For Django<1.10

Expand All @@ -46,22 +47,26 @@ class Meta(object):
horizontal_key = 'user'


class AnotherGroup(AbstractHorizontalModel):
class ProxyBaseModel(AbstractHorizontalModel):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.DO_NOTHING,
db_constraint=False,
)
egg = models.CharField(max_length=15)
sushi = models.CharField(max_length=15, null=True, default=None, unique=True)

objects = HorizontalManager() # For Django<1.10
sushi = models.CharField(max_length=15, unique=True)

class Meta(object):
horizontal_group = 'b'
horizontal_key = 'user'


class ProxiedModel(ProxyBaseModel):
tempura = models.CharField(max_length=15, unique=True)
karaage = models.CharField(max_length=15, unique=True)

class Meta(object):
unique_together = (
('user', 'egg'),
('tempura', 'karaage'),
)


Expand All @@ -71,12 +76,22 @@ class AbstractModel(AbstractHorizontalModel):
on_delete=models.DO_NOTHING,
db_constraint=False,
)
pizza = models.CharField(max_length=15)
pizza = models.CharField(max_length=15, unique=True)
potate = models.CharField(max_length=15, unique=True)

class Meta(object):
abstract = True
horizontal_group = 'b'
horizontal_key = 'user'
unique_together = (
('pizza', 'potate'),
)


class ConcreteModel(AbstractModel):
potate = models.CharField(max_length=15)
coke = models.CharField(max_length=15, unique=True)

class Meta(AbstractModel.Meta):
unique_together = (
('pizza', 'coke'),
)
Loading

0 comments on commit 4e413b4

Please sign in to comment.