def test_get_should_return_none_if_no_id_is_matched(
            self, mock_get_table, an_exchange, get_dynamodb_table):
        # Given
        mock_get_table.return_value = get_dynamodb_table

        # When
        exchange_repository.save(an_exchange)

        # Then
        actual_exchange = exchange_repository.get('non-existent-id')
        assert actual_exchange is None
    def test_save_should_create_readable_item(self, mock_get_table,
                                              an_exchange, get_dynamodb_table):
        # Given
        mock_get_table.return_value = get_dynamodb_table

        # When
        exchange_repository.save(an_exchange)

        # Then
        actual_exchange = exchange_repository.get(an_exchange.identifier)
        assert actual_exchange == an_exchange
    def test_delete_should_delete_indicated_exchange(self, mock_get_table,
                                                     an_exchange,
                                                     get_dynamodb_table):
        # Given
        mock_get_table.return_value = get_dynamodb_table
        exchange_repository.save(an_exchange)

        # When
        exchange_repository.delete(an_exchange.identifier)

        # Then
        assert exchange_repository.get(an_exchange.identifier) is None
    def test_get_by_download_token_should_return_an_exchange(
            self, mock_get_table, get_dynamodb_table, an_exchange):
        # Given
        mock_get_table.return_value = get_dynamodb_table
        exchange_repository.save(an_exchange)

        # When
        actual_exchange = exchange_repository.get_by_download_token(
            an_exchange.download_token)

        # Then
        assert actual_exchange == an_exchange
    def test_revoke_upload_token(self, mock_get_table, an_exchange,
                                 get_dynamodb_table):
        # Given
        mock_get_table.return_value = get_dynamodb_table
        exchange_repository.save(an_exchange)

        # When
        exchange_repository.revoke_upload(an_exchange.identifier)
        actual_exchange = exchange_repository.get(an_exchange.identifier)

        # Then
        assert actual_exchange.revoked_upload is True
    def test_get_identifier_from_token_should_return_identifier_associated_with_given_token(
            self, mock_get_table, an_exchange, get_dynamodb_table):
        # Given
        mock_get_table.return_value = get_dynamodb_table
        exchange_repository.save(an_exchange)

        # When
        identifier_from_upload_token = exchange_repository.get_identifier_from_token(
            upload_token=an_exchange.upload_token)
        identifier_from_download_token = exchange_repository.get_identifier_from_token(
            download_token=an_exchange.download_token)

        # Then
        assert identifier_from_upload_token == an_exchange.identifier
        assert identifier_from_download_token == an_exchange.identifier
    def test_get_before_time_should_return_only_exchanges_created_before_this_time(
            self, mock_get_table, get_dynamodb_table, an_exchange,
            generate_old_exchange):
        # Given
        mock_get_table.return_value = get_dynamodb_table
        time_before = datetime.now(timezone.utc) - timedelta(days=2)

        old_exchange = generate_old_exchange(days_before=3)
        exchange_repository.save(an_exchange)
        exchange_repository.save(old_exchange)

        # When
        actual_exchanges = exchange_repository.get_before_time(time_before)

        # Then
        assert actual_exchanges == [old_exchange]
    def test_get_account_exchanges_should_return_only_account_exchanges(
            self, mock_get_table, two_exchanges_with_same_sub,
            an_exchange_with_different_sub, get_dynamodb_table):
        # Given
        mock_get_table.return_value = get_dynamodb_table
        for exchange in two_exchanges_with_same_sub:
            exchange_repository.save(exchange)
        exchange_repository.save(an_exchange_with_different_sub)

        # When
        actual_exchanges = exchange_repository.get_account_exchanges(
            two_exchanges_with_same_sub[0].sub)

        # Then
        assert actual_exchanges[0] in two_exchanges_with_same_sub
        assert actual_exchanges[1] in two_exchanges_with_same_sub
        assert len(actual_exchanges) == 2