From 62cb71e591d5fe7a6c9a608dad0303f95b1fe3cf Mon Sep 17 00:00:00 2001 From: Mugi Khan Date: Tue, 29 Oct 2024 17:51:29 +0200 Subject: [PATCH 1/6] Add unit testing docs --- .../flutter/unit-testing.mdx | 24 +++++++++++++++++++ mint.json | 1 + 2 files changed, 25 insertions(+) create mode 100644 client-sdk-references/flutter/unit-testing.mdx diff --git a/client-sdk-references/flutter/unit-testing.mdx b/client-sdk-references/flutter/unit-testing.mdx new file mode 100644 index 00000000..dd3fe5b0 --- /dev/null +++ b/client-sdk-references/flutter/unit-testing.mdx @@ -0,0 +1,24 @@ +--- +title: "Unit testing" +description: "Guidelines for unit testing with PowerSync" +--- + +For unit-testing your projects using PowerSync you will need the `powersync-sqlite-core` binary in you project's root folder. + +1. Download the PowerSync SQLite Binary + - Go to the PowerSync SQLite Core [Releases](https://github.com/powersync-ja/powersync-sqlite-core/releases). + - Download the binary compatible with your OS. +2. Rename the Binary + - Rename the binary by removing the architecture suffix. + - Example: powersync_x64.dll to powersync.dll + - Example: libpowersync_aarch64.dylib to libpowersync.dylib + - Example: libpowersync_x64.so to libpowersync.so +3. Place the binary in your project + - Move the renamed binary to the root directory of your project. + +#### If you have trouble with loading the extension confirm the following + +Ensure that your SQLite3 binary install on your system has extension loading enabled. You can confirm this by doing the following +- Run `sqlite3` in your command-line interface. +- In the sqlite3 prompt run `PRAGMA compile_options;` +- Check the output for the option ENABLE_LOAD_EXTENSION. diff --git a/mint.json b/mint.json index d76373ac..11c29a56 100644 --- a/mint.json +++ b/mint.json @@ -276,6 +276,7 @@ "client-sdk-references/flutter/flutter-web-support", "client-sdk-references/flutter/flutter-orm-support", "client-sdk-references/flutter/usage-examples", + "client-sdk-references/flutter/unit-testing", "client-sdk-references/flutter/api-reference" ] }, From c8c48d55dbc0176b69994e3d9a2bfff5a1146610 Mon Sep 17 00:00:00 2001 From: Mughees Khan Date: Tue, 29 Oct 2024 18:35:22 +0200 Subject: [PATCH 2/6] Update client-sdk-references/flutter/unit-testing.mdx Co-authored-by: benitav --- client-sdk-references/flutter/unit-testing.mdx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client-sdk-references/flutter/unit-testing.mdx b/client-sdk-references/flutter/unit-testing.mdx index dd3fe5b0..89491c0b 100644 --- a/client-sdk-references/flutter/unit-testing.mdx +++ b/client-sdk-references/flutter/unit-testing.mdx @@ -3,7 +3,8 @@ title: "Unit testing" description: "Guidelines for unit testing with PowerSync" --- -For unit-testing your projects using PowerSync you will need the `powersync-sqlite-core` binary in you project's root folder. +For unit-testing your projects using PowerSync + (e.g. testing whether your queries run as expected) you will need the `powersync-sqlite-core` binary in your project's root directory. 1. Download the PowerSync SQLite Binary - Go to the PowerSync SQLite Core [Releases](https://github.com/powersync-ja/powersync-sqlite-core/releases). From ea2a989e9e9e04b22d2776651a3a102092fc5c18 Mon Sep 17 00:00:00 2001 From: Mugi Khan Date: Tue, 29 Oct 2024 19:23:41 +0200 Subject: [PATCH 3/6] Include code snippet --- .../flutter/unit-testing.mdx | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/client-sdk-references/flutter/unit-testing.mdx b/client-sdk-references/flutter/unit-testing.mdx index 89491c0b..040dfde9 100644 --- a/client-sdk-references/flutter/unit-testing.mdx +++ b/client-sdk-references/flutter/unit-testing.mdx @@ -3,8 +3,8 @@ title: "Unit testing" description: "Guidelines for unit testing with PowerSync" --- -For unit-testing your projects using PowerSync - (e.g. testing whether your queries run as expected) you will need the `powersync-sqlite-core` binary in your project's root directory. +For unit-testing your projects using PowerSync +(e.g. testing whether your queries run as expected) you will need the `powersync-sqlite-core` binary in your project's root directory. 1. Download the PowerSync SQLite Binary - Go to the PowerSync SQLite Core [Releases](https://github.com/powersync-ja/powersync-sqlite-core/releases). @@ -17,9 +17,51 @@ For unit-testing your projects using PowerSync 3. Place the binary in your project - Move the renamed binary to the root directory of your project. +This snippet below is only included as a guide to unit testing in Flutter with PowerSync. For more information refer to the [official Flutter unit testing documentation](https://docs.flutter.dev/cookbook/testing/unit/introduction). + +```dart +import 'dart:io'; +import 'package:powersync/powersync.dart'; +import 'package:path/path.dart'; + +const schema = Schema([ + Table('customers', [Column.text('name'), Column.text('email')]) +]); + +late final PowerSyncDatabase testDB; + +String getTestDatabasePath() async { + const dbFilename = 'powersync-test.db'; + final dir = Directory.current.absolute.path; + return join(dir, dbFilename); +} + +Future openTestDatabase() async { + testDB = PowerSyncDatabase( + schema: schema, + path: await getTestDatabasePath(), + logger: testLogger, + ); + + await testDB.initialize(); +} + +test('INSERT', () async { + await powersync.execute( + 'INSERT INTO customers(name, email) VALUES(?, ?)', ['John Doe', 'john@hotmail.com']); + + final results = await powersync.getAll('SELECT * FROM customers'); + + expect(results.length, 1); + expect(results, ['John Doe', 'john@hotmail.com']); +}); +``` + #### If you have trouble with loading the extension confirm the following Ensure that your SQLite3 binary install on your system has extension loading enabled. You can confirm this by doing the following + - Run `sqlite3` in your command-line interface. - In the sqlite3 prompt run `PRAGMA compile_options;` - Check the output for the option ENABLE_LOAD_EXTENSION. +- If you see ENABLE_LOAD_EXTENSION, it means extension loading is enabled. From e95283f8e92b4dd2c985a85ec5ecac0fe9a98ed4 Mon Sep 17 00:00:00 2001 From: Mugi Khan Date: Tue, 29 Oct 2024 19:24:15 +0200 Subject: [PATCH 4/6] Formatting --- client-sdk-references/flutter/unit-testing.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client-sdk-references/flutter/unit-testing.mdx b/client-sdk-references/flutter/unit-testing.mdx index 040dfde9..63b84c5f 100644 --- a/client-sdk-references/flutter/unit-testing.mdx +++ b/client-sdk-references/flutter/unit-testing.mdx @@ -63,5 +63,5 @@ Ensure that your SQLite3 binary install on your system has extension loading ena - Run `sqlite3` in your command-line interface. - In the sqlite3 prompt run `PRAGMA compile_options;` -- Check the output for the option ENABLE_LOAD_EXTENSION. -- If you see ENABLE_LOAD_EXTENSION, it means extension loading is enabled. +- Check the output for the option `ENABLE_LOAD_EXTENSION`. +- If you see `ENABLE_LOAD_EXTENSION`, it means extension loading is enabled. From 8a840e2edb87f0edf0993a9cdd0ed2c11a10e7c7 Mon Sep 17 00:00:00 2001 From: Mugi Khan Date: Wed, 30 Oct 2024 09:59:37 +0200 Subject: [PATCH 5/6] Add steps to confirm extension loading and solutions --- client-sdk-references/flutter/unit-testing.mdx | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/client-sdk-references/flutter/unit-testing.mdx b/client-sdk-references/flutter/unit-testing.mdx index 63b84c5f..4d59dc4e 100644 --- a/client-sdk-references/flutter/unit-testing.mdx +++ b/client-sdk-references/flutter/unit-testing.mdx @@ -65,3 +65,12 @@ Ensure that your SQLite3 binary install on your system has extension loading ena - In the sqlite3 prompt run `PRAGMA compile_options;` - Check the output for the option `ENABLE_LOAD_EXTENSION`. - If you see `ENABLE_LOAD_EXTENSION`, it means extension loading is enabled. + +If the above steps don't work you can also confirm if extension loading is enabled by trying to load the extension in your command-line interface. + +- Run `sqlite3` in your command-line interface. +- Run `.load libpowersync.dylib` (macOS) or `.load libpowersync.so` (Linux) or `.load powersync.dll` (Windows). +- If this runs without error, then extension loading is enabled. If it fails with an error message about extension loading being disabled, then it’s not enabled in your SQLite installation. + +If it is not enabled you will have to download a compiled SQLite binary with extension loading enabled (e.g. using homebrew) or [compile SQLite](https://www.sqlite.org/howtocompile.html) with extension loading enabled and +include it in you projects folder alongside the extension. From f43d545929f5a408e79adc0726cdbcf11dc8e2be Mon Sep 17 00:00:00 2001 From: Mugi Khan Date: Wed, 30 Oct 2024 10:08:15 +0200 Subject: [PATCH 6/6] Cleanup --- client-sdk-references/flutter/unit-testing.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client-sdk-references/flutter/unit-testing.mdx b/client-sdk-references/flutter/unit-testing.mdx index 4d59dc4e..7729ae22 100644 --- a/client-sdk-references/flutter/unit-testing.mdx +++ b/client-sdk-references/flutter/unit-testing.mdx @@ -69,7 +69,7 @@ Ensure that your SQLite3 binary install on your system has extension loading ena If the above steps don't work you can also confirm if extension loading is enabled by trying to load the extension in your command-line interface. - Run `sqlite3` in your command-line interface. -- Run `.load libpowersync.dylib` (macOS) or `.load libpowersync.so` (Linux) or `.load powersync.dll` (Windows). +- Run `.load /path/to/file/libpowersync.dylib` (macOS) or `.load /path/to/file/libpowersync.so` (Linux) or `.load /path/to/file/powersync.dll` (Windows). - If this runs without error, then extension loading is enabled. If it fails with an error message about extension loading being disabled, then it’s not enabled in your SQLite installation. If it is not enabled you will have to download a compiled SQLite binary with extension loading enabled (e.g. using homebrew) or [compile SQLite](https://www.sqlite.org/howtocompile.html) with extension loading enabled and