Skip to content

Commit

Permalink
Generate snippets from .ts test file
Browse files Browse the repository at this point in the history
  • Loading branch information
cbullinger committed May 21, 2024
1 parent 41129af commit 6e29ca1
Show file tree
Hide file tree
Showing 38 changed files with 1,323 additions and 0 deletions.
892 changes: 892 additions & 0 deletions examples/node/v12/__tests__/realm-query-language.test.ts

Large diffs are not rendered by default.

71 changes: 71 additions & 0 deletions examples/node/v12/__tests__/rql-data-models.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import Realm, { BSON } from "realm";
import { Item, Project, Office, Address } from "./models/rql-data-models.ts";

describe("Test RQL Models", () => {
let realm;
const config = { schema: [Project, Item, Office, Address] };

beforeEach(async () => {
realm = await Realm.open(config);
});

afterEach(() => {
// After each test, delete the objects and close the realm
if (realm && !realm.isClosed) {
realm.write(() => {
realm.deleteAll();
});
realm.close();
expect(realm.isClosed).toBe(true);
}
});

afterAll(() => {
Realm.deleteFile(config);
});

test("Can open realm with config", async () => {
expect(realm.isClosed).toBe(false);
});

test("Can create object of Item type", () => {
realm.write(() => {
realm.create(Item, {
_id: new BSON.ObjectId(),
name: "get coffee",
});
});
const coffeeItem = realm.objects(Item)[0];
expect(coffeeItem._id instanceof BSON.ObjectId).toBe(true);
expect(coffeeItem.name).toBe("get coffee");
expect(coffeeItem.isComplete).toBe(false);
});

test("Can create object of Project type", () => {
realm.write(() => {
const teaItem = realm.create(Item, {
_id: new BSON.ObjectId(),
name: "get tea",
});
const officeAddress = realm.create(Office, {
name: "Austin",
address: {
name: "Main Branch",
street: "123 Main St",
zipcode: 10019,
},
});
realm.create(Project, {
_id: new BSON.ObjectId(),
name: "beverages",
items: [teaItem],
projectLocation: officeAddress,
});
});
const bevProject = realm.objects(Project)[0];
expect(bevProject._id instanceof BSON.ObjectId).toBe(true);
expect(bevProject.name).toBe("beverages");
expect(bevProject.items[0].name).toBe("get tea");
expect(bevProject.projectLocation.name).toBe("Austin");
});
});
81 changes: 81 additions & 0 deletions examples/node/v12/__tests__/rql-data-models.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import Realm, { BSON } from "realm";
import { Item, Project, Office, Address } from "./models/rql-data-models.ts";

