From 782d05fd5503b7f603b84c8d0764eaa7e39ff198 Mon Sep 17 00:00:00 2001 From: maxwellflitton Date: Wed, 15 Jan 2025 02:45:10 +0000 Subject: [PATCH 1/2] adding DB snapshots --- .gitignore | 1 + docker-compose.yml | 12 ++++++++ scripts/download_snapshots.sh | 15 ++++++++++ surrealdb/data/types/datetime.py | 8 ++--- surrealdb/data/types/duration.py | 3 +- tests/integration/async/test_batch.py | 41 ++++++++++++++++++++++++++ tests/unit/cbor_types/test_datetime.py | 4 ++- 7 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 scripts/download_snapshots.sh create mode 100644 tests/integration/async/test_batch.py diff --git a/.gitignore b/.gitignore index 293663e7..e1536619 100644 --- a/.gitignore +++ b/.gitignore @@ -87,3 +87,4 @@ clog/ manifest/ .mypy_cache/ .ruff_cache/ +tests/db_snapshots/ diff --git a/docker-compose.yml b/docker-compose.yml index 2f3b664f..e379a96b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,18 @@ services: ports: - 8000:8000 + surrealdb_big_data: + image: surrealdb/surrealdb:v2.0.0 + command: "start" + environment: + - SURREAL_USER=root + - SURREAL_PASS=root + - SURREAL_LOG=trace + ports: + - 8300:8000 + volumes: + - ./tests/db_snapshots/data/:/data/ + surrealdb_200: image: surrealdb/surrealdb:v2.0.0 command: "start" diff --git a/scripts/download_snapshots.sh b/scripts/download_snapshots.sh new file mode 100644 index 00000000..21f477f8 --- /dev/null +++ b/scripts/download_snapshots.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +# navigate to directory +SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" +cd $SCRIPTPATH + +cd .. +cd tests + +if [ -d "./db_snapshots" ]; then + echo "DB snapshots are already present" + rm -rf ./db_snapshots +fi + +dockpack pull -i maxwellflitton/surrealdb-data -d ./db_snapshots diff --git a/surrealdb/data/types/datetime.py b/surrealdb/data/types/datetime.py index 30a4b022..53526d96 100644 --- a/surrealdb/data/types/datetime.py +++ b/surrealdb/data/types/datetime.py @@ -1,17 +1,17 @@ -import pytz # type: ignore - from dataclasses import dataclass from datetime import datetime -from math import floor from typing import Tuple +import pytz # type: ignore +from math import floor + @dataclass class DateTimeCompact: timestamp: int = 0 # nanoseconds @staticmethod - def parse(seconds: int, nanoseconds: int): + def parse(seconds: int, nanoseconds: int) -> "DateTimeCompact": return DateTimeCompact(nanoseconds + (seconds * pow(10, 9))) def get_seconds_and_nano(self) -> Tuple[int, int]: diff --git a/surrealdb/data/types/duration.py b/surrealdb/data/types/duration.py index 756e3771..09b94025 100644 --- a/surrealdb/data/types/duration.py +++ b/surrealdb/data/types/duration.py @@ -1,7 +1,8 @@ from dataclasses import dataclass -from math import floor from typing import Tuple +from math import floor + @dataclass class Duration: diff --git a/tests/integration/async/test_batch.py b/tests/integration/async/test_batch.py new file mode 100644 index 00000000..dcac1084 --- /dev/null +++ b/tests/integration/async/test_batch.py @@ -0,0 +1,41 @@ +from typing import List +from unittest import TestCase, main + +from surrealdb import SurrealDB, RecordID +from tests.integration.connection_params import TestConnectionParams +import asyncio +import websockets + + +class TestBatch(TestCase): + + def setUp(self) -> None: + self.params = TestConnectionParams() + self.db = SurrealDB(self.params.url) + self.queries: List[str] = [] + + self.db.connect() + self.db.use(self.params.namespace, self.params.database) + self.db.sign_in("root", "root") + + # self.query = """ + # CREATE |product:1000000| CONTENT { + # name: rand::enum('Cruiser Hoodie', 'Surreal T-Shirt'), + # colours: [rand::string(10), rand::string(10),], + # price: rand::int(20, 60), + # time: { + # created_at: rand::time(1611847404, 1706455404), + # updated_at: rand::time(1651155804, 1716906204) + # } + # }; + # """ + # self.db.query(query=self.query) + + def tearDown(self) -> None: + pass + + def test_batch(self): + print("test_batch") + +if __name__ == '__main__': + main() diff --git a/tests/unit/cbor_types/test_datetime.py b/tests/unit/cbor_types/test_datetime.py index 8bd284d1..ec3f4249 100644 --- a/tests/unit/cbor_types/test_datetime.py +++ b/tests/unit/cbor_types/test_datetime.py @@ -1,4 +1,4 @@ -from unittest import TestCase +from unittest import TestCase, main from surrealdb.data.types.datetime import DateTimeCompact from surrealdb.data.cbor import encode, decode @@ -21,3 +21,5 @@ def test_datetime(self): self.assertEqual(decoded.get_date_time(), '2024-12-12T09:00:58.083988Z') +if __name__ == '__main__': + main() From 2714b3c7bd5415ea2c90acb420bb9df52dcfc5e3 Mon Sep 17 00:00:00 2001 From: maxwellflitton Date: Tue, 25 Feb 2025 10:53:50 +0000 Subject: [PATCH 2/2] adding the docs for getting the repo up and running --- CONTRIBUTING.md | 2 +- README.md | 51 +++++++++++++++++++ docker-compose.yml | 35 ------------- scripts/term.sh | 24 +++++++++ .../surrealdb/connections/mixins/__init__.py | 0 5 files changed, 76 insertions(+), 36 deletions(-) create mode 100644 scripts/term.sh rename scripts/setup.sh => src/surrealdb/connections/mixins/__init__.py (100%) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 72080f00..ceab3db1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -11,7 +11,7 @@ If you are worried or don’t know where to start, check out our next section ex Please help us keep SurrealDB open and inclusive. Kindly read and follow our [Code of Conduct](/CODE_OF_CONDUCT.md). diff --git a/README.md b/README.md index f695e941..2f413eb1 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,58 @@ with Surreal("ws://localhost:8000/rpc") as db: ## Next steps Now that you have learned the basics of the SurrealDB SDK for Python, you can learn more about the SDK and its methods [in the methods section](https://surrealdb.com/docs/sdk/python/methods) and [data types section](https://surrealdb.com/docs/sdk/python/data-types). + ## Contributing + Contributions to this library are welcome! If you encounter issues, have feature requests, or want to make improvements, feel free to open issues or submit pull requests. +If you want to contribute to the Github repo please read the general contributing guidelines on concepts such as how to create a pull requests [here](https://github.com/surrealdb/surrealdb.py/blob/main/CONTRIBUTING.md). + +## Getting the repo up and running + +To contribute, it's a good idea to get the repo up and running first. We can do this by running the tests. If the tests pass, your `PYTHONPATH` works and the client is making successful calls to the database. To do this we must run the database with the following command: + +```bash +# if the docker-compose binary is installed +docker-compose up -d + +# if you are running docker compose directly through docker +docker compose up -d +``` + +Now that the database is running, we can enter a terminal session with all the requirements installed and `PYTHONPATH` configured with the command below: + +```bash +bash scripts/term.sh +``` + +You will now be running an interactive terminal through a python virtual environment with all the dependencies installed. We can now run the tests with the following command: + +```bash +python -m unittest discover +``` + +The number of tests might increase but at the time of writing this you should get a printout like the one below: + +```bash +.........................................................................................................................................Error in live subscription: sent 1000 (OK); no close frame received +.......................................................................................... +---------------------------------------------------------------------- +Ran 227 tests in 6.313s + +OK +``` +Finally, we clean up the database with the command below: +```bash +# if the docker-compose binary is installed +docker-compose down + +# if you are running docker compose directly through docker +docker compose down +``` +To exit the terminal session merely execute the following command: +```bash +exit +``` + And there we have it, our tests are passing. diff --git a/docker-compose.yml b/docker-compose.yml index 8e48a7f8..97207000 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,42 +10,7 @@ services: - SURREAL_LOG=debug - SURREAL_CAPS_ALLOW_GUESTS=true ports: -<<<<<<< HEAD - - 8300:8000 - - surrealdb_big_data: - image: surrealdb-big-data -# environment: -# - SURREAL_USER=root -# - SURREAL_PASS=root -# - SURREAL_LOG=trace - ports: - - 9000:8000 - - surrealdb_big_data: - image: surrealdb/surrealdb:v2.0.0 - command: "start" - environment: - - SURREAL_USER=root - - SURREAL_PASS=root - - SURREAL_LOG=trace - ports: - - 8300:8000 - volumes: - - ./tests/db_snapshots/data/:/data/ - - surrealdb_200: - image: surrealdb/surrealdb:v2.0.0 - command: "start" - environment: - - SURREAL_USER=root - - SURREAL_PASS=root - - SURREAL_LOG=trace - ports: - - 8200:8000 -======= - 8000:8000 ->>>>>>> 0c8a24aca5689176fb3ee89398c08b2cd8ced4a1 surrealdb_121: image: surrealdb/surrealdb:v1.2.1 diff --git a/scripts/term.sh b/scripts/term.sh new file mode 100644 index 00000000..bdc6066b --- /dev/null +++ b/scripts/term.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +# navigate to directory +SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" +cd $SCRIPTPATH + +# Define the PYTHONPATH for the terminal session +cd ../src +export PYTHONPATH=$(pwd) +cd .. + +# Configure the virtual environment if needed +if [ -d "venv" ]; then + echo "existing python virtual environment exists." +else + echo "python virtual environment does not exist. Creating one" + python3 -m venv venv +fi + +# start the terminal session +source venv/bin/activate +pip install -r requirements.txt +echo "now running in virtual environment. Execute the 'exit' to exit the virtual env terminal" +bash --norc diff --git a/scripts/setup.sh b/src/surrealdb/connections/mixins/__init__.py similarity index 100% rename from scripts/setup.sh rename to src/surrealdb/connections/mixins/__init__.py