Skip to content
This repository has been archived by the owner on Jan 18, 2020. It is now read-only.

Commit

Permalink
Fix #6
Browse files Browse the repository at this point in the history
  • Loading branch information
bruth committed Jan 9, 2013
1 parent 86e29c4 commit fac9ff9
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 14 deletions.
6 changes: 3 additions & 3 deletions modeltree/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from modeltree.tests.utils import *
from modeltree.tests.query import *
from modeltree.tests.tree import *
from .utils import *
from .query import *
from .tree import *
Empty file.
26 changes: 26 additions & 0 deletions modeltree/tests/regressions/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Regression data model for issue #6
from django.db import models

class Specimen(models.Model):
aliquot_id = models.DecimalField(decimal_places=0, max_digits=16, db_column='ALIQUOT_ID', primary_key=True) # Field name made lowercase.
# snip...
class Meta:
db_table = u'specimen'
verbose_name = 'specimen'
verbose_name_plural = 'specimens'

class Subject(models.Model):
study_id = models.TextField(primary_key=True, db_column='study_id')
# snip...
class Meta:
db_table = u'subject'
verbose_name = 'subject'
verbose_name_plural = 'subjects'


class Link(models.Model):
aliquot_id = models.ForeignKey(Specimen, db_column='ALIQUOT_ID', primary_key=True)
study_id = models.ForeignKey(Subject, db_column='study_id')
# snip...
class Meta:
db_table = u'link'
18 changes: 18 additions & 0 deletions modeltree/tests/regressions/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.test import TestCase

from modeltree.tree import trees
from .models import Specimen, Link, Subject


class Bug6(TestCase):
"""Wrong primary key used when constructing joins.
https://github.com/cbmi/modeltree/issues/6
"""
def test(self):
mt = trees.create(Specimen)

qs, alias = mt.add_joins(Link)
self.assertEqual(str(qs.query), 'SELECT "specimen"."ALIQUOT_ID" FROM "specimen" LEFT OUTER JOIN "link" ON ("specimen"."ALIQUOT_ID" = "link"."ALIQUOT_ID")')

qs, alias = mt.add_joins(Subject)
self.assertEqual(str(qs.query), 'SELECT "specimen"."ALIQUOT_ID" FROM "specimen" LEFT OUTER JOIN "link" ON ("specimen"."ALIQUOT_ID" = "link"."ALIQUOT_ID") INNER JOIN "subject" ON ("link"."study_id" = "subject"."study_id")')
5 changes: 3 additions & 2 deletions modeltree/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
},
'project': {
'model': 'tests.Project'
}
},
}

INSTALLED_APPS = (
'modeltree',
'modeltree.tests'
'modeltree.tests',
'modeltree.tests.regressions',
)
12 changes: 4 additions & 8 deletions modeltree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,13 @@ def get_joins(self, **kwargs):
self.parent.db_table,
self.m2m_db_table,
self.parent.pk_column,
self.m2m_reverse_field if self.reverse else \
self.m2m_field,
self.m2m_reverse_field if self.reverse else self.m2m_field,
)

c2 = (
self.m2m_db_table,
self.db_table,
self.m2m_field if self.reverse else \
self.m2m_reverse_field,
self.m2m_field if self.reverse else self.m2m_reverse_field,
self.pk_column,
)

Expand All @@ -168,10 +166,8 @@ def get_joins(self, **kwargs):
c1 = (
self.parent.db_table,
self.db_table,
self.parent.pk_column if self.reverse else \
self.foreignkey_field,
self.foreignkey_field if self.reverse else \
self.parent.pk_column,
self.parent.pk_column if self.reverse else self.foreignkey_field,
self.foreignkey_field if self.reverse else self.pk_column,
)

copy = kwargs.copy()
Expand Down
8 changes: 7 additions & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#!/bin/sh

DJANGO_SETTINGS_MODULE='modeltree.tests.settings' PYTHONPATH=. coverage run `which django-admin.py` test modeltree
ARGS="$@"

if [ ! $ARGS ]; then
ARGS="modeltree regressions"
fi

DJANGO_SETTINGS_MODULE='modeltree.tests.settings' PYTHONPATH=. coverage run `which django-admin.py` test $ARGS
coverage html

0 comments on commit fac9ff9

Please sign in to comment.