Ejemplo n.º 1
0
    def test_list_tables(self):
        """
        Connection.list_tables
        """
        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), LIST_TABLE_DATA
            conn = Connection(self.region)
            conn.list_tables(exclusive_start_table_name='Thread')
            self.assertEqual(req.call_args[1], {'exclusive_start_table_name': 'Thread'})

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), LIST_TABLE_DATA
            conn = Connection(self.region)
            conn.list_tables(limit=3)
            self.assertEqual(req.call_args[1], {'limit': 3})

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), LIST_TABLE_DATA
            conn = Connection(self.region)
            conn.list_tables()
            self.assertEqual(req.call_args[1], {})

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpBadRequest(), None
            conn = Connection(self.region)
            self.assertRaises(TableError, conn.list_tables)
Ejemplo n.º 2
0
"""
Examples using a connection
"""
from pynamodb.connection import Connection

# Get a connection
conn = Connection(host='http://localhost:8000')
print(conn)

# List tables
print(conn.list_tables())

# Describe a table
print(conn.describe_table('Thread'))

# Get an item
print(conn.get_item('Thread', 'hash-key', 'range-key'))

# Put an item
conn.put_item('Thread',
              'hash-key',
              'range-key',
              attributes={
                  'forum_name': 'value',
                  'subject': 'value'
              })

# Delete an item
conn.delete_item('Thread', 'hash-key', 'range-key')
Ejemplo n.º 3
0
def test_connection_integration(ddb_url):
    table_name = 'pynamodb-ci-connection'

    # For use with a fake dynamodb connection
    # See: http://aws.amazon.com/dynamodb/developer-resources/
    conn = Connection(host=ddb_url)

    print(conn)
    print("conn.describe_table...")
    table = None
    try:
        table = conn.describe_table(table_name)
    except TableDoesNotExist:
        params = {
            'read_capacity_units':
            1,
            'write_capacity_units':
            1,
            'attribute_definitions': [{
                'attribute_type': STRING,
                'attribute_name': 'Forum'
            }, {
                'attribute_type': STRING,
                'attribute_name': 'Thread'
            }, {
                'attribute_type': STRING,
                'attribute_name': 'AltKey'
            }, {
                'attribute_type': NUMBER,
                'attribute_name': 'number'
            }],
            'key_schema': [{
                'key_type': HASH,
                'attribute_name': 'Forum'
            }, {
                'key_type': RANGE,
                'attribute_name': 'Thread'
            }],
            'global_secondary_indexes': [{
                'index_name':
                'alt-index',
                'key_schema': [{
                    'KeyType': 'HASH',
                    'AttributeName': 'AltKey'
                }],
                'projection': {
                    'ProjectionType': 'KEYS_ONLY'
                },
                'provisioned_throughput': {
                    'ReadCapacityUnits': 1,
                    'WriteCapacityUnits': 1,
                }
            }],
            'local_secondary_indexes': [{
                'index_name':
                'view-index',
                'key_schema': [{
                    'KeyType': 'HASH',
                    'AttributeName': 'Forum'
                }, {
                    'KeyType': 'RANGE',
                    'AttributeName': 'AltKey'
                }],
                'projection': {
                    'ProjectionType': 'KEYS_ONLY'
                }
            }]
        }
        print("conn.create_table...")
        conn.create_table(table_name, **params)

    while table is None:
        time.sleep(1)
        table = conn.describe_table(table_name)

    while table['TableStatus'] == 'CREATING':
        time.sleep(2)
        table = conn.describe_table(table_name)
    print("conn.list_tables")
    conn.list_tables()
    print("conn.update_table...")

    conn.update_table(table_name,
                      read_capacity_units=table.get(
                          PROVISIONED_THROUGHPUT).get(READ_CAPACITY_UNITS) + 1,
                      write_capacity_units=2)

    table = conn.describe_table(table_name)

    while table['TableStatus'] != 'ACTIVE':
        time.sleep(2)
        table = conn.describe_table(table_name)

    print("conn.put_item")
    conn.put_item(
        table_name,
        'item1-hash',
        range_key='item1-range',
        attributes={'foo': {
            'S': 'bar'
        }},
        condition=NotExists(Path('Forum')),
    )
    conn.get_item(table_name, 'item1-hash', range_key='item1-range')
    conn.delete_item(table_name, 'item1-hash', range_key='item1-range')

    items = []
    for i in range(10):
        items.append({"Forum": "FooForum", "Thread": f"thread-{i}"})
    print("conn.batch_write_items...")
    conn.batch_write_item(table_name, put_items=items)
    print("conn.batch_get_items...")
    data = conn.batch_get_item(table_name, items)
    print("conn.query...")
    conn.query(
        table_name,
        "FooForum",
        range_key_condition=(BeginsWith(Path('Thread'), Value('thread'))),
    )
    print("conn.scan...")
    conn.scan(table_name, )
    print("conn.delete_table...")
    conn.delete_table(table_name)
Ejemplo n.º 4
0
def list_table():
    from pynamodb.connection import Connection
    conn = Connection(host=DB_HOST, region=REGION)
    tables = conn.list_tables()
    print(tables)
    return tables
Ejemplo n.º 5
0
"""
Examples using a connection
"""
from __future__ import print_function
from pynamodb.connection import Connection

# Get a connection
conn = Connection(host='http://localhost:8000')
print(conn)

# List tables
print(conn.list_tables())

# Describe a table
print(conn.describe_table('Thread'))

# Get an item
print(conn.get_item('Thread', 'hash-key', 'range-key'))

# Put an item
conn.put_item('Thread', 'hash-key', 'range-key', attributes={'forum_name': 'value', 'subject': 'value'})

# Delete an item
conn.delete_item('Thread', 'hash-key', 'range-key')
# Creating a connection is simple
from pynamodb.connection import Connection

conn = Connection()

# You can specify a different DynamoDB url
conn = Connection(host='http://alternative-domain/')

# By default, PynamoDB will connect to the us-east-1 region, but you can specify a different one.
conn = Connection(region='us-west-1')

# Modifying tables

# You can easily list tables:
conn.list_tables()

# or delete a table:

# conn.delete_table('Thread')

# If you want to change the capacity of a table, that can be done as well:

conn.update_table('Thread', read_capacity_units=20, write_capacity_units=20)

# You can create tables as well, although the syntax is verbose. You should really use the model API instead,
#  but here is a low level example to demonstrate the point:

kwargs = {
    'write_capacity_units':
    1,
Ejemplo n.º 7
0
def updateOrder(request):
    body = request.body.decode('utf-8')
    params = json.loads(body)
    shop_domain = params.get('shop_domain', 'nothing')
    order_status = params.get('order_status', 'nothing')  #use in future
    is_digital = params.get('is_digital', False)  #use in future
    shipping_company = params.get('shipping_company',
                                  'nothing')  #use in future
    tracking_no = params.get('tracking_no', 'nothing')  #use in future
    print(shop_domain)
    conn = Connection(region='us-east-1')
    table = conn.list_tables()
    personal_data = conn.query('updateOrder', shop_domain)

    if personal_data == {}:
        data = {'status': 'Failure'}
        dump = json.dumps(data)
        return HttpResponse(dump, content_type='application/json')
    print(personal_data)
    for item in personal_data['Items']:
        print(item['username']['S'])
        comment_numper = 4
        driver = webdriver.Chrome('chromedriver')
        driver.get("https://news.ycombinator.com/news")
        driver.maximize_window()
        time.sleep(1)
        elem = driver.find_element_by_xpath(
            '//a[@href="login?goto=news"]'
        )  # driver.find_element_by_link_text("login")
        ActionChains(driver).move_to_element(elem).click().perform()

        elem = driver.find_element_by_name("acct")
        ActionChains(driver).move_to_element(elem).click().perform()
        ActionChains(driver).send_keys(item['username']['S']).perform()

        elem = driver.find_element_by_name("pw")
        ActionChains(driver).move_to_element(elem).click().perform()
        ActionChains(driver).send_keys(item['password']['S']).perform()

        ActionChains(driver).send_keys(Keys.RETURN).perform()

        driver.get("https://news.ycombinator.com/news")
        time.sleep(1)

        upvote_elems = driver.find_elements_by_class_name("votearrow")
        ActionChains(driver).move_to_element(upvote_elems[random.randint(
            0,
            len(upvote_elems) - 1)]).click().perform()

        time.sleep(1)

        comment_elems = driver.find_elements_by_partial_link_text("comment")
        ActionChains(driver).move_to_element(
            comment_elems[int(comment_numper)]).click().perform()

        write_comment_element = driver.find_element_by_xpath(
            '//textarea[@name="text"]')
        ActionChains(driver).move_to_element(
            write_comment_element).click().perform()
        ActionChains(driver).send_keys(
            'This is one of the best test comments ever. Its bigly.').perform(
            )

        submit_element = driver.find_element_by_xpath(
            '//input[@type="submit"]')
        ActionChains(driver).move_to_element(submit_element).click().perform()

        time.sleep(1)

        driver.get("https://news.ycombinator.com/news")
        logout_elem = driver.find_element_by_id("logout")
        ActionChains(driver).move_to_element(logout_elem).click().perform()

        driver.close()
    data = {'status': 'Success'}
    dump = json.dumps(data)
    return HttpResponse(dump, content_type='application/json')
Ejemplo n.º 8
0
                }
            }
        ]
    }
    print("conn.create_table...")
    conn.create_table(table_name, **params)

while table is None:
    time.sleep(2)
    table = conn.describe_table(table_name)

while table['TableStatus'] == 'CREATING':
    time.sleep(2)
    table = conn.describe_table(table_name)
print("conn.list_tables")
conn.list_tables()
print("conn.update_table...")

conn.update_table(
    table_name,
    read_capacity_units=table.get(PROVISIONED_THROUGHPUT).get(READ_CAPACITY_UNITS) + 1,
    write_capacity_units=2
)

table = conn.describe_table(table_name)

while table['TableStatus'] != 'ACTIVE':
    time.sleep(2)
    table = conn.describe_table(table_name)

print("conn.put_item")