Skip to content

Commit

Permalink
Merge pull request #98 from ambitioninc/develop
Browse files Browse the repository at this point in the history
1.2.0
  • Loading branch information
somewes authored Jul 11, 2019
2 parents b8c1fd4 + fd0c6e5 commit 0c47cd1
Show file tree
Hide file tree
Showing 21 changed files with 360 additions and 248 deletions.
21 changes: 8 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
dist: xenial
language: python

sudo: false

python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
- "3.7"

env:
matrix:
- DJANGO=1.10
- DJANGO=1.11
- DJANGO=2.0
- DJANGO=2.1
- DJANGO=2.2
- DJANGO=master

addons:
postgresql: '9.5'
postgresql: '9.6'

matrix:
exclude:
- { python: "3.7", env: DJANGO=2.0 }

include:
- { python: "3.6", env: TOXENV=flake8 }

exclude:
- { python: "2.7", env: DJANGO=master }
- { python: "2.7", env: DJANGO=2.0 }
- { python: "3.4", env: DJANGO=master }
- { python: "3.6", env: DJANGO=1.10 }

allow_failures:
- env: DJANGO=master

Expand Down
10 changes: 10 additions & 0 deletions docs/release_notes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Release Notes
=============

v1.2.0
------
* Add Django 2.1
* Add Django 2.2
* Add python 3.7
* Drop python 2.7
* Drop python 3.4
* Drop python 3.5
* Drop Django 1.10

v1.1.0
------
* Use tox to support more versions
Expand Down
35 changes: 27 additions & 8 deletions querybuilder/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ def set_right_table(self, table):
# check if this join type is for a foreign key
for field in self.left_table.model._meta.fields:
if (
field.get_internal_type() == 'OneToOneField' or
field.get_internal_type() == 'ForeignKey'
field.get_internal_type() == 'OneToOneField' or field.get_internal_type() == 'ForeignKey'
):
if field.remote_field.model == self.right_table.model:
if self.right_table.field_prefix is None:
Expand Down Expand Up @@ -213,8 +212,7 @@ def get_condition(self):
# check if this join type is for a foreign key
for field in self.right_table.model._meta.fields:
if (
field.get_internal_type() == 'OneToOneField' or
field.get_internal_type() == 'ForeignKey'
field.get_internal_type() == 'OneToOneField' or field.get_internal_type() == 'ForeignKey'
):
if field.remote_field.model == self.left_table.model:
table_join_field = field.column
Expand Down Expand Up @@ -1159,11 +1157,32 @@ def get_update_sql(self, rows):
row_values = []
sql_args = []

for row in rows:
# Loop over each row and put the %s placeholder for each field
for i, row in enumerate(rows):

# Build a list of each field's placeholder
placeholders = []
for value in row:
sql_args.append(value)
placeholders.append('%s')

# If this is the first row, add casting information so the db knows the field types
if i == 0 and hasattr(self.tables[0], 'model'):
for field_index, value in enumerate(row):
# Append the value
sql_args.append(value)

# Figure out how to cast it
field_object = self.tables[0].model._meta.get_field(field_names[field_index])
db_type = field_object.db_type(self.connection)

# Don't cast the pk
if db_type == 'serial':
placeholders.append('%s')
else:
# Cast the placeholder to the data type
placeholders.append('%s::{0}'.format(db_type))
else:
for value in row:
sql_args.append(value)
placeholders.append('%s')
row_values.append('({0})'.format(', '.join(placeholders)))
row_values_sql = ', '.join(row_values)

Expand Down
1 change: 1 addition & 0 deletions querybuilder/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default_app_config = 'querybuilder.tests.apps.QuerybuilderTestConfig'
21 changes: 14 additions & 7 deletions querybuilder/tests/aggregate_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_avg(self):
]
)
query_str = query.get_sql()
expected_query = 'SELECT AVG(tests_order.margin) AS "margin_avg" FROM tests_order'
expected_query = 'SELECT AVG(querybuilder_tests_order.margin) AS "margin_avg" FROM querybuilder_tests_order'
self.assertEqual(query_str, expected_query, get_comparison_str(query_str, expected_query))

def test_max_field(self):
Expand All @@ -71,7 +71,7 @@ def test_max_field(self):
]
)
query_str = query.get_sql()
expected_query = 'SELECT MAX(tests_order.margin) AS "margin_max" FROM tests_order'
expected_query = 'SELECT MAX(querybuilder_tests_order.margin) AS "margin_max" FROM querybuilder_tests_order'
self.assertEqual(query_str, expected_query, get_comparison_str(query_str, expected_query))

def test_min_field(self):
Expand All @@ -85,7 +85,7 @@ def test_min_field(self):
]
)
query_str = query.get_sql()
expected_query = 'SELECT MIN(tests_order.margin) AS "margin_min" FROM tests_order'
expected_query = 'SELECT MIN(querybuilder_tests_order.margin) AS "margin_min" FROM querybuilder_tests_order'
self.assertEqual(query_str, expected_query, get_comparison_str(query_str, expected_query))

def test_stddev(self):
Expand All @@ -96,7 +96,9 @@ def test_stddev(self):
]
)
query_str = query.get_sql()
expected_query = 'SELECT STDDEV(tests_order.margin) AS "margin_stddev" FROM tests_order'
expected_query = (
'SELECT STDDEV(querybuilder_tests_order.margin) AS "margin_stddev" FROM querybuilder_tests_order'
)
self.assertEqual(query_str, expected_query, get_comparison_str(query_str, expected_query))

def test_sum_field(self):
Expand All @@ -110,7 +112,7 @@ def test_sum_field(self):
]
)
query_str = query.get_sql()
expected_query = 'SELECT SUM(tests_order.margin) AS "margin_sum" FROM tests_order'
expected_query = 'SELECT SUM(querybuilder_tests_order.margin) AS "margin_sum" FROM querybuilder_tests_order'
self.assertEqual(query_str, expected_query, get_comparison_str(query_str, expected_query))

def test_variance(self):
Expand All @@ -121,7 +123,9 @@ def test_variance(self):
]
)
query_str = query.get_sql()
expected_query = 'SELECT VARIANCE(tests_order.margin) AS "margin_variance" FROM tests_order'
expected_query = (
'SELECT VARIANCE(querybuilder_tests_order.margin) AS "margin_variance" FROM querybuilder_tests_order'
)
self.assertEqual(query_str, expected_query, get_comparison_str(query_str, expected_query))

def test_count(self):
Expand All @@ -139,7 +143,10 @@ def test_count(self):
received
)
)
self.assertEqual(query.get_count_query().get_sql(), 'SELECT COUNT(tests_user.*) AS "all_count" FROM tests_user')
self.assertEqual(
query.get_count_query().get_sql(),
'SELECT COUNT(querybuilder_tests_user.*) AS "all_count" FROM querybuilder_tests_user'
)

# Make sure the copy didn't modify the original
self.assertEqual(len(query.tables[0].fields), 2)
Expand Down
7 changes: 7 additions & 0 deletions querybuilder/tests/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.apps import AppConfig


class QuerybuilderTestConfig(AppConfig):
name = 'querybuilder.tests'
label = 'querybuilder_tests'
verbose_name = 'querybuilder Tests'
73 changes: 38 additions & 35 deletions querybuilder/tests/date_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def test_year(self):
]
)
query_str = query.get_sql()
expected_query = 'SELECT CAST(EXTRACT(year FROM tests_order.time) AS INT) AS "time__year" FROM tests_order'
expected_query = (
'SELECT CAST(EXTRACT(year FROM querybuilder_tests_order.time) AS INT) AS "time__year" '
'FROM querybuilder_tests_order'
)
self.assertEqual(query_str, expected_query, get_comparison_str(query_str, expected_query))

def test_year_auto(self):
Expand All @@ -25,9 +28,9 @@ def test_year_auto(self):
)
query_str = query.get_sql()
expected_query = (
'SELECT CAST(EXTRACT(year FROM tests_order.time) AS INT) AS "time__year", '
'CAST(EXTRACT(epoch FROM date_trunc(\'year\', tests_order.time)) AS INT) AS "time__epoch" '
'FROM tests_order '
'SELECT CAST(EXTRACT(year FROM querybuilder_tests_order.time) AS INT) AS "time__year", '
'CAST(EXTRACT(epoch FROM date_trunc(\'year\', querybuilder_tests_order.time)) AS INT) AS "time__epoch" '
'FROM querybuilder_tests_order '
'GROUP BY time__year, time__epoch '
'ORDER BY time__epoch ASC'
)
Expand All @@ -42,9 +45,9 @@ def test_year_auto_desc(self):
)
query_str = query.get_sql()
expected_query = (
'SELECT CAST(EXTRACT(year FROM tests_order.time) AS INT) AS "time__year", '
'CAST(EXTRACT(epoch FROM date_trunc(\'year\', tests_order.time)) AS INT) AS "time__epoch" '
'FROM tests_order '
'SELECT CAST(EXTRACT(year FROM querybuilder_tests_order.time) AS INT) AS "time__year", '
'CAST(EXTRACT(epoch FROM date_trunc(\'year\', querybuilder_tests_order.time)) AS INT) AS "time__epoch" '
'FROM querybuilder_tests_order '
'GROUP BY time__year, time__epoch '
'ORDER BY time__epoch DESC'
)
Expand All @@ -59,10 +62,10 @@ def test_month_auto(self):
)
query_str = query.get_sql()
expected_query = (
'SELECT CAST(EXTRACT(year FROM tests_order.time) AS INT) AS "time__year", '
'CAST(EXTRACT(month FROM tests_order.time) AS INT) AS "time__month", '
'CAST(EXTRACT(epoch FROM date_trunc(\'month\', tests_order.time)) AS INT) AS "time__epoch" '
'FROM tests_order '
'SELECT CAST(EXTRACT(year FROM querybuilder_tests_order.time) AS INT) AS "time__year", '
'CAST(EXTRACT(month FROM querybuilder_tests_order.time) AS INT) AS "time__month", '
'CAST(EXTRACT(epoch FROM date_trunc(\'month\', querybuilder_tests_order.time)) AS INT) AS "time__epoch" '
'FROM querybuilder_tests_order '
'GROUP BY time__year, time__month, time__epoch '
'ORDER BY time__epoch ASC'
)
Expand All @@ -77,12 +80,12 @@ def test_hour_auto(self):
)
query_str = query.get_sql()
expected_query = (
'SELECT CAST(EXTRACT(year FROM tests_order.time) AS INT) AS "time__year", '
'CAST(EXTRACT(month FROM tests_order.time) AS INT) AS "time__month", '
'CAST(EXTRACT(day FROM tests_order.time) AS INT) AS "time__day", '
'CAST(EXTRACT(hour FROM tests_order.time) AS INT) AS "time__hour", '
'CAST(EXTRACT(epoch FROM date_trunc(\'hour\', tests_order.time)) AS INT) AS "time__epoch" '
'FROM tests_order '
'SELECT CAST(EXTRACT(year FROM querybuilder_tests_order.time) AS INT) AS "time__year", '
'CAST(EXTRACT(month FROM querybuilder_tests_order.time) AS INT) AS "time__month", '
'CAST(EXTRACT(day FROM querybuilder_tests_order.time) AS INT) AS "time__day", '
'CAST(EXTRACT(hour FROM querybuilder_tests_order.time) AS INT) AS "time__hour", '
'CAST(EXTRACT(epoch FROM date_trunc(\'hour\', querybuilder_tests_order.time)) AS INT) AS "time__epoch" '
'FROM querybuilder_tests_order '
'GROUP BY time__year, time__month, time__day, time__hour, time__epoch '
'ORDER BY time__epoch ASC'
)
Expand All @@ -97,13 +100,13 @@ def test_minute_auto(self):
)
query_str = query.get_sql()
expected_query = (
'SELECT CAST(EXTRACT(year FROM tests_order.time) AS INT) AS "time__year", '
'CAST(EXTRACT(month FROM tests_order.time) AS INT) AS "time__month", '
'CAST(EXTRACT(day FROM tests_order.time) AS INT) AS "time__day", '
'CAST(EXTRACT(hour FROM tests_order.time) AS INT) AS "time__hour", '
'CAST(EXTRACT(minute FROM tests_order.time) AS INT) AS "time__minute", '
'CAST(EXTRACT(epoch FROM date_trunc(\'minute\', tests_order.time)) AS INT) AS "time__epoch" '
'FROM tests_order '
'SELECT CAST(EXTRACT(year FROM querybuilder_tests_order.time) AS INT) AS "time__year", '
'CAST(EXTRACT(month FROM querybuilder_tests_order.time) AS INT) AS "time__month", '
'CAST(EXTRACT(day FROM querybuilder_tests_order.time) AS INT) AS "time__day", '
'CAST(EXTRACT(hour FROM querybuilder_tests_order.time) AS INT) AS "time__hour", '
'CAST(EXTRACT(minute FROM querybuilder_tests_order.time) AS INT) AS "time__minute", '
'CAST(EXTRACT(epoch FROM date_trunc(\'minute\', querybuilder_tests_order.time)) AS INT) AS "time__epoch" '
'FROM querybuilder_tests_order '
'GROUP BY time__year, time__month, time__day, time__hour, time__minute, time__epoch '
'ORDER BY time__epoch ASC'
)
Expand All @@ -118,14 +121,14 @@ def test_second_auto(self):
)
query_str = query.get_sql()
expected_query = (
'SELECT CAST(EXTRACT(year FROM tests_order.time) AS INT) AS "time__year", '
'CAST(EXTRACT(month FROM tests_order.time) AS INT) AS "time__month", '
'CAST(EXTRACT(day FROM tests_order.time) AS INT) AS "time__day", '
'CAST(EXTRACT(hour FROM tests_order.time) AS INT) AS "time__hour", '
'CAST(EXTRACT(minute FROM tests_order.time) AS INT) AS "time__minute", '
'CAST(EXTRACT(second FROM tests_order.time) AS INT) AS "time__second", '
'CAST(EXTRACT(epoch FROM date_trunc(\'second\', tests_order.time)) AS INT) AS "time__epoch" '
'FROM tests_order '
'SELECT CAST(EXTRACT(year FROM querybuilder_tests_order.time) AS INT) AS "time__year", '
'CAST(EXTRACT(month FROM querybuilder_tests_order.time) AS INT) AS "time__month", '
'CAST(EXTRACT(day FROM querybuilder_tests_order.time) AS INT) AS "time__day", '
'CAST(EXTRACT(hour FROM querybuilder_tests_order.time) AS INT) AS "time__hour", '
'CAST(EXTRACT(minute FROM querybuilder_tests_order.time) AS INT) AS "time__minute", '
'CAST(EXTRACT(second FROM querybuilder_tests_order.time) AS INT) AS "time__second", '
'CAST(EXTRACT(epoch FROM date_trunc(\'second\', querybuilder_tests_order.time)) AS INT) AS "time__epoch" '
'FROM querybuilder_tests_order '
'GROUP BY time__year, time__month, time__day, time__hour, time__minute, time__second, time__epoch '
'ORDER BY time__epoch ASC'
)
Expand All @@ -140,8 +143,8 @@ def test_group_none(self):
)
query_str = query.get_sql()
expected_query = (
'SELECT CAST(EXTRACT(epoch FROM tests_order.time) AS INT) AS "time__epoch" '
'FROM tests_order '
'SELECT CAST(EXTRACT(epoch FROM querybuilder_tests_order.time) AS INT) AS "time__epoch" '
'FROM querybuilder_tests_order '
'GROUP BY time__epoch '
'ORDER BY time__epoch ASC'
)
Expand All @@ -155,5 +158,5 @@ def test_group_all(self):
]
)
query_str = query.get_sql()
expected_query = 'SELECT CAST(0 AS INT) AS "time__epoch" FROM tests_order'
expected_query = 'SELECT CAST(0 AS INT) AS "time__epoch" FROM querybuilder_tests_order'
self.assertEqual(query_str, expected_query, get_comparison_str(query_str, expected_query))
Loading

0 comments on commit 0c47cd1

Please sign in to comment.