def test_query_filter_gte():
    table = create_table_with_local_indexes()
    item_data = [
        {
            'forum_name': 'Cool Forum',
            'subject': 'Check this out!',
            'version': '1',
            'threads': 1,
        },
        {
            'forum_name': 'Cool Forum',
            'subject': 'Read this now!',
            'version': '1',
            'threads': 5,
        },
        {
            'forum_name': 'Cool Forum',
            'subject': 'Please read this... please',
            'version': '1',
            'threads': 0,
        }
    ]
    for data in item_data:
        item = Item(table, data)
        item.save(overwrite=True)

    results = table.query(
        forum_name__eq='Cool Forum', index='threads_index', threads__gte=1
    )
    list(results).should.have.length_of(2)
def test_batch_read():
    table = create_table()

    item_data = {
        'Body': 'http://url_to_lolcat.gif',
        'SentBy': 'User A',
        'ReceivedTime': '12/9/2011 11:36:03 PM',
    }
    item_data['forum_name'] = 'the-key1'
    item = Item(table,item_data)     
    item.save()  

    item = Item(table,item_data)
    item_data['forum_name'] = 'the-key2'
    item.save(overwrite = True)

    item_data = {
        'Body': 'http://url_to_lolcat.gif',
        'SentBy': 'User B',
        'ReceivedTime': '12/9/2011 11:36:03 PM',
        'Ids': set([1, 2, 3]),
        'PK': 7,
    }
    item = Item(table,item_data) 
    item_data['forum_name'] = 'another-key'
    item.save(overwrite = True)
    
    results = table.batch_get(keys=[
                {'forum_name': 'the-key1'},
                {'forum_name': 'another-key'}])

    # Iterate through so that batch_item gets called
    count = len([x for x in results])
    count.should.equal(2)
def test_query():
    table = create_table()

    item_data = {
        'forum_name': 'LOLCat Forum',
        'Body': 'http://url_to_lolcat.gif',
        'SentBy': 'User A',
        'ReceivedTime': '12/9/2011 11:36:03 PM',
        'subject': 'Check this out!'
    }
    item = Item(table, item_data)
    item.save(overwrite=True)

    item['forum_name'] = 'the-key'
    item['subject'] = '456'
    item.save(overwrite=True)

    item['forum_name'] = 'the-key'
    item['subject'] = '123'
    item.save(overwrite=True)

    item['forum_name'] = 'the-key'
    item['subject'] = '789'
    item.save(overwrite=True)

    table.count().should.equal(4)

    results = table.query_2(forum_name__eq='the-key',
                            subject__gt='1', consistent=True)
    expected = ["123", "456", "789"]
    for index, item in enumerate(results):
        item["subject"].should.equal(expected[index])

    results = table.query_2(forum_name__eq="the-key",
                            subject__gt='1', reverse=True)
    for index, item in enumerate(results):
        item["subject"].should.equal(expected[len(expected) - 1 - index])

    results = table.query_2(forum_name__eq='the-key',
                            subject__gt='1', consistent=True)
    sum(1 for _ in results).should.equal(3)

    results = table.query_2(forum_name__eq='the-key',
                            subject__gt='234', consistent=True)
    sum(1 for _ in results).should.equal(2)

    results = table.query_2(forum_name__eq='the-key', subject__gt='9999')
    sum(1 for _ in results).should.equal(0)

    results = table.query_2(forum_name__eq='the-key', subject__beginswith='12')
    sum(1 for _ in results).should.equal(1)

    results = table.query_2(forum_name__eq='the-key', subject__beginswith='7')
    sum(1 for _ in results).should.equal(1)

    results = table.query_2(forum_name__eq='the-key',
                            subject__between=['567', '890'])
    sum(1 for _ in results).should.equal(1)
def test_conflicting_writes():
    table = Table.create('messages', schema=[
        HashKey('id'),
    ])

    item_data = {'id': '123', 'data': '678'}
    item1 = Item(table, item_data)
    item2 = Item(table, item_data)
    item1.save()

    item1['data'] = '579'
    item2['data'] = '912'

    item1.save()
    item2.save.when.called_with().should.throw(ConditionalCheckFailedException)
def test_query_with_local_indexes():
    table = create_table_with_local_indexes()
    item_data = {
        'forum_name': 'Cool Forum',
        'subject': 'Check this out!',
        'version': '1',
        'threads': 1,
        'status': 'inactive'
    }
    item = Item(table, item_data)
    item.save(overwrite=True)

    item['version'] = '2'
    item.save(overwrite=True)
    results = table.query(forum_name__eq='Cool Forum', index='threads_index', threads__eq=1)
    list(results).should.have.length_of(1)
