Skip to content

Do some type changes and cleanup #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,6 @@ dmypy.json

# VS Code settings
.vscode

# Poetry lock
poetry.lock
8 changes: 4 additions & 4 deletions examples/chat_history.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Set
from typing import List

from upstash_redis import Redis

Expand All @@ -19,7 +19,7 @@ class ChatModel:
def __init__(self, redis: Redis):
self.redis = redis

def get_user_chats(self, user_id: str) -> Set[str]:
def get_user_chats(self, user_id: str) -> List[str]:
# A set of chat ids for a user, stores which chats belong to the user
return self.redis.smembers(f"user:{user_id}:chats")

Expand Down Expand Up @@ -95,7 +95,7 @@ def delete_user(self, user_id: str):
# Output
# chatid_1: 1
# chatid_2: 2
# chat_ids: {'2', '1'}
# chat_ids: ['2', '1']
# chat 1 messages: ['user:Hello', 'bot:Hello', 'user:How are you?']
# chat 2 messages: ['user:This is chat2']
# chatids after deletion: set()
# chatids after deletion: []
2 changes: 2 additions & 0 deletions examples/scan_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# since a task must be popped from the waiting list by its id,
# we use a set


# scan:completed:123 -> a list of completed tasks for the client
class TaskQueue:
def __init__(self, redis: Redis):
Expand Down Expand Up @@ -64,6 +65,7 @@ def complete_scan(self, clientid: str, website: str, result: str):

queue.add_scan_task("client1", "https://google.com")


# This code will be run by multiple workers
def work():
task = queue.start_scan()
Expand Down
760 changes: 0 additions & 760 deletions poetry.lock

This file was deleted.

3 changes: 2 additions & 1 deletion tests/commands/asyncio/bitmap/test_bitop.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ async def test_not_with_more_than_one_source_key(async_redis: Redis) -> None:
== 'The "NOT " operation takes only one source key as argument.'
)


@mark.asyncio
async def test_not(async_redis: Redis) -> None:
assert (
await async_redis.bitop(
"NOT", "bitop_destination_4", "string_as_bitop_source_1"
)
== 4
)
)
13 changes: 9 additions & 4 deletions tests/commands/asyncio/generic/test_expire.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import datetime
from asyncio import sleep

from pytest import mark

from tests.execute_on_http import execute_on_http
from upstash_redis.asyncio import Redis

import datetime


@mark.asyncio
async def test(async_redis: Redis) -> None:
Expand All @@ -16,20 +15,26 @@ async def test(async_redis: Redis) -> None:
await sleep(1)
assert await execute_on_http("EXISTS", "string_for_expire") == 0


@mark.asyncio
async def test_with_datetime(async_redis: Redis) -> None:
assert await async_redis.expire("string_for_expire_dt", datetime.timedelta(seconds=1)) is True
assert (
await async_redis.expire("string_for_expire_dt", datetime.timedelta(seconds=1))
is True
)

# Check if the expiry was correctly set.
await sleep(1)
assert await execute_on_http("EXISTS", "string_for_expire_dt") == 0


@mark.asyncio
async def test_xx(async_redis: Redis) -> None:
# Must fail since it does not have an expiry.
assert await async_redis.expire("string_without_expire", 1, xx=True) is False


@mark.asyncio
async def test_gt(async_redis: Redis) -> None:
# Must fail since it 1 is not greater than infinity.
assert await async_redis.expire("string_without_expire", 1, gt=True) is False
assert await async_redis.expire("string_without_expire", 1, gt=True) is False
13 changes: 10 additions & 3 deletions tests/commands/asyncio/generic/test_expireat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from asyncio import sleep
import datetime
from asyncio import sleep
from time import time

from pytest import mark
Expand All @@ -21,10 +21,17 @@ async def test(async_redis: Redis) -> None:
await sleep(2)
assert await execute_on_http("EXISTS", "string_for_expireat") == 0


@mark.asyncio
async def test_with_datetime(async_redis: Redis) -> None:
assert await async_redis.expireat("string_for_expireat_dt", datetime.datetime.now() + datetime.timedelta(seconds=1)) is True
assert (
await async_redis.expireat(
"string_for_expireat_dt",
datetime.datetime.now() + datetime.timedelta(seconds=1),
)
is True
)

# Check if the expiry was correctly set.
await sleep(1)
assert await execute_on_http("EXISTS", "string_for_expireat_dt") == 0
assert await execute_on_http("EXISTS", "string_for_expireat_dt") == 0
12 changes: 9 additions & 3 deletions tests/commands/asyncio/generic/test_pexpire.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from asyncio import sleep
import datetime
from asyncio import sleep

from pytest import mark

Expand All @@ -15,10 +15,16 @@ async def test(async_redis: Redis) -> None:
await sleep(1)
assert await execute_on_http("EXISTS", "string_for_pexpire") == 0


@mark.asyncio
async def test_with_datetime(async_redis: Redis) -> None:
assert await async_redis.pexpire("string_for_pexpire_dt", datetime.timedelta(milliseconds=200)) is True
assert (
await async_redis.pexpire(
"string_for_pexpire_dt", datetime.timedelta(milliseconds=200)
)
is True
)

# Check if the expiry was correctly set.
await sleep(0.2)
assert await execute_on_http("EXISTS", "string_for_pexpire_dt") == 0
assert await execute_on_http("EXISTS", "string_for_pexpire_dt") == 0
13 changes: 10 additions & 3 deletions tests/commands/asyncio/generic/test_pexpireat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from asyncio import sleep
import datetime
from asyncio import sleep
from time import time

from pytest import mark
Expand All @@ -21,10 +21,17 @@ async def test(async_redis: Redis) -> None:
await sleep(2)
assert await execute_on_http("EXISTS", "string_for_pexpireat") == 0


@mark.asyncio
async def test_with_datetime(async_redis: Redis) -> None:
assert await async_redis.pexpireat("string_for_pexpireat_dt", datetime.datetime.now() + datetime.timedelta(milliseconds=200)) is True
assert (
await async_redis.pexpireat(
"string_for_pexpireat_dt",
datetime.datetime.now() + datetime.timedelta(milliseconds=200),
)
is True
)

# Check if the expiry was correctly set.
await sleep(0.2)
assert await execute_on_http("EXISTS", "string_for_pexpireat_dt") == 0
assert await execute_on_http("EXISTS", "string_for_pexpireat_dt") == 0
29 changes: 15 additions & 14 deletions tests/commands/asyncio/geo/test_georadius.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pytest import mark, raises

from upstash_redis.asyncio import Redis
from upstash_redis.utils import GeoSearchResult


@mark.asyncio
Expand All @@ -20,8 +21,8 @@ async def test_with_distance(async_redis: Redis) -> None:
unit="KM",
withdist=True,
) == [
{"member": "Palermo", "distance": 190.4424},
{"member": "Catania", "distance": 56.4413},
GeoSearchResult(member="Palermo", distance=190.4424),
GeoSearchResult(member="Catania", distance=56.4413),
]


Expand All @@ -35,8 +36,8 @@ async def test_with_hash(async_redis: Redis) -> None:
unit="KM",
withhash=True,
) == [
{"member": "Palermo", "hash": 3479099956230698},
{"member": "Catania", "hash": 3479447370796909},
GeoSearchResult(member="Palermo", hash=3479099956230698),
GeoSearchResult(member="Catania", hash=3479447370796909),
]


Expand All @@ -50,16 +51,16 @@ async def test_with_coordinates(async_redis: Redis) -> None:
unit="KM",
withcoord=True,
) == [
{
"member": "Palermo",
"longitude": 13.361389338970184,
"latitude": 38.115556395496299,
},
{
"member": "Catania",
"longitude": 15.087267458438873,
"latitude": 37.50266842333162,
},
GeoSearchResult(
member="Palermo",
longitude=13.361389338970184,
latitude=38.115556395496299,
),
GeoSearchResult(
member="Catania",
longitude=15.087267458438873,
latitude=37.50266842333162,
),
]


Expand Down
29 changes: 15 additions & 14 deletions tests/commands/asyncio/geo/test_georadius_ro.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pytest import mark, raises

from upstash_redis.asyncio import Redis
from upstash_redis.utils import GeoSearchResult


@mark.asyncio
Expand All @@ -20,8 +21,8 @@ async def test_with_distance(async_redis: Redis) -> None:
unit="KM",
withdist=True,
) == [
{"member": "Palermo", "distance": 190.4424},
{"member": "Catania", "distance": 56.4413},
GeoSearchResult(member="Palermo", distance=190.4424),
GeoSearchResult(member="Catania", distance=56.4413),
]


Expand All @@ -35,8 +36,8 @@ async def test_with_hash(async_redis: Redis) -> None:
unit="KM",
withhash=True,
) == [
{"member": "Palermo", "hash": 3479099956230698},
{"member": "Catania", "hash": 3479447370796909},
GeoSearchResult(member="Palermo", hash=3479099956230698),
GeoSearchResult(member="Catania", hash=3479447370796909),
]


Expand All @@ -50,16 +51,16 @@ async def test_with_coordinates(async_redis: Redis) -> None:
unit="KM",
withcoord=True,
) == [
{
"member": "Palermo",
"longitude": 13.361389338970184,
"latitude": 38.115556395496299,
},
{
"member": "Catania",
"longitude": 15.087267458438873,
"latitude": 37.50266842333162,
},
GeoSearchResult(
member="Palermo",
longitude=13.361389338970184,
latitude=38.115556395496299,
),
GeoSearchResult(
member="Catania",
longitude=15.087267458438873,
latitude=37.50266842333162,
),
]


Expand Down
29 changes: 15 additions & 14 deletions tests/commands/asyncio/geo/test_georadiusbymember.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pytest import mark, raises

from upstash_redis.asyncio import Redis
from upstash_redis.utils import GeoSearchResult


@mark.asyncio
Expand All @@ -22,8 +23,8 @@ async def test_with_distance(async_redis: Redis) -> None:
unit="KM",
withdist=True,
) == [
{"member": "Palermo", "distance": 166.2742},
{"member": "Catania", "distance": 0.0},
GeoSearchResult(member="Palermo", distance=166.2742),
GeoSearchResult(member="Catania", distance=0.0),
]


Expand All @@ -36,8 +37,8 @@ async def test_with_hash(async_redis: Redis) -> None:
unit="KM",
withhash=True,
) == [
{"member": "Palermo", "hash": 3479099956230698},
{"member": "Catania", "hash": 3479447370796909},
GeoSearchResult(member="Palermo", hash=3479099956230698),
GeoSearchResult(member="Catania", hash=3479447370796909),
]


Expand All @@ -50,16 +51,16 @@ async def test_with_coordinates(async_redis: Redis) -> None:
unit="KM",
withcoord=True,
) == [
{
"member": "Palermo",
"longitude": 13.3613893389701841,
"latitude": 38.115556395496299,
},
{
"member": "Catania",
"longitude": 15.087267458438873,
"latitude": 37.50266842333162,
},
GeoSearchResult(
member="Palermo",
longitude=13.3613893389701841,
latitude=38.115556395496299,
),
GeoSearchResult(
member="Catania",
longitude=15.087267458438873,
latitude=37.50266842333162,
),
]


Expand Down
Loading