"""
Integration tests for disco_dynamodb.py
"""

import json
from random import randint
from test.helpers.integration_helpers import IntegrationTest


MOCK_TABLE_NAME = "IntegrationTest"
CREATE_CMD = "disco_dynamodb.py create --env {0} --config test/test_dynamodb_create.json --wait"
DELETE_CMD = "disco_dynamodb.py delete --table {0} --env {1} --wait"
LIST_CMD = "disco_dynamodb.py list"


class TestDiscoDynamoDB(IntegrationTest):
    """ Test bin/disco_dynamodb.py """

    def setUp(self):
        """
        Generate random environment name for integration test env
        """
        self.env_name = "env_{0}".format(randint(10000, 99999))

    def test_create_and_delete_table(self):
        """ Ensures we can create and delete DynamoDB table properly """
        table_list_output = u"{0:<20} {1}".format(MOCK_TABLE_NAME, self.env_name)

        try:
            create_output = self.run_cmd(CREATE_CMD.format(self.env_name).split())

            table = json.loads(create_output)

            self.assertEqual(table["TableStatus"], "ACTIVE")
            self.assertEqual(table["TableName"], "{0}_{1}".format(MOCK_TABLE_NAME, self.env_name))
            self.assertEqual(table["ProvisionedThroughput"]["WriteCapacityUnits"], 10)
            self.assertEqual(table["ProvisionedThroughput"]["ReadCapacityUnits"], 10)
            for key in table["KeySchema"]:
                if key["KeyType"] == "HASH":
                    self.assertEqual(key["AttributeName"], "_id")
                else:
                    self.assertEqual(key["AttributeName"], "mock_range_key")

            self.assertEqual(table["GlobalSecondaryIndexes"][0]["IndexName"], "mock_index")
            self.assertEqual(table["GlobalSecondaryIndexes"][0]["KeySchema"][0]["AttributeName"],
                             "mock_index_attr")
            self.assertEqual(table["GlobalSecondaryIndexes"][0]["Projection"]["ProjectionType"], "ALL")

            # Also assert that table can be found when running the list command
            list_output = self.run_cmd(LIST_CMD.format(self.env_name).split())
            lines = list_output.split('\n')
            self.assertIn(table_list_output, lines)
        finally:
            delete_output = self.run_cmd(DELETE_CMD.format(MOCK_TABLE_NAME, self.env_name).split())
            delete_output = json.loads(delete_output)

            self.assertEqual(delete_output["TableName"], "{0}_{1}".format(MOCK_TABLE_NAME, self.env_name))
            self.assertEqual(delete_output["TableStatus"], "DELETED")

        list_output = self.run_cmd(LIST_CMD.format(self.env_name).split())
        lines = list_output.split('\n')
        self.assertNotIn(table_list_output, lines)

@@ -970,16 +970,45 @@
 n.loads(
+self._remove_lines_from_logs(
 create_o
@@ -1005,32 +1005,33 @@
 s(create_output)
+)
 %0A%0A            se
@@ -2357,16 +2357,45 @@
 n.loads(
+self._remove_lines_from_logs(
 delete_o
@@ -2400,16 +2400,17 @@
 _output)
+)
 %0A%0A      
@@ -2688,32 +2688,32 @@
 put.split('%5Cn')%0A
-
         self.ass
@@ -2727,28 +2727,529 @@
 n(table_list_output, lines)%0A
+%0A    def _remove_lines_from_logs(self, input_string):%0A        lines = %5B%5D%0A%0A        for line in input_string.split(%22%5Cn%22):%0A            words = line.split()%0A%0A            try:%0A                # If it quacks like a logging line...%0A                if words%5B3%5D in %5B%22WARNING%22, %22WARN%22, %22INFO%22, %22DEBUG%22, %22CRITICAL%22, %22NOTSET%22%5D:%0A                    continue%0A            except IndexError:%0A                pass%0A%0A            lines.append(line)%0A%0A        output_string = %22%5Cn%22.join(lines)%0A%0A        return output_string%0A

Fix Broken DynamoDB Tests