def test_query():
    table = create_table()

    item_data = {
        'forum_name': 'the-key',
        'Body': 'http://url_to_lolcat.gif',
        'SentBy': 'User A',
        'ReceivedTime': '12/9/2011 11:36:03 PM',
    }
    item = Item(table, item_data)
    item.save(overwrite=True)
    table.count().should.equal(1)
    table = Table("messages")

    results = table.query(forum_name__eq='the-key')
    sum(1 for _ in results).should.equal(1)
def test_delete_item():
    table = create_table()
    item_data = {
        'forum_name': 'LOLCat Forum',
        'Body': 'http://url_to_lolcat.gif',
        'SentBy': 'User A',
        'ReceivedTime': '12/9/2011 11:36:03 PM',
    }
    item =Item(table,item_data)
    item['subject'] = 'Check this out!'        
    item.save()
    table.count().should.equal(1)

    response = item.delete()
    response.should.equal(True)
    
    table.count().should.equal(0)
    item.delete.when.called_with().should.throw(ConditionalCheckFailedException)
def test_delete_item():
    table = create_table()

    item_data = {
        'forum_name': 'LOLCat Forum',
        'Body': 'http://url_to_lolcat.gif',
        'SentBy': 'User A',
        'ReceivedTime': '12/9/2011 11:36:03 PM',
    }
    item =Item(table,item_data)
    item.save()
    table.count().should.equal(1)

    response = item.delete()
    
    response.should.equal(True)

    table.count().should.equal(0)

    item.delete.when.called_with().should.throw(JSONResponseError)
def test_query_with_global_indexes():
    table = Table.create('messages', schema=[
        HashKey('subject'),
        RangeKey('version'),
    ], global_indexes=[
        GlobalAllIndex('topic-created_at-index',
            parts=[
                HashKey('topic'),
                RangeKey('created_at', data_type='N')
            ],
            throughput={
                'read': 6,
                'write': 1
            }
        ),
        GlobalAllIndex('status-created_at-index',
            parts=[
                HashKey('status'),
                RangeKey('created_at', data_type='N')
            ],
            throughput={
                'read': 2,
                'write': 1
            }
        )
    ])

    item_data = {
        'subject': 'Check this out!',
        'version': '1',
        'created_at': 0,
        'status': 'inactive'
    }
    item = Item(table, item_data)
    item.save(overwrite=True)

    item['version'] = '2'
    item.save(overwrite=True)

    results = table.query(status__eq='active')
    list(results).should.have.length_of(0)
def test_scan():
    table = create_table()
    item_data = {
        'Body': 'http://url_to_lolcat.gif',
        'SentBy': 'User A',
        'ReceivedTime': '12/9/2011 11:36:03 PM',
    }
    item_data['forum_name'] = 'the-key'
    item_data['subject'] = '456'

    item = Item(table, item_data)
    item.save()

    item['forum_name'] = 'the-key'
    item['subject'] = '123'
    item.save()

    item_data = {
        'Body': 'http://url_to_lolcat.gif',
        'SentBy': 'User B',
        'ReceivedTime': '12/9/2011 11:36:09 PM',
        'Ids': set([1, 2, 3]),
        'PK': 7,
    }

    item_data['forum_name'] = 'the-key'
    item_data['subject'] = '789'

    item = Item(table, item_data)
    item.save()

    results = table.scan()
    sum(1 for _ in results).should.equal(3)

    results = table.scan(SentBy__eq='User B')
    sum(1 for _ in results).should.equal(1)

    results = table.scan(Body__beginswith='http')
    sum(1 for _ in results).should.equal(3)

    results = table.scan(Ids__null=False)
    sum(1 for _ in results).should.equal(1)

    results = table.scan(Ids__null=True)
    sum(1 for _ in results).should.equal(2)

    results = table.scan(PK__between=[8, 9])
    sum(1 for _ in results).should.equal(0)

    results = table.scan(PK__between=[5, 8])
    sum(1 for _ in results).should.equal(1)
def test_query_non_hash_range_key():
    table = create_table_with_local_indexes()
    item_data = [
        {
            'forum_name': 'Cool Forum',
            'subject': 'Check this out!',
            'version': '1',
            'threads': 1,
        },
        {
            'forum_name': 'Cool Forum',
            'subject': 'Read this now!',
            'version': '3',
            'threads': 5,
        },
        {
            'forum_name': 'Cool Forum',
            'subject': 'Please read this... please',
            'version': '2',
            'threads': 0,
        }
    ]
    for data in item_data:
        item = Item(table, data)
        item.save(overwrite=True)

    results = table.query(
        forum_name__eq='Cool Forum', version__gt="2"
    )
    results = list(results)
    results.should.have.length_of(1)

    results = table.query(
        forum_name__eq='Cool Forum', version__lt="3"
    )
    results = list(results)
    results.should.have.length_of(2)
Example #12
0
def test_conflicting_writes():
    table = Table.create('messages', schema=[
        HashKey('id'),
    ])

    item_data = {'id': '123', 'data': '678'}
    item1 = Item(table, item_data)
    item2 = Item(table, item_data)
    item1.save()

    item1['data'] = '579'
    item2['data'] = '912'

    item1.save()
    item2.save.when.called_with().should.throw(ConditionalCheckFailedException)
Example #13
0
def get_share(share_to_score, user_to_score):
  try:
    share = table.get_item(share_id = share_to_score)
    print('Fetched share from db. Share id is: {}'.format(share_to_score))
  except ItemNotFound:
    share = Item(table, data={
     'share_id': share_to_score,
     'username': user_to_score,
     'points': 0,
     'voters': [],
     'upvote_count': 0,
     'downvote_count': 0,
     'last_vote': '',
     'first_upvote': True,
     'first_downvote': True
    })
    print('Created new share object. Share id is: {}'.format(share_to_score))

  return share
Example #14
0
def test_delete_item():
    table = create_table()
    item_data = {
        'forum_name': 'LOLCat Forum',
        'Body': 'http://url_to_lolcat.gif',
        'SentBy': 'User A',
        'ReceivedTime': '12/9/2011 11:36:03 PM',
    }
    item = Item(table, item_data)
    item['subject'] = 'Check this out!'
    item.save()
    table.count().should.equal(1)

    response = item.delete()
    response.should.equal(True)

    table.count().should.equal(0)
    item.delete().should.equal(False)
Example #15
0
def test_query_with_local_indexes():
    table = create_table_with_local_indexes()
    item_data = {
        'forum_name': 'Cool Forum',
        'subject': 'Check this out!',
        'version': '1',
        'threads': 1,
        'status': 'inactive'
    }
    item = Item(table, item_data)
    item.save(overwrite=True)

    item['version'] = '2'
    item.save(overwrite=True)
    results = table.query(forum_name__eq='Cool Forum', index='threads_index', threads__eq=1)
    list(results).should.have.length_of(1)
def test_delete_item():
    table = create_table()

    item_data = {
        "forum_name": "LOLCat Forum",
        "Body": "http://url_to_lolcat.gif",
        "SentBy": "User A",
        "ReceivedTime": "12/9/2011 11:36:03 PM",
    }
    item = Item(table, item_data)
    item.save()
    table.count().should.equal(1)

    response = item.delete()

    response.should.equal(True)

    table.count().should.equal(0)

    # Deletes are idempotent and 'False' here would imply an error condition
    item.delete().should.equal(True)
def test_delete_item():
    table = create_table()

    item_data = {
        'forum_name': 'LOLCat Forum',
        'Body': 'http://url_to_lolcat.gif',
        'SentBy': 'User A',
        'ReceivedTime': '12/9/2011 11:36:03 PM',
    }
    item = Item(table, item_data)
    item.save()
    table.count().should.equal(1)

    response = item.delete()

    response.should.equal(True)

    table.count().should.equal(0)

    # Deletes are idempotent and 'False' here would imply an error condition
    item.delete().should.equal(True)
Example #18
0
def test_query_with_global_indexes():
    table = Table.create('messages',
                         schema=[
                             HashKey('subject'),
                             RangeKey('version'),
                         ],
                         global_indexes=[
                             GlobalAllIndex('topic-created_at-index',
                                            parts=[
                                                HashKey('topic'),
                                                RangeKey('created_at',
                                                         data_type='N')
                                            ],
                                            throughput={
                                                'read': 6,
                                                'write': 1
                                            }),
                             GlobalAllIndex('status-created_at-index',
                                            parts=[
                                                HashKey('status'),
                                                RangeKey('created_at',
                                                         data_type='N')
                                            ],
                                            throughput={
                                                'read': 2,
                                                'write': 1
                                            })
                         ])

    item_data = {
        'subject': 'Check this out!',
        'version': '1',
        'created_at': 0,
        'status': 'inactive'
    }
    item = Item(table, item_data)
    item.save(overwrite=True)

    item['version'] = '2'
    item.save(overwrite=True)

    results = table.query(status__eq='active')
    list(results).should.have.length_of(0)
def test_delete_item():
    table = create_table()
    item_data = {
        'forum_name': 'LOLCat Forum',
        'Body': 'http://url_to_lolcat.gif',
        'SentBy': 'User A',
        'ReceivedTime': '12/9/2011 11:36:03 PM',
    }
    item = Item(table, item_data)
    item['subject'] = 'Check this out!'
    item.save()
    table.count().should.equal(1)

    response = item.delete()
    response.should.equal(True)

    table.count().should.equal(0)
    # Deletes are idempotent
    item.delete().should.equal(True)
def test_delete_item():
    table = create_table()

    item_data = {
        'forum_name': 'LOLCat Forum',
        'Body': 'http://url_to_lolcat.gif',
        'SentBy': 'User A',
        'ReceivedTime': '12/9/2011 11:36:03 PM',
    }
    item = Item(table, item_data)
    item.save()
    table.count().should.equal(1)

    response = item.delete()

    response.should.equal(True)

    table.count().should.equal(0)

    # Deletes are idempotent and 'False' here would imply an error condition
    item.delete().should.equal(True)
Example #21
0
from boto.dynamodb2.table import Table
from boto.dynamodb2.table import Item
import time
reading = Table('sump')
#print reading

polltime = long(time.time())
level = 30

new_entry = Item(reading, data={
    'Id': 'sump',
    'Date': polltime,
    'Level': level,
})

new_entry.save()
True
Example #22
0
from boto.dynamodb2.table import Table
from boto.dynamodb2.table import Item
import time

reading = Table("sump")
# print reading

polltime = long(time.time())
level = 30

new_entry = Item(reading, data={"Id": "sump", "Date": polltime, "Level": level})

new_entry.save()
True
Example #23
0
 def new_client(self, data):
     item = Item(self, data=data)
     return item
Example #24
0
 def profile_new(self, stats):
     clean_stats = self.profile_clean(stats)
     clean_stats[time_keys.ts_add] = int(time.time())
     item = Item(self, data=clean_stats)
     item.save()
     return item
Example #25
0
 def missing(self, t):
     item = Item(self, data=t)
     item.save()
Example #26
0
def test_batch_read():
    table = create_table()

    item_data = {
        'Body': 'http://url_to_lolcat.gif',
        'SentBy': 'User A',
        'ReceivedTime': '12/9/2011 11:36:03 PM',
    }
    item_data['forum_name'] = 'the-key1'
    item = Item(table, item_data)
    item.save()

    item = Item(table, item_data)
    item_data['forum_name'] = 'the-key2'
    item.save(overwrite=True)

    item_data = {
        'Body': 'http://url_to_lolcat.gif',
        'SentBy': 'User B',
        'ReceivedTime': '12/9/2011 11:36:03 PM',
        'Ids': set([1, 2, 3]),
        'PK': 7,
    }
    item = Item(table, item_data)
    item_data['forum_name'] = 'another-key'
    item.save(overwrite=True)

    results = table.batch_get(keys=[
        {
            'forum_name': 'the-key1'
        },
        {
            'forum_name': 'another-key'
        },
    ])

    # Iterate through so that batch_item gets called
    count = len([x for x in results])
    count.should.equal(2)
    sys.exit(1)

# Figure out the time the recording was taken.
# Need to figure out how to accomodate for DST
# offset = 60*60*6 # we'll subtract 6 hours worth of seconds to adjust timezone from GMT
polltime = long(time.time())

# Write data to Amazon's dynamodb for cloud storage
from boto.dynamodb2.table import Table
from boto.dynamodb2.table import Item

reading = Table('sump')
new_entry = Item(reading,
                 data={
                     'Id': 'sump',
                     'Date': polltime,
                     'Level': depth,
                     'Pressure': pressure,
                 })
new_entry.save()

# Push SMS alert to phone if sump reaches critical level
# Recipient ph # is managed in the AWS SNS admin
# TO-DO:
# Check dynamodb to see how many messages sent in last 60 mins.
# If 3, mention in message that it will be the last one for x mins.
# if 4, do not send
# put url to chart in msg
if notificationkillswitch == 0:
    if depth > alertthreshold:
        sns = boto.connect_sns()
#!/usr/bin/env python
#
# Sends notification via AWS SNS (Amazon's messaging service) every time the raspberry pi starts up

from boto.dynamodb2.table import Table
from boto.dynamodb2.table import Item
import time, boto

sns = boto.connect_sns()
now = long(time.time())
startuptime = time.strftime('%H:%M:%S', time.localtime(now))
arn = "arn:aws:sns:us-east-1:361127226605:SumpPumpAlerts"
msg = "Telemetry Pi successful startup at " + startuptime
subj = "Telemetry Pi booted at  " + startuptime
sns.publish(arn, msg, subj)

# Store SMS message to DynamoDB for logging purposes:
reading = Table('sns_alerts')
new_entry = Item(reading,
                 data={
                     'type': 'bootup',
                     'date': now,
                     'subject': subj,
                     'message': msg,
                 })
new_entry.save()
True
print subj
Example #29
0
 def create_item(self, data):
     return Item(self.table, data=data)
def test_batch_read():
    table = create_table()

    item_data = {
        "Body": "http://url_to_lolcat.gif",
        "SentBy": "User A",
        "ReceivedTime": "12/9/2011 11:36:03 PM",
    }
    item_data["forum_name"] = "the-key1"
    item = Item(table, item_data)
    item.save()

    item = Item(table, item_data)
    item_data["forum_name"] = "the-key2"
    item.save(overwrite=True)

    item_data = {
        "Body": "http://url_to_lolcat.gif",
        "SentBy": "User B",
        "ReceivedTime": "12/9/2011 11:36:03 PM",
        "Ids": set([1, 2, 3]),
        "PK": 7,
    }
    item = Item(table, item_data)
    item_data["forum_name"] = "another-key"
    item.save(overwrite=True)

    results = table.batch_get(keys=[{
        "forum_name": "the-key1"
    }, {
        "forum_name": "another-key"
    }])

    # Iterate through so that batch_item gets called
    count = len([x for x in results])
    count.should.equal(2)
Example #31
0
def test_query():

    table = create_table()

    item_data = {
        'forum_name': 'LOLCat Forum',
        'Body': 'http://url_to_lolcat.gif',
        'SentBy': 'User A',
        'ReceivedTime': '12/9/2011 11:36:03 PM',
        'subject': 'Check this out!'
    }
    item = Item(table, item_data)
    item.save(overwrite=True)

    item['forum_name'] = 'the-key'
    item['subject'] = '456'
    item.save(overwrite=True)

    item['forum_name'] = 'the-key'
    item['subject'] = '123'
    item.save(overwrite=True)

    item['forum_name'] = 'the-key'
    item['subject'] = '789'
    item.save(overwrite=True)

    table.count().should.equal(4)

    results = table.query(forum_name__eq='the-key',
                          subject__gt='1',
                          consistent=True)
    expected = ["123", "456", "789"]
    for index, item in enumerate(results):
        item["subject"].should.equal(expected[index])

    results = table.query(forum_name__eq="the-key",
                          subject__gt='1',
                          reverse=True)
    for index, item in enumerate(results):
        item["subject"].should.equal(expected[len(expected) - 1 - index])

    results = table.query(forum_name__eq='the-key',
                          subject__gt='1',
                          consistent=True)
    sum(1 for _ in results).should.equal(3)

    results = table.query(forum_name__eq='the-key',
                          subject__gt='234',
                          consistent=True)
    sum(1 for _ in results).should.equal(2)

    results = table.query(forum_name__eq='the-key', subject__gt='9999')
    sum(1 for _ in results).should.equal(0)

    results = table.query(forum_name__eq='the-key', subject__beginswith='12')
    sum(1 for _ in results).should.equal(1)

    results = table.query(forum_name__eq='the-key', subject__beginswith='7')
    sum(1 for _ in results).should.equal(1)

    results = table.query(forum_name__eq='the-key',
                          subject__between=['567', '890'])
    sum(1 for _ in results).should.equal(1)
Example #32
0

def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))


conn = boto.dynamodb2.connect_to_region(
    'eu-west-1'
)

users = Table('CMR', connection=conn)

data = users.get_item(node='ccm1',date='2014-01-02')

print data
print data['date'],data['a_number'],data['duration']

data['duration']=data['duration']+random.randint(1,5)

data.save()

node=id_generator()

dato2 = Item(users, data= {
    'node': node,
    'date': '2015-01-01',
    'duration': 3
})

dato2.save()