def test_max_column(self): """ Test a maximum column select, with group by clause """ sql, args = RulingModel(self._provider).objects.values_list('name').aggregate(Max('id')).\ group_by('name').to_sql() self.assertEqual(sql, "SELECT name, MAX(id) as id__max FROM test_model GROUP BY name") sql, args = RulingModel(self._provider).objects.aggregate(Max('id')). \ group_by('name').to_sql() self.assertEqual(sql, "SELECT MAX(id) as id__max FROM test_model GROUP BY name")
def test_qoper_in(self): """ test the In QOper filter """ sql, args = RulingModel(self._provider).objects.filter(Q('name', QOper.O_IN, 'Jane', 'John', 'Patrick')).\ to_sql() self.assertEqual( sql, "SELECT * FROM test_model WHERE name IN (%s, %s, %s)") self.assertEqual(args, ['Jane', 'John', 'Patrick'])
def test_qoper_between(self): """ Test a Between QOper filter """ sql, args = RulingModel(self._provider).objects.filter( Q('name', QOper.O_BETWEEN, 1, 20)).to_sql() self.assertEqual( sql, "SELECT * FROM test_model WHERE name BETWEEN %s AND %s") self.assertEqual(args, [1, 20]) ts = datetime.now() sql, args = RulingModel(self._provider).objects.filter( Q('created', QOper.O_BETWEEN, ts - timedelta(days=2), ts)).to_sql() self.assertEqual( sql, "SELECT * FROM test_model WHERE created BETWEEN \'%s\' AND \'%s\'") self.assertEqual(args, [ts - timedelta(days=2), ts])
def test_count_column(self): """ Test a count column select, with group by clause """ sql, args = RulingModel(self._provider).objects.values_list('name').aggregate(Count('id')).\ group_by('name').to_sql() self.assertEqual(sql, "SELECT name, COUNT(id) as id__count FROM test_model GROUP BY name")
def test_qoper_equals(self): """ Test a Equals QOPer filter """ sql, args = RulingModel(self._provider).objects.filter( Q('id', QOper.O_EQUAL, 12345)).to_sql() self.assertEqual(sql, "SELECT * FROM test_model WHERE id = %s") self.assertEqual(args, [12345])
def test_qoper_like(self): """ Test a LIKE QOper filter """ sql, args = RulingModel(self._provider).objects.filter( Q('name', QOper.O_LIKE, '%test%')).to_sql() self.assertEqual(sql, "SELECT * FROM test_model WHERE name LIKE %s") self.assertEqual(args, ['%test%'])
def test_limit(self): """ Test the limit clause """ sql, args = RulingModel(self._provider).objects.limit(10).to_sql() self.assertEqual(sql, "SELECT * FROM test_model LIMIT 10")
result = dbconn.db_connected() class RulingStatus(IntEnum): """ Bit values """ META_DOWNLOADED = 1 META_CHANGED = 2 DOC_DOWNLOADED = 10 # loop until there are no documents that need downloading while True: # Only grab 10 records at a time rulings = RulingModel(dbconn).objects. \ filter(Q('status', QOper.O_LT, RulingStatus.DOC_DOWNLOADED.value, placeholder=dbconn.placeholder)). \ limit(10) if not rulings or len(rulings) == 0: break for ruling in rulings: print("downloading: {0}".format(ruling.ruling_no)) # Download... result = True if result is True: ruling.status = RulingStatus.DOC_DOWNLOADED.value ruling.save(cleaned=True)
def test_order_by(self): """ Test a simple order by clause """ sql, args = RulingModel(self._provider).objects.order_by('id').to_sql() self.assertEqual(sql, "SELECT * FROM test_model ORDER BY id")
def test_group_by(self): """ Test a simple group by clause """ sql, args = RulingModel(self._provider).objects.group_by('id').to_sql() self.assertEqual(sql, "SELECT * FROM test_model GROUP BY id")
def test_sum_column(self): """ Test a sum column select, no group by clause """ sql, args = RulingModel(self._provider).objects.aggregate( Sum('id')).to_sql() self.assertEqual(sql, "SELECT SUM(id) as id__sum FROM test_model")
def test_count_column(self): """ Test a count column select, no group by clause """ sql, args = RulingModel(self._provider).objects.aggregate( Count('id')).to_sql() self.assertEqual(sql, "SELECT COUNT(id) as id__count FROM test_model")
def test_min_column(self): """ Test a minimum column select, no group by clause """ sql, args = RulingModel(self._provider).objects.aggregate( Min('id')).to_sql() self.assertEqual(sql, "SELECT MIN(id) as id__min FROM test_model")
def test_simple_select_specific_columsn(self): """ Test simple select returning a column list and all rows """ sql, args = RulingModel(self._provider).objects.values_list( "id", "name").to_sql() self.assertEqual(sql, "SELECT id, name FROM test_model")
def test_simple_select(self): """ test simple select returning all columns and rows """ sql, args = RulingModel(self._provider).objects.to_sql() self.assertEqual(sql, 'SELECT * FROM test_model')