Best Python code snippet using localstack_python
test_describe_table.py
Source:test_describe_table.py  
...31import time32from util import multiset33# Test that DescribeTable correctly returns the table's name and state34def test_describe_table_basic(test_table):35    got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']36    assert got['TableName'] == test_table.name37    assert got['TableStatus'] == 'ACTIVE'38# Test that DescribeTable correctly returns the table's schema, in39# AttributeDefinitions and KeySchema attributes40def test_describe_table_schema(test_table):41    got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']42    expected = { # Copied from test_table()'s fixture43        'KeySchema': [ { 'AttributeName': 'p', 'KeyType': 'HASH' },44                    { 'AttributeName': 'c', 'KeyType': 'RANGE' }45        ],46        'AttributeDefinitions': [47                    { 'AttributeName': 'p', 'AttributeType': 'S' },48                    { 'AttributeName': 'c', 'AttributeType': 'S' },49        ]50    }51    assert got['KeySchema'] == expected['KeySchema']52    # The list of attribute definitions may be arbitrarily reordered53    assert multiset(got['AttributeDefinitions']) == multiset(expected['AttributeDefinitions'])54# Test that DescribeTable correctly returns the table's billing mode,55# in the BillingModeSummary attribute.56def test_describe_table_billing(test_table):57    got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']58    assert got['BillingModeSummary']['BillingMode'] == 'PAY_PER_REQUEST'59    # The BillingModeSummary should also contain a60    # LastUpdateToPayPerRequestDateTime attribute, which is a date.61    # We don't know what date this is supposed to be, but something we62    # do know is that the test table was created already with this billing63    # mode, so the table creation date should be the same as the billing64    # mode setting date.65    assert 'LastUpdateToPayPerRequestDateTime' in got['BillingModeSummary']66    assert got['BillingModeSummary']['LastUpdateToPayPerRequestDateTime'] == got['CreationDateTime']67# Test that DescribeTable correctly returns the table's creation time.68# We don't know what this creation time is supposed to be, so this test69# cannot be very thorough... We currently just tests against something we70# know to be wrong - returning the *current* time, which changes on every71# call.72@pytest.mark.xfail(reason="DescribeTable does not return table creation time")73def test_describe_table_creation_time(test_table):74    got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']75    assert 'CreationDateTime' in got76    time1 = got['CreationDateTime']77    time.sleep(1) 78    got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']79    time2 = got['CreationDateTime']80    assert time1 == time281# Test that DescribeTable returns the table's estimated item count82# in the ItemCount attribute. Unfortunately, there's not much we can83# really test here... The documentation says that the count can be84# delayed by six hours, so the number we get here may have no relation85# to the current number of items in the test table. The attribute should exist,86# though. This test does NOT verify that ItemCount isn't always returned as87# zero - such stub implementation will pass this test.88@pytest.mark.xfail(reason="DescribeTable does not return table item count")89def test_describe_table_item_count(test_table):90    got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']91    assert 'ItemCount' in got92# Similar test for estimated size in bytes - TableSizeBytes - which again,93# may reflect the size as long as six hours ago.94@pytest.mark.xfail(reason="DescribeTable does not return table size")95def test_describe_table_size(test_table):96    got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']97    assert 'TableSizeBytes' in got98# Test the ProvisionedThroughput attribute returned by DescribeTable.99# This is a very partial test: Our test table is configured without100# provisioned throughput, so obviously it will not have interesting settings101# for it. DynamoDB returns zeros for some of the attributes, even though102# the documentation suggests missing values should have been fine too.103@pytest.mark.xfail(reason="DescribeTable does not return provisioned throughput")104def test_describe_table_provisioned_throughput(test_table):105    got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']106    assert got['ProvisionedThroughput']['NumberOfDecreasesToday'] == 0107    assert got['ProvisionedThroughput']['WriteCapacityUnits'] == 0108    assert got['ProvisionedThroughput']['ReadCapacityUnits'] == 0109# This is a silly test for the RestoreSummary attribute in DescribeTable -110# it should not exist in a table not created by a restore. When testing111# the backup/restore feature, we will have more meaninful tests for the112# value of this attribute in that case.113def test_describe_table_restore_summary(test_table):114    got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']115    assert not 'RestoreSummary' in got116# This is a silly test for the SSEDescription attribute in DescribeTable -117# by default, a table is encrypted with AWS-owned keys, not using client-118# owned keys, and the SSEDescription attribute is not returned at all.119def test_describe_table_encryption(test_table):120    got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']121    assert not 'SSEDescription' in got122# This is a silly test for the StreamSpecification attribute in DescribeTable -123# when there are no streams, this attribute should be missing.124def test_describe_table_stream_specification(test_table):125    got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']126    assert not 'StreamSpecification' in got127# Test that the table has an ARN, a unique identifier for the table which128# includes which zone it is on, which account, and of course the table's129# name. The ARN format is described in130# https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html#genref-arns131def test_describe_table_arn(test_table):132    got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']133    assert 'TableArn' in got and got['TableArn'].startswith('arn:')134# Test that the table has a TableId.135# TODO: Figure out what is this TableId supposed to be, it is just a136# unique id that is created with the table and never changes? Or anything137# else?138@pytest.mark.xfail(reason="DescribeTable does not return TableId")139def test_describe_table_id(test_table):140    got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']141    assert 'TableId' in got142# DescribeTable error path: trying to describe a non-existent table should143# result in a ResourceNotFoundException.144def test_describe_table_non_existent_table(dynamodb):145    with pytest.raises(ClientError, match='ResourceNotFoundException') as einfo:146        dynamodb.meta.client.describe_table(TableName='non_existent_table')147    # As one of the first error-path tests that we wrote, let's test in more148    # detail that the error reply has the appropriate fields:149    response = einfo.value.response150    print(response)151    err = response['Error']152    assert err['Code'] == 'ResourceNotFoundException'...unit_test_catalog-3.py
Source:unit_test_catalog-3.py  
...24    cat = CSVCatalog.CSVCatalog()25    t = cat.create_table(26        "teams",27        "../data/Teams.csv")28    print("Test1: \n Teams table", json.dumps(t.describe_table(), indent = 2), "\n\n")    # The output should be: Teams table {"definition": {"name": "teams", "path": "../data/Teams.csv"}, "columns": [], "indexes": {}}29    t.add_column_definition(CSVCatalog.ColumnDefinition("Rank", "number"))30    t.add_column_definition(CSVCatalog.ColumnDefinition("AB", "number"))31    t.add_column_definition(CSVCatalog.ColumnDefinition("CG", "number"))32    t.add_column_definition(CSVCatalog.ColumnDefinition("teamID", "text", not_null=True))33    t.drop_column_definition("Rank")34    print("Test2: \n Teams table", json.dumps(t.describe_table(), indent = 2), "\n\n")    # The output should have "AB" and "teamID"'s column definitions35    t.define_primary_key(["teamID"])36    print("Test3: \n Teams table", json.dumps(t.describe_table(), indent = 2), "\n\n")    # The output should have a "PRIMARY" index now37    t.define_index("data1", ["AB"], "INDEX")                # The output should have a "data1" index now38    t.define_index("data2", ["CG"])                         # The output should have a "data2" index now39    print("Test4: \n Teams table", json.dumps(t.describe_table(), indent = 2), "\n\n")    # Test the index definitions40    t.drop_column_definition("AB")41    print("Test5: \n Teams table", json.dumps(t.describe_table(), indent = 2), "\n\n")    # Test the column and index definitions, column "AB" and index "data1" should disappear42    test_create_table_1()43    p = CSVCatalog.CSVCatalog().get_table("People")44    p.add_column_definition(CSVCatalog.ColumnDefinition("playerID", "TEXT", not_null=True))45    p.define_primary_key(["playerID"])46    print("Test6: \n People table", json.dumps(p.describe_table(), indent = 2), "\n\n")   # Test reloading an existing table and altering the existing table47    t.drop_index("data2")48    print("Test7: \n Teams table", json.dumps(t.describe_table(), indent = 2), "\n\n")    # Test dropping an existing index, the index "data2" should disappear49    print_test_separator("Complete test_table_6")50def test_table_7_fail():51    """52    Simple create of table definition. No columns or indexes.53    :return:54    """55    cleanup()56    print_test_separator("Starting test_table_7")57    cat = CSVCatalog.CSVCatalog()58    try:59        t = cat.create_table(60            "teams",61            "../data/Teams.csv")62        print("Teams table", json.dumps(t.describe_table(), indent = 2))63        t.add_column_definition(CSVCatalog.ColumnDefinition("Rank", "number"))64        t.add_column_definition(CSVCatalog.ColumnDefinition("AB", "number"))65        t.add_column_definition(CSVCatalog.ColumnDefinition("playerID", "text"))    # should raise an error -100 here since there's no "playerID" in "Teams"66    except Exception as e:67        print("Exception e = ", e)68        print_test_separator("SUCCESS test_table_7 should fail.")69    print_test_separator("Complete test_table_7")70def test_table_8_fail():71    """72    Simple create of table definition. No columns or indexes.73    :return:74    """75    cleanup()76    print_test_separator("Starting test_table_8")77    cat = CSVCatalog.CSVCatalog()78    try:79        t = cat.create_table(80            "teams",81            "../data/Teams.csv")82        print("Teams table", json.dumps(t.describe_table(), indent = 2))83        t.add_column_definition(CSVCatalog.ColumnDefinition("Rank", "number"))84        t.add_column_definition(CSVCatalog.ColumnDefinition("AB", "number"))85        t.add_column_definition(CSVCatalog.ColumnDefinition("teamID", "text"))86        t.drop_column_definition("W")            # should raise an error -401 here since there's no "W" in column definitions87    except Exception as e:88        print("Exception e = ", e)89        print_test_separator("SUCCESS test_table_8 should fail.")90    print_test_separator("Complete test_table_8")91def test_table_9_fail():92    """93    Simple create of table definition. No columns or indexes.94    :return:95    """96    cleanup()97    print_test_separator("Starting test_table_9")98    cat = CSVCatalog.CSVCatalog()99    try:100        t = cat.create_table(101            "teams",102            "../data/Teams.csv")103        print("Teams table", json.dumps(104            t.describe_table(), indent = 2))105        t.add_column_definition(CSVCatalog.ColumnDefinition("Rank", "number"))106        t.add_column_definition(CSVCatalog.ColumnDefinition("AB", "number"))107        t.add_column_definition(CSVCatalog.ColumnDefinition("teamID", "text"))108        t.drop_column_definition("Rank")109        print("Teams table", json.dumps(t.describe_table(), indent = 2))110        t.define_primary_key(["teamID"])111        print("Teams table", json.dumps(t.describe_table(), indent = 2))112        t.define_index("data1", ["AB", "HB"], "INDEX")  # should raise an error -403 here since there's no "HB" in column definitions113    except Exception as e:114        print("Exception e = ", e)115        print_test_separator("SUCCESS test_table_9 should fail.")116    print_test_separator("Complete test_table_9")117def test_table_10_fail():118    """119    Simple create of table definition. No columns or indexes.120    :return:121    """122    cleanup()123    print_test_separator("Starting test_table_10")124    cat = CSVCatalog.CSVCatalog()125    try:126        t = cat.create_table(127            "teams",128            "../data/Teams.csv")129        print("Teams table", json.dumps(t.describe_table(), indent = 2))130        t.add_column_definition(CSVCatalog.ColumnDefinition("Rank", "number"))131        t.add_column_definition(CSVCatalog.ColumnDefinition("AB", "number"))132        t.add_column_definition(CSVCatalog.ColumnDefinition("CG", "number"))133        t.add_column_definition(CSVCatalog.ColumnDefinition("teamID", "text", not_null=True))134        t.drop_column_definition("Rank")135        print("Teams table", json.dumps(t.describe_table(), indent = 2))136        t.define_primary_key(["teamID"])137        print("Teams table", json.dumps(t.describe_table(), indent = 2))138        t.define_index("data1", ["AB"], "INDEX")139        t.define_index("data2", ["CG"])140        print("Teams table", json.dumps(t.describe_table(), indent = 2))141        t.drop_column_definition("AB")142        print("Teams table", json.dumps(t.describe_table(), indent = 2))143        t.drop_index("data1")             # should raise an error -403 here: because we dropped "AB", the "data1" index should disappear144    except Exception as e:145        print("Exception e = ", e)146        print_test_separator("SUCCESS test_table_10 should fail.")147    print_test_separator("Complete test_table_10")148def test_create_table_1():149    """150    Simple create of table definition. No columns or indexes.151    :return:152    """153    cat = CSVCatalog.CSVCatalog()154    t = cat.create_table(155        "people",156        "../data/People.csv")...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