describe("Test RQL Models", () => {
let realm: Realm;
const config = { schema: [Project, Item, Office, Address] };

beforeEach(async () => {
realm = await Realm.open(config);
});

afterEach(() => {
if (realm && !realm.isClosed) {
realm.write(() => {
realm.deleteAll();
});
realm.close();
expect(realm.isClosed).toBe(true);
}
});

afterAll(() => {
Realm.deleteFile(config);
});

test("Can open realm with config", async () => {
expect(realm.isClosed).toBe(false);
});

test("Can create object of Item type", () => {
const itemId = new BSON.ObjectId();
realm.write(() => {
realm.create(Item, {
_id: itemId,
name: "get coffee",
});
});
const coffeeItem = realm.objects(Item)[0];
expect(coffeeItem._id).toEqual(itemId);
expect(coffeeItem.name).toBe("get coffee");
expect(coffeeItem.isComplete).toBe(false);
});

test("Can create object of Project type", () => {
const projectId = new BSON.ObjectId();
realm.write(() => {
// Create the tea item
const teaItem = realm.create("Item", {
_id: new BSON.ObjectId(),
name: "get tea",
});

// Create the address object
const address = {
name: "Main Branch",
street: "123 Main St",
zipcode: 10019,
};

// Create the office object
const office = realm.create("Office", {
name: "Main Office",
address: address,
});
// Create the project object
realm.create("Project", {
_id: projectId,
name: "beverages",
items: [teaItem],
projectLocation: office,
});
});

const bevProject = realm.objects(Project)[0];
expect(bevProject._id).toEqual(projectId);
expect(bevProject.name).toBe("beverages");
expect(bevProject.items[0].name).toBe("get tea");
expect(bevProject.projectLocation?.name).toBe("Main Office");
expect(bevProject.projectLocation?.address.name).toBe("Main Branch");
});
});
6 changes: 6 additions & 0 deletions examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.. code-block:: typescript
var priorityNum = 5;
// Find projects with average item `priority` above 5.
"items.@avg.priority > $0", priorityNum
// Find projects where maximum `priority` of all items is 5.
"items.@max.priority < $0", priorityNum
// Find projects where minimum `priority` of all items is 5.
"items.@min.priority > $0", priorityNum
// Find projects with more than 5 items.
"items.@count > $0", 5
// Find projects with item `progressMinutes` greater than 100.
"items.@sum.progressMinutes > $0", 100
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. code-block:: typescript
"progressMinutes * priority == 90"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.. code-block:: typescript
// Find items that are referenced by multiple projects
"projects.@count > 1"
// Find items that are not referenced by any project
"@links.Project.items.@count == 0"
// Find items that belong to a project where the average item has
// been worked on for at least 5 minutes
"@links.Project.items.items.@avg.progressMinutes > 10"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.. code-block:: typescript
// Find items that are not referenced by another object of any type
"@links.@count == 0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. code-block:: typescript
// Find items that belong to a project with a quota
// less than 10 (using '@links').
"@links.Project.items.quota < 10"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.. code-block:: typescript
// Find items where ANY project that references the item
// has a quota greater than 10.
"ANY @links.Project.items.quota > 10"
// Find items where ALL projects that reference the item
// have a quota less than 5.
"ALL @links.Project.items.quota < 5"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. code-block:: typescript
// Find items that belong to a project with a quota greater than 10
// (using 'LinkingObjects').
"projects.quota > 10"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. code-block:: typescript
// Find items with a `priority` greater than 3.
"2 * priority > 6" // `priority > 3`
"priority >= 2 * (2 - 1) + 2" // `priority >= 4`
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.. code-block:: typescript
// Compare `priority` values against a threshold value.
"priority > $0", 5
// Compare `progressMinutes` values against a threshold value.
"progressMinutes > $0", 120
// Compare `assignee` values to `null` value.
"assignee == $0", null
// Compare `priority` values against an inclusive range of values.
"priority BETWEEN { $0 , $1 }", 1, 5
// Compare `progressMinutes` values against any of the listed values.
"progressMinutes IN { $0, $1, $2 }", 10, 30, 60
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. code-block:: typescript
var lastYear = new Date(1577883184000); // Unix timestamp in ms
var thisYear = new Date("2021-01-01@17:30:15:0"); // DateTime in UTC
var today = new Date("April 01, 2021 03:24:00"); // Alternate DateTime format
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. code-block:: typescript
// Find to-do items completed before today's date.
"dateCompleted < $0", today
// Find to-do items completed this year until today.
"dateCompleted > $0 AND dateCompleted < $1", thisYear, today
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. code-block:: typescript
"projectLocation.address.zipcode == 10019"
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.. code-block:: typescript
// Find `comments` dictionary properties with key 'status'.
"comments.@keys == $0", "status"
// Find `comments` dictionary properties with key 'status'
// and value 'On track'.
"comments['status'] == $0", "On track"
// Find `comments` dictionary properties with
// more than one key-value pair.
"comments.@count > $0", 1
// Find `comments` dictionary properties where ANY
// values are of type 'string`.
"ANY comments.@type == 'string'"
"comments.@type == 'string'" // (Equivalent - ANY is implied.)
// Find `comments` dictionary properties where ALL
// values are of type 'int'.
"ALL comments.@type == 'int'"
// Find `comments` dictionary properties where NO
// values are of type 'int'.
"NONE comments.@type == 'int'"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. code-block:: typescript
"oid(631a072f75120729dc9223d9) IN items._id"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.. code-block:: typescript
const ids = [
new BSON.ObjectId("631a072f75120729dc9223d9"),
new BSON.ObjectId("631a0737c98f89f5b81cd24d"),
new BSON.ObjectId("631a073c833a34ade21db2b2"),
];
const parameterizedQuery = realm.objects(Item).filtered("_id IN $0", ids);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. code-block:: typescript
"priority IN {0, 1, 2}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. code-block:: typescript
// Find all items assigned to Ali AND marked completed.
"assignee == $0 AND isComplete == $1", "Ali", true
// Find all items assigned to Alex OR to Ali.
"assignee == $0 OR assignee == $1", "Alex", "Ali"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.. code-block:: typescript
// comparison to language null pointer
"assignee == $0", null
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. code-block:: typescript
"assignee == nil"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. code-block:: typescript
"_id == $0", oidValue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. code-block:: typescript
"_id == oid(6001c033600510df3bbfd864)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.. code-block:: typescript
// Include one parameter with `$0`.
"progressMinutes > 1 AND assignee == $0", "Ali"
// Include multiple parameters using ascending integers,
// starting at`$0`.
"progressMinutes > $0 AND assignee == $1", 1, "Alex"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. code-block:: typescript
const expression = "priority == 1";
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.. code-block:: typescript
// Find items with 'write' in the name.
"name TEXT $0", "write"
// Use '-' to exclude:
// Find items with 'write' but not 'tests' in the name.
"name TEXT $0", "write -tests"
// Use '*' to match any suffix characters:
// Find items starting with 'wri-'.
"name TEXT $0", "wri*"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. code-block:: typescript
"progressMinutes > 1 AND assignee == 'Ali'"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.. code-block:: typescript
// Find projects with no complete items.
"NONE items.isComplete == $0", true
// Find projects that contain an item with priority 10.
"ANY items.priority == $0", 10
// Find projects that only contain completed items.
"ALL items.isComplete == $0", true
// Find projects with at least one item assigned to
// either Alex or Ali.
"ANY items.assignee IN { $0 , $1 }", "Alex", "Ali"
// Projects with no items assigned to either Alex or Ali.
"NONE items.assignee IN { $0 , $1 }", "Alex", "Ali"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. code-block:: typescript
const items = realm.objects(Item);
// Get all items where 'priority' property is 7 or more.
const importantItems = items.filtered("priority >= $0", 7);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. code-block:: typescript
"assignee == 'Ali' SORT(priority DESC) DISTINCT(name) LIMIT(5)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.. code-block:: typescript
// Find projects whose name starts with the letter 'e'
// (case-insensitive).
"name BEGINSWITH[c] $0", "e"
// Find projects whose name contains the letters 'ie'
// (case-sensitive).
"name CONTAINS $0", "ie"
Loading

0 comments on commit 6e29ca1

Please sign in to comment.