Blitzapps / blitz-orm

GNU Affero General Public License v3.0
23 stars 9 forks source link

Sweep: Add query tests #69

Open lveillard opened 3 months ago

lveillard commented 3 months ago

Details

Context: This repo is an ORM that supports queries for surrealDB and typeDB. The tests are run for both, with the same tests, using the common BQL (Blitz query language) as the input and the output. In the mid part

Todo: Take a deep breath and find out missing query tests for different potential combinations.

As there are roleFields, and two types of linkFields (target: role, and target:relation) we need to test different features on different items.

Context files /home/loic/blitz-orm/tests/mocks/schema.ts //The data follows these rules /home/loic/blitz-orm/tests/mocks/generatedSchema.ts // This is an extended version of that schema, with some precomputed things /home/loic/blitz-orm/tests/surrealdb/mocks/data.surql //This is the original data set, translated to surrealDB /home/loic/blitz-orm/tests/typedb/mocks/data.tql //This is the same data set equivalent, for typeDB Also this is the state machine that runs the BQL to SurrealQL/TypeQL runs it and then transforms the answers back to BQL output: /home/loic/blitz-orm/src/stateMachine/query/machine.ts /home/loic/blitz-orm/src/stateMachine/query/surql/machine.ts /home/loic/blitz-orm/src/stateMachine/query/tql/machine.ts

FIles to change: /home/loic/blitz-orm/tests/unit/queries/query.ts

Instructions:

sweep-ai[bot] commented 3 months ago
Sweeping

75%

Actions

/home/loic/blitz-orm/tests/unit/queries/query.ts

--- 
+++ 
@@ -1 +1,185 @@
-// Existing test cases
+// Existing test cases
+
+// New test cases
+
+it('should query entities with nested link fields targeting relations', async () => {
+  const res = await ctx.query([
+    {
+      $thing: 'User',
+      $fields: [
+        'id',
+        'name',
+        {
+          $path: 'spaces',
+          $fields: ['id', 'name']
+        }
+      ]
+    }
+  ]);
+
+  expect(res).toEqual([
+    {
+      id: 'user-1',
+      name: 'John',
+      spaces: [
+        {
+          id: 'space-1',
+          name: 'Space 1'
+        },
+        {
+          id: 'space-2',
+          name: 'Space 2'
+        }
+      ]
+    },
+    {
+      id: 'user-2',
+      name: 'Jane',
+      spaces: [
+        {
+          id: 'space-3',
+          name: 'Space 3'
+        }
+      ]
+    }
+  ]);
+});
+
+it('should query entities with nested link fields targeting roles', async () => {
+  const res = await ctx.query([
+    {
+      $thing: 'Space',
+      $fields: [
+        'id',
+        'name',
+        {
+          $path: 'users',
+          $fields: ['id', 'name']
+        }
+      ]
+    }
+  ]);
+
+  expect(res).toEqual([
+    {
+      id: 'space-1',
+      name: 'Space 1',
+      users: [
+        {
+          id: 'user-1',
+          name: 'John'
+        }
+      ]
+    },
+    {
+      id: 'space-2',
+      name: 'Space 2',
+      users: [
+        {
+          id: 'user-1',
+          name: 'John'
+        }
+      ]
+    },
+    {
+      id: 'space-3',
+      name: 'Space 3',
+      users: [
+        {
+          id: 'user-2',
+          name: 'Jane'
+        }
+      ]
+    }
+  ]);
+});
+
+it('should query relations with nested role fields', async () => {
+  const res = await ctx.query([
+    {
+      $thing: 'Space-User',
+      $fields: [
+        {
+          $path: 'users',
+          $fields: ['id', 'name']
+        },
+        {
+          $path: 'spaces',
+          $fields: ['id', 'name']
+        }
+      ]
+    }
+  ]);
+
+  expect(res).toEqual([
+    {
+      users: {
+        id: 'user-1',
+        name: 'John'
+      },
+      spaces: {
+        id: 'space-1',
+        name: 'Space 1'
+      }
+    },
+    {
+      users: {
+        id: 'user-1',
+        name: 'John'
+      },
+      spaces: {
+        id: 'space-2',
+        name: 'Space 2'
+      }
+    },
+    {
+      users: {
+        id: 'user-2',
+        name: 'Jane'
+      },
+      spaces: {
+        id: 'space-3',
+        name: 'Space 3'
+      }
+    }
+  ]);
+});
+
+it('should combine filters, sorting, and pagination with different field types', async () => {
+  const res = await ctx.query([
+    {
+      $thing: 'User',
+      $fields: [
+        'id',
+        'name',
+        {
+          $path: 'spaces',
+          $fields: ['id', 'name'],
+          $filter: {
+            name: 'Space 1'
+          },
+          $sort: ['name'],
+          $limit: 1
+        }
+      ],
+      $filter: {
+        name: 'John'
+      },
+      $sort: ['name'],
+      $limit: 1
+    }
+  ]);
+
+  expect(res).toEqual([
+    {
+      id: 'user-1',
+      name: 'John',
+      spaces: [
+        {
+          id: 'space-1',
+          name: 'Space 1'
+        }
+      ]
+    }
+  ]);
+});

Feel free to add more details to the issue description so Sweep can better address it. Alternatively, reach out to Kevin or William for help at https://community.sweep.dev/.

Report a bug.


[!TIP] To recreate the pull request, edit the issue title or description.

This is an automated message generated by Sweep AI.