'Regression test for bug #7110.
When using select_related(), we must query the
Device and Building tables using two different aliases (each) in order to
differentiate the start and end Connection fields. The net result is that
both the "connections = ..." queries here should give the same results
without pulling in more than the absolute minimum number of tables
(history has shown that it\'s easy to make a mistake in the implementation
and include some unnecessary bonus joins).'

def test_regression_7110(self):

    b = Building.objects.create(name='101')
    dev1 = Device.objects.create(name='router', building=b)
    dev2 = Device.objects.create(name='switch', building=b)
    dev3 = Device.objects.create(name='server', building=b)
    port1 = Port.objects.create(port_number='4', device=dev1)
    port2 = Port.objects.create(port_number='7', device=dev2)
    port3 = Port.objects.create(port_number='1', device=dev3)
    c1 = Connection.objects.create(start=port1, end=port2)
    c2 = Connection.objects.create(start=port2, end=port3)
    connections = Connection.objects.filter(start__device__building=b, end__device__building=b).order_by('id')
    self.assertEquals([(c.id, unicode(c.start), unicode(c.end)) for c in connections], [(1, u'router/4', u'switch/7'), (2, u'switch/7', u'server/1')])
    connections = Connection.objects.filter(start__device__building=b, end__device__building=b).select_related().order_by('id')
    self.assertEquals([(c.id, unicode(c.start), unicode(c.end)) for c in connections], [(1, u'router/4', u'switch/7'), (2, u'switch/7', u'server/1')])
    self.assertEquals(connections.query.count_active_tables(), 7)
