Skip to content

Commit

Permalink
[Supabase] bump to 2.7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDSM committed Aug 19, 2024
1 parent 2a8f387 commit 98e1fa9
Show file tree
Hide file tree
Showing 20 changed files with 424 additions and 770 deletions.
28 changes: 26 additions & 2 deletions additional_tests/supabase_backend_tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
VERBOSE = False


@pytest_asyncio.fixture
async def anon_client():
async with _anon_client() as client:
yield client


@pytest_asyncio.fixture
async def authenticated_client_1():
async with _authenticated_client(*get_backend_client_creds(1)) as client:
Expand Down Expand Up @@ -145,14 +151,32 @@ async def _authenticated_client(email, password, admin_key=None):
supabase_client = community.CommunitySupabaseClient(
backend_url,
admin_key or backend_key,
community.SyncConfigurationStorage(config)
community.ASyncConfigurationStorage(config)
)
if admin_key is None:
await supabase_client.sign_in(email, password)
yield supabase_client
finally:
if supabase_client:
await supabase_client.close()
await supabase_client.aclose()


@contextlib.asynccontextmanager
async def _anon_client():
config = commons_configuration.Configuration("", "")
config.config = {}
backend_url, backend_key = get_backend_api_creds()
supabase_client = None
try:
supabase_client = community.CommunitySupabaseClient(
backend_url,
backend_key,
community.ASyncConfigurationStorage(config)
)
yield supabase_client
finally:
if supabase_client:
await supabase_client.aclose()


@pytest_asyncio.fixture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
authenticated_client_3, get_backend_client_creds, sandboxed_insert
import octobot.community.supabase_backend.enums as enums
import octobot.community.supabase_backend.community_supabase_client as community_supabase_client
import octobot.community.supabase_backend.supabase_realtime_client as supabase_realtime_client


# All test coroutines will be treated as marked.
Expand All @@ -40,15 +39,7 @@
VERBOSE = True


async def test_not_listen(authenticated_client_1):
assert isinstance(authenticated_client_1.realtime, supabase_realtime_client.AuthenticatedSupabaseRealtimeClient)
# access_token is passed and is a real user auth token (not the anon supabase_key)
assert len(authenticated_client_1.realtime.access_token) > len(authenticated_client_1.supabase_key)
# ensure no connection is created as long as subscribe is not called
assert authenticated_client_1.realtime.channels == []
assert authenticated_client_1.realtime.socket.closed is True
assert authenticated_client_1.realtime.socket.connected is False
assert authenticated_client_1.realtime.socket.ws_connection is None
# DISABLED UNTIL REALTIME IS USED AGAIN


async def test_listen_one_chan(authenticated_client_1, authenticated_client_2, authenticated_client_3, sandboxed_insert):
Expand Down
8 changes: 5 additions & 3 deletions additional_tests/supabase_backend_tests/test_bot_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ async def test_fetch_startup_info(authenticated_client_1_with_temp_bot):
)


async def test_upsert_and_reset_trades(authenticated_client_1_with_temp_bot, authenticated_client_2):
async def test_upsert_and_reset_trades(authenticated_client_1_with_temp_bot, authenticated_client_2): #todo
authenticated_client_1, bot_id = authenticated_client_1_with_temp_bot
# bot just created, no trade yet
existing_trades = await authenticated_client_1.fetch_trades(bot_id)
Expand Down Expand Up @@ -308,8 +308,9 @@ def _equal(d_1, d_2, key):
for sub_key in range(len(d_1[key]))
)
if key == "time":
return community.CommunitySupabaseClient.get_parsed_time(d_1[key]) == \
community.CommunitySupabaseClient.get_parsed_time(d_2[key])
t1 = community.CommunitySupabaseClient.get_parsed_time(d_1[key].split("+0")[0])
t2 = community.CommunitySupabaseClient.get_parsed_time(d_2[key].split("+0")[0])
return t1 == t2
return d_1[key] == d_2[key]


Expand All @@ -325,6 +326,7 @@ def trades_mock(seed, bot_id):
octobot_trading.enums.TraderOrderType.SELL_LIMIT.value if int(seed + i) % 2 == 0
else octobot_trading.enums.TraderOrderType.BUY_MARKET.value,
octobot_trading.enums.ExchangeConstantsOrderColumns.ENTRIES.value: ["entry_id"],
octobot_trading.enums.ExchangeConstantsOrderColumns.BROKER_APPLIED.value: True,
}
for i in range(2)
]
Expand Down
74 changes: 74 additions & 0 deletions additional_tests/supabase_backend_tests/test_candles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# This file is part of OctoBot (https://github.com/Drakkar-Software/OctoBot)
# Copyright (c) 2023 Drakkar-Software, All rights reserved.
#
# OctoBot is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either
# version 3.0 of the License, or (at your option) any later version.
#
# OctoBot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with OctoBot. If not, see <https://www.gnu.org/licenses/>.
import math
import time
import pytest

