Skip to content

Commit

Permalink
Merge pull request #35 from rbw0/branch0.4.3
Browse files Browse the repository at this point in the history
Branch0.4.3
  • Loading branch information
rbw authored Apr 27, 2017
2 parents b636a3d + 2a163de commit 18a73f9
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 125 deletions.
21 changes: 20 additions & 1 deletion docs/usage/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Connecting

This shows some examples of how to connect with pysnow using either username and password or OAuth.

See the :meth:`documentation for `pysnow.Client` <pysnow.Client>` for more info
See the :meth:`pysnow.Client` documentation for details.

Using username and password
---------------------------
Expand All @@ -18,6 +18,25 @@ Using username and password
password='mypassword')
Using a custom session object
-----------------------------

You can pass a custom session object to `Client`.
In this example regular user / pass authentication is used, but with SSL verification disabled.

.. code-block:: python
import pysnow
import requests
s = requests.Session()
s.verify = False
s.auth = requests.auth.HTTPBasicAuth('myusername', 'mypassword')
# Create client object
sn = pysnow.Client(instance='myinstance', session=s)
Using OAuth
-----------

Expand Down
10 changes: 6 additions & 4 deletions docs/usage/full/query_builder.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ See the :meth:`pysnow.Request.get_all` and :meth:`pysnow.QueryBuilder` documenta
end = dt.now() - td(days=20)
# Query incident records with number starting with 'INC0123', created between 1970-01-01 and 20 days back in time
qb = pysnow.QueryBuilder()\
.field('number').starts_with('INC0123')\
.AND()\
.field('sys_created_on').between(start, end)
qb = (
pysnow.QueryBuilder()
.field('number').starts_with('INC0123')
.AND()
.field('sys_created_on').between(start, end)
)
r = s.query('incident', query=qb)
Expand Down
31 changes: 23 additions & 8 deletions docs/usage/query.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Simple. And sufficient in many cases.
Using the query builder
^^^^^^^^^^^^^^^^^^^^^^^
Perhaps a bit verbose, but pretty simple and powerful.

The recommended way to create complex queries.

See the :meth:`pysnow.QueryBuilder` documentation for more details.

Expand All @@ -30,18 +31,32 @@ See the :meth:`pysnow.QueryBuilder` documentation for more details.
end = dt.now() - td(days=20)
# Query incident records with number starting with 'INC0123', created between 1970-01-01 and 20 days back in time
qb = pysnow.QueryBuilder()\
.field('number').starts_with('INC0123')\
.AND()\
.field('sys_created_on').between(start, end)
qb = (
pysnow.QueryBuilder()
.field('number').starts_with('INC0123')
.AND()
.field('sys_created_on').between(start, end)
)
r = s.query('incident', query=qb)
SN Pass-through
^^^^^^^^^^^^^^^
It's a one-liner. Quite obscure. But hey, it's a one-liner.

It's recommended to use the query builder for complex queries, as it offers a cleaner way to create queries.

However, you can still use SN pass-through queries should the query builder not satisfy your needs for some reason.

This is a pass-through equivalent of the QB example above.

.. code-block:: python
# Query incident records starting with 'INC012' or short_description containing 'test'
r = s.query(table='incident', query='numberSTARTSWITHINC012^ORshort_descriptionLIKEtest')
from datetime import datetime as dt
from datetime import timedelta as td
# Set start and end range
start = dt(1970, 1, 1)
end = dt.now() - td(days=20)
# Query incident records with number starting with 'INC0123', created between 1970-01-01 and 20 days back in time
r = s.query(table='incident', query='numberSTARTSWITHINC0150^sys_created_onBETWEENjavascript:gs.dateGenerate("%s")@javascript:gs.dateGenerate("%s")' % (start, end))
2 changes: 1 addition & 1 deletion pysnow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
from pysnow.exceptions import *

__author__ = "Robert Wikman <[email protected]>"
__version__ = "0.4.2"
__version__ = "0.4.3"
165 changes: 54 additions & 111 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,56 @@

class TestIncident(unittest.TestCase):
def test_query_no_expression(self):
try:
q = str(pysnow.QueryBuilder().field('test'))
self.assertFalse(q)
except pysnow.QueryEmpty:
pass
q = pysnow.QueryBuilder().field('test')
self.assertRaises(pysnow.QueryEmpty, q.__str__)

def test_query_no_query(self):
try:
q = str(pysnow.QueryBuilder())
self.assertFalse(q)
except pysnow.QueryEmpty:
pass
q = pysnow.QueryBuilder()
self.assertRaises(pysnow.QueryEmpty, q.__str__)

def test_query_unexpected_logical(self):
try:
q = str(pysnow.QueryBuilder().AND())
self.assertFalse(q)
except pysnow.QueryExpressionError:
pass
q = pysnow.QueryBuilder()
self.assertRaises(pysnow.QueryExpressionError, q.AND)

def test_query_expression_no_field(self):
try:
q = str(pysnow.QueryBuilder().equals('test'))
self.assertFalse(q)
except pysnow.QueryMissingField:
pass
q = pysnow.QueryBuilder()
self.assertRaises(pysnow.QueryMissingField, q.equals, 'test')

def test_query_no_field_expression(self):
try:
q = str(pysnow.QueryBuilder().field('test').equals('test').AND().field('test'))
self.assertFalse(q)
except pysnow.QueryExpressionError:
pass
q = pysnow.QueryBuilder().field('test').equals('test').AND().field('test')
self.assertRaises(pysnow.QueryExpressionError, q.__str__)

def test_query_field_multiple_expressions(self):
try:
q = str(pysnow.QueryBuilder().field('test').equals('test').between(1, 2))
self.assertFalse(q)
except pysnow.QueryMultipleExpressions:
pass
q = pysnow.QueryBuilder().field('test').equals('test')
self.assertRaises(pysnow.QueryMultipleExpressions, q.between, 1, 2)

def test_query_unfinished_logical(self):
try:
q = str(pysnow.QueryBuilder().field('test').equals('test').AND())
self.assertFalse(q)
except pysnow.QueryMissingField:
pass
q = pysnow.QueryBuilder().field('test').equals('test').AND()
self.assertRaises(pysnow.QueryMissingField, q.__str__)

def test_query_logical_and(self):
# Make sure AND() operator between expressions works
q = pysnow.QueryBuilder().field('test').equals('test').AND().field('test2').equals('test')

self.assertEquals(str(q), 'test=test^test2=test')

def test_query_logical_or(self):
# Make sure OR() operator between expressions works
q = pysnow.QueryBuilder().field('test').equals('test').OR().field('test2').equals('test')

self.assertEquals(str(q), 'test=test^ORtest2=test')

def test_query_logical_nq(self):
# Make sure NQ() operator between expressions works
q = pysnow.QueryBuilder().field('test').equals('test').NQ().field('test2').equals('test')

self.assertEquals(str(q), 'test=test^NQtest2=test')

def test_query_cond_between(self):
# Make sure between with str arguments fails
try:
q = pysnow.QueryBuilder().field('test').between('test', 'test')
self.assertFalse(q)
except pysnow.QueryTypeError:
pass
q1 = pysnow.QueryBuilder().field('test')
self.assertRaises(pysnow.QueryTypeError, q1.between, 'test', 'test')

# Make sure between with int arguments works
q1 = str(pysnow.QueryBuilder().field('test').between(1, 2))
self.assertEquals(str(q1), 'testBETWEEN1@2')
q2 = str(pysnow.QueryBuilder().field('test').between(1, 2))
self.assertEquals(str(q2), 'testBETWEEN1@2')

start = dt(1970, 1, 1)
end = dt(1970, 1, 2)
Expand All @@ -94,55 +67,39 @@ def test_query_cond_between(self):

def test_query_cond_starts_with(self):
# Make sure type checking works
try:
q = pysnow.QueryBuilder().field('test').starts_with(1)
self.assertFalse(q)
except pysnow.QueryTypeError:
pass
q1 = pysnow.QueryBuilder().field('test')
self.assertRaises(pysnow.QueryTypeError, q1.starts_with, 1)

# Make sure a valid operation works
q = pysnow.QueryBuilder().field('test').starts_with('val')

self.assertEquals(str(q), 'testSTARTSWITHval')
q2 = pysnow.QueryBuilder().field('test').starts_with('val')
self.assertEquals(str(q2), 'testSTARTSWITHval')

def test_query_cond_ends_with(self):
# Make sure type checking works
try:
q = pysnow.QueryBuilder().field('test').ends_with(1)
self.assertFalse(q)
except pysnow.QueryTypeError:
pass
q1 = pysnow.QueryBuilder().field('test')
self.assertRaises(pysnow.QueryTypeError, q1.ends_with, 1)

# Make sure a valid operation works
q = pysnow.QueryBuilder().field('test').ends_with('val')

self.assertEquals(str(q), 'testENDSWITHval')
q2 = pysnow.QueryBuilder().field('test').ends_with('val')
self.assertEquals(str(q2), 'testENDSWITHval')

def test_query_cond_contains(self):
# Make sure type checking works
try:
q = pysnow.QueryBuilder().field('test').contains(1)
self.assertFalse(q)
except pysnow.QueryTypeError:
pass
q1 = pysnow.QueryBuilder().field('test')
self.assertRaises(pysnow.QueryTypeError, q1.contains, 1)

# Make sure a valid operation works
q = pysnow.QueryBuilder().field('test').contains('val')

self.assertEquals(str(q), 'testLIKEval')
q2 = pysnow.QueryBuilder().field('test').contains('val')
self.assertEquals(str(q2), 'testLIKEval')

def test_query_cond_not_contains(self):
# Make sure type checking works
try:
q = pysnow.QueryBuilder().field('test').not_contains(1)
self.assertFalse(q)
except pysnow.QueryTypeError:
pass
q1 = pysnow.QueryBuilder().field('test')
self.assertRaises(pysnow.QueryTypeError, q1.not_contains, 1)

# Make sure a valid operation works
q = pysnow.QueryBuilder().field('test').not_contains('val')

self.assertEquals(str(q), 'testNOTLIKEval')
q2 = pysnow.QueryBuilder().field('test').not_contains('val')
self.assertEquals(str(q2), 'testNOTLIKEval')

def test_query_cond_is_empty(self):
# Make sure a valid operation works
Expand All @@ -152,59 +109,45 @@ def test_query_cond_is_empty(self):

def test_query_cond_equals(self):
# Make sure type checking works
try:
q = pysnow.QueryBuilder().field('test').equals(dt)
self.assertFalse(q)
except pysnow.QueryTypeError:
pass
q1 = pysnow.QueryBuilder().field('test')
self.assertRaises(pysnow.QueryTypeError, q1.equals, dt)

# Make sure a valid operation works
q = pysnow.QueryBuilder().field('test').equals('test')

self.assertEquals(str(q), 'test=test')
q2 = pysnow.QueryBuilder().field('test').equals('test')
self.assertEquals(str(q2), 'test=test')

def test_query_cond_not_equals(self):
# Make sure type checking works
try:
q = pysnow.QueryBuilder().field('test').not_equals(dt)
self.assertFalse(q)
except pysnow.QueryTypeError:
pass
q1 = pysnow.QueryBuilder().field('test')
self.assertRaises(pysnow.QueryTypeError, q1.not_equals, dt)

# Make sure a valid operation works
q = pysnow.QueryBuilder().field('test').not_equals('test')

self.assertEquals(str(q), 'test!=test')
q2 = pysnow.QueryBuilder().field('test').not_equals('test')
self.assertEquals(str(q2), 'test!=test')

def test_query_cond_greater_than(self):
# Make sure type checking works
try:
q = pysnow.QueryBuilder().field('test').greater_than('a')
self.assertFalse(q)
except pysnow.QueryTypeError:
pass
q1 = pysnow.QueryBuilder().field('test')
self.assertRaises(pysnow.QueryTypeError, q1.greater_than, 'a')

# Make sure a valid operation works
q = pysnow.QueryBuilder().field('test').greater_than(1)
self.assertEquals(str(q), 'test>1')
q2 = pysnow.QueryBuilder().field('test').greater_than(1)
self.assertEquals(str(q2), 'test>1')

q = pysnow.QueryBuilder().field('test').greater_than(dt(2016, 2, 1))
self.assertEquals(str(q), 'test>2016-02-01 00:00:00')
q3 = pysnow.QueryBuilder().field('test').greater_than(dt(2016, 2, 1))
self.assertEquals(str(q3), 'test>2016-02-01 00:00:00')

def test_query_cond_less_than(self):
# Make sure type checking works
try:
q = pysnow.QueryBuilder().field('test').less_than('a')
self.assertFalse(q)
except pysnow.QueryTypeError:
pass
q1 = pysnow.QueryBuilder().field('test')
self.assertRaises(pysnow.QueryTypeError, q1.less_than, 'a')

# Make sure a valid operation works
q = pysnow.QueryBuilder().field('test').less_than(1)
self.assertEquals(str(q), 'test<1')
q2 = pysnow.QueryBuilder().field('test').less_than(1)
self.assertEquals(str(q2), 'test<1')

q = pysnow.QueryBuilder().field('test').less_than(dt(2016, 2, 1))
self.assertEquals(str(q), 'test<2016-02-01 00:00:00')
q3 = pysnow.QueryBuilder().field('test').less_than(dt(2016, 2, 1))
self.assertEquals(str(q3), 'test<2016-02-01 00:00:00')

def test_complex_query(self):
start = dt(2016, 2, 1)
Expand Down

0 comments on commit 18a73f9

Please sign in to comment.