import octobot_commons.enums as commons_enums
from additional_tests.supabase_backend_tests import authenticated_client_1, anon_client


# All test coroutines will be treated as marked.
pytestmark = pytest.mark.asyncio


async def test_fetch_candles_history_range_as_auth(authenticated_client_1):
# locale db
min_time, max_time = await authenticated_client_1.fetch_candles_history_range(
"binance", "BTC/USDT", commons_enums.TimeFrames.FOUR_HOURS, False
)
assert 0 < min_time < max_time < time.time()

# prod db
prod_min_time, prod_max_time = await authenticated_client_1.fetch_candles_history_range(
"binance", "BTC/USDT", commons_enums.TimeFrames.FOUR_HOURS, True
)
assert 0 < prod_min_time < prod_max_time < time.time()
assert prod_min_time != min_time
assert prod_max_time != max_time


async def test_fetch_candles_history_range_as_anon(anon_client):
# locale db
min_time, max_time = await anon_client.fetch_candles_history_range(
"binance", "BTC/USDT", commons_enums.TimeFrames.FOUR_HOURS, False
)
assert 0 < min_time < max_time < time.time()

# prod db
prod_min_time, prod_max_time = await anon_client.fetch_candles_history_range(
"binance", "BTC/USDT", commons_enums.TimeFrames.FOUR_HOURS, True
)
assert 0 < prod_min_time < prod_max_time < time.time()
assert prod_min_time != min_time
assert prod_max_time != max_time


async def test_fetch_candles_history(anon_client):
start_time = 1718785679
end_time = 1721377495
candles_count = math.floor((end_time - start_time) / (
commons_enums.TimeFramesMinutes[commons_enums.TimeFrames.FIFTEEN_MINUTES] * 60
))
# requires multiple fetches
assert candles_count == 2879
candles = await anon_client.fetch_candles_history(
"binance", "BTC/USDT", commons_enums.TimeFrames.FIFTEEN_MINUTES, start_time, end_time
)
fetched_count = candles_count + 1
assert len(candles) == fetched_count
# candles are unique
assert len(set(c[0] for c in candles)) == fetched_count
46 changes: 46 additions & 0 deletions additional_tests/supabase_backend_tests/test_cryptocurrencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This file is part of OctoBot (https://github.com/Drakkar-Software/OctoBot)
# Copyright (c) 2023 Drakkar-Software, All rights reserved.
#
# OctoBot is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either
# version 3.0 of the License, or (at your option) any later version.
#
# OctoBot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with OctoBot. If not, see <https://www.gnu.org/licenses/>.
import postgrest
import pytest

from additional_tests.supabase_backend_tests import authenticated_client_1


# All test coroutines will be treated as marked.
pytestmark = pytest.mark.asyncio


async def test_paginated_fetch_cryptocurrencies(authenticated_client_1):
pages = []

def cryptocurrencies_request_factory(table: postgrest.AsyncRequestBuilder, select_count):
pages.append(1)
return (
table.select(
"symbol, last_price",
count=select_count
)
)

raw_currencies = await authenticated_client_1.paginated_fetch(
authenticated_client_1,
"cryptocurrencies",
cryptocurrencies_request_factory
)
assert len(raw_currencies) > 1500
# ensure at least 2 calls have been performed
assert 2 <= sum(pages)

41 changes: 41 additions & 0 deletions additional_tests/supabase_backend_tests/test_storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This file is part of OctoBot (https://github.com/Drakkar-Software/OctoBot)
# Copyright (c) 2023 Drakkar-Software, All rights reserved.
#
# OctoBot is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either
# version 3.0 of the License, or (at your option) any later version.
#
# OctoBot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with OctoBot. If not, see <https://www.gnu.org/licenses/>.
import json
import pytest

from additional_tests.supabase_backend_tests import admin_client


# All test coroutines will be treated as marked.
pytestmark = pytest.mark.asyncio


async def test_upload_asset(admin_client):
asset_content = json.dumps({"test_upload_asset_content": 1}).encode()
asset_name = "test_upload_asset"
asset_bucket = "product-images"
await admin_client.remove_asset(asset_bucket, asset_name) # remove asset if exists
uploaded_asset_id = await admin_client.upload_asset(asset_bucket, asset_name, asset_content)

assets = await admin_client.list_assets(asset_bucket)
asset_by_id = {
asset["id"]: asset
for asset in assets
}
assert uploaded_asset_id in asset_by_id
assert asset_by_id[uploaded_asset_id]["name"] == asset_name

await admin_client.remove_asset(asset_bucket, asset_name)
24 changes: 16 additions & 8 deletions additional_tests/supabase_backend_tests/test_user_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import octobot.community as community
import octobot.community.supabase_backend.enums as supabase_backend_enums
from additional_tests.supabase_backend_tests import authenticated_client_1, authenticated_client_2, \
admin_client, get_backend_api_creds, skip_if_no_service_key
admin_client, anon_client, get_backend_api_creds, skip_if_no_service_key


# All test coroutines will be treated as marked.
Expand All @@ -36,6 +36,14 @@ async def test_get_user(authenticated_client_1):
)


async def test_get_auth_key(authenticated_client_1, anon_client):
user_auth_key = authenticated_client_1._get_auth_key()
assert user_auth_key != authenticated_client_1.supabase_key

anon_auth_key = anon_client._get_auth_key()
assert anon_auth_key == anon_client.supabase_key


async def test_update_metadata(authenticated_client_1):
user = await authenticated_client_1.get_user()
previous_metadata = user[supabase_backend_enums.UserKeys.USER_METADATA.value]
Expand Down Expand Up @@ -68,7 +76,7 @@ async def test_fetch_subscribed_products_urls(authenticated_client_1):
async def test_sign_in_with_otp_token(authenticated_client_1, skip_if_no_service_key, admin_client):
# generate OTP token
user = await authenticated_client_1.get_user()
res = admin_client.auth.admin.generate_link({"email": user["email"], "type": "magiclink"})
res = await admin_client.auth.admin.generate_link({"email": user["email"], "type": "magiclink"})
token = res.properties.hashed_token

# create new client
Expand All @@ -80,19 +88,19 @@ async def test_sign_in_with_otp_token(authenticated_client_1, skip_if_no_service
supabase_client = community.CommunitySupabaseClient(
backend_url,
backend_key,
community.SyncConfigurationStorage(config)
community.ASyncConfigurationStorage(config)
)
saved_session = "saved_session"
supabase_client.auth._storage.set_item(supabase_client.auth._storage_key, saved_session)
await supabase_client.auth._storage.set_item(supabase_client.auth._storage_key, saved_session)
# wrong token
with pytest.raises(authentication.AuthenticationError):
await supabase_client.sign_in_with_otp_token("1234")
# save session has not been removed
assert supabase_client.auth._storage.get_item(supabase_client.auth._storage_key) == saved_session
assert await supabase_client.auth._storage.get_item(supabase_client.auth._storage_key) == saved_session

await supabase_client.sign_in_with_otp_token(token)
# save session has been updated
updated_session = supabase_client.auth._storage.get_item(supabase_client.auth._storage_key)
updated_session = await supabase_client.auth._storage.get_item(supabase_client.auth._storage_key)
assert updated_session != saved_session

# ensure new supabase_client is bound to the same user as the previous client
Expand All @@ -102,7 +110,7 @@ async def test_sign_in_with_otp_token(authenticated_client_1, skip_if_no_service
# already consumed token
with pytest.raises(authentication.AuthenticationError):
await supabase_client.sign_in_with_otp_token(token)
assert supabase_client.auth._storage.get_item(supabase_client.auth._storage_key) == updated_session
assert await supabase_client.auth._storage.get_item(supabase_client.auth._storage_key) == updated_session
finally:
if supabase_client:
await supabase_client.close()
await supabase_client.aclose()
2 changes: 0 additions & 2 deletions octobot/community/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
StrategyData,
)
from octobot.community.supabase_backend import (
PostgresFunctions,
SyncConfigurationStorage,
ASyncConfigurationStorage,
AuthenticatedAsyncSupabaseClient,
Expand Down Expand Up @@ -108,7 +107,6 @@
"ErrorsUploader",
"StartupInfo",
"StrategyData",
"PostgresFunctions",
"SyncConfigurationStorage",
"ASyncConfigurationStorage",
"AuthenticatedAsyncSupabaseClient",
Expand Down
Loading

0 comments on commit 98e1fa9

Please sign in to comment.