Skip to content

Commit

Permalink
Add code examples using snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
cbullinger committed Mar 15, 2024
1 parent 29b14bf commit 6af071d
Show file tree
Hide file tree
Showing 21 changed files with 705 additions and 233 deletions.
27 changes: 12 additions & 15 deletions examples/dart/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -500,27 +500,24 @@ packages:
realm_common:
dependency: transitive
description:
name: realm_common
sha256: "29516a9e43a9e75b2e16226ce24ccd9a549d9377e69be218394d8fb84da11183"
url: "https://pub.dev"
source: hosted
version: "1.9.0"
path: "realm-dart/realm_common"
relative: true
source: path
version: "2.0.0-alpha.2-70119e393caad696c7945747e231caa4684a4079"
realm_dart:
dependency: "direct main"
description:
name: realm_dart
sha256: "55cf02d26b0775e79570cf0ba6e4036a10c66250b99cb8f1ee41d71057206a7f"
url: "https://pub.dev"
source: hosted
version: "1.9.0"
path: "realm-dart/realm_dart"
relative: true
source: path
version: "2.0.0-alpha.2-70119e393caad696c7945747e231caa4684a4079"
realm_generator:
dependency: transitive
description:
name: realm_generator
sha256: "6d26ca214aad1b49f37a1a86f3216f19a25657d38d82cefb16551a35aa316ec1"
url: "https://pub.dev"
source: hosted
version: "1.9.0"
path: "realm-dart/realm_generator"
relative: true
source: path
version: "2.0.0-alpha.2-70119e393caad696c7945747e231caa4684a4079"
rxdart:
dependency: transitive
description:
Expand Down
5 changes: 3 additions & 2 deletions examples/dart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ description: A simple command-line application using Realm Dart SDK.
publish_to: none

environment:
sdk: "^3.0.2"
sdk: "^3.1.0"

dependencies:
realm_dart: ^1.3.0
realm_dart:
path: ./realm-dart/realm_dart
path: ^1.8.2
dart_jsonwebtoken: ^2.4.2
faker: ^2.0.0
Expand Down
204 changes: 161 additions & 43 deletions examples/dart/test/data_types_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class _RealmValueExample {
@Indexed()
late RealmValue singleAnyValue;
late List<RealmValue> listOfMixedAnyValues;
late Map<String, RealmValue> mapOfMixedAnyValues;
}

// :snippet-end:
Expand Down Expand Up @@ -151,6 +152,7 @@ main() {
final object = UuidPrimaryKey(myId);
// :snippet-end:
expect(myId.toString(), isA<String>());
expect(myId, object.id);
});
test('ObjectId', () {
// :snippet-start: objectid-use
Expand All @@ -159,54 +161,170 @@ main() {
// :snippet-end:
expect(object.id.toString(), isA<String>());
});
test("RealmValue - RealmValue.from()", () {
// :snippet-start: realm-value-from
final realm = Realm(Configuration.local([RealmValueExample.schema]));

realm.write(() {
realm.addAll([
RealmValueExample(
singleAnyValue: RealmValue.from(1),
listOfMixedAnyValues: [Uuid.v4(), "abc", 123].map(RealmValue.from)),
RealmValueExample(
singleAnyValue: RealmValue.nullValue(),
listOfMixedAnyValues: ["abc", 123].map(RealmValue.from))
]);
});
// :snippet-end:
group('RealmValue - ', () {
test("RealmValue.from()", () {
// :snippet-start: realm-value-from
final realm = Realm(Configuration.local([RealmValueExample.schema]));

expect(
realm.query<RealmValueExample>("singleAnyValue.@type == 'int'").first,
isNotNull);
expect(
realm.query<RealmValueExample>("singleAnyValue.@type == 'Null'").first,
isNotNull);
cleanUpRealm(realm);
});
test("RealmValue - RealmValue.type and RealmValue.value", () {
final realm = Realm(Configuration.local([RealmValueExample.schema]));
realm.write(() {
realm.addAll([
RealmValueExample(
realm.write(() {
var anyValue = realm.add(RealmValueExample(
singleAnyValue: RealmValue.from(1),
listOfMixedAnyValues: [Uuid.v4(), "abc", 123].map(RealmValue.from)),
RealmValueExample(
listOfMixedAnyValues: [Uuid.v4(), 'abc', 123].map(RealmValue.from),
mapOfMixedAnyValues: {
'1': RealmValue.from(123),
'2': RealmValue.from('abc')
}));

// Use 'RealmValue.nullValue()' to set null values
var anyValueNull = realm.add(RealmValueExample(
singleAnyValue: RealmValue.nullValue(),
listOfMixedAnyValues: ["abc", 123].map(RealmValue.from))
]);
listOfMixedAnyValues: [null, null].map(RealmValue.from),
mapOfMixedAnyValues: {'null': RealmValue.nullValue()}));

// :remove-start:
expect(anyValue.singleAnyValue.type, RealmValueType.int);
expect(anyValue.listOfMixedAnyValues[1].value.toString(), 'abc');
expect(
anyValue.mapOfMixedAnyValues.containsValue(RealmValue.from('abc')),
true);
expect(anyValueNull.singleAnyValue.value, null);
expect(anyValueNull.listOfMixedAnyValues[0].value, null);
expect(anyValueNull.mapOfMixedAnyValues.containsValue(null), true);
});
// :remove-end:
// :snippet-end:
cleanUpRealm(realm);
});
var calledCount = 0;
// :snippet-start: realm-value-type-value
final data = realm.all<RealmValueExample>();
for (var obj in data) {
if (obj.singleAnyValue.type == int) {
print(obj.singleAnyValue.value.toString());
calledCount++; // :remove:
test("RealmValueType and RealmValue.value", () {
final realm = Realm(Configuration.local([RealmValueExample.schema]));
realm.write(() {
realm.addAll([
RealmValueExample(
singleAnyValue: RealmValue.from(1),
listOfMixedAnyValues:
[Uuid.v4(), 'abc', 123].map(RealmValue.from),
mapOfMixedAnyValues: {
'1': RealmValue.from(123),
'2': RealmValue.from('abc')
}),
RealmValueExample(
singleAnyValue: RealmValue.nullValue(),
listOfMixedAnyValues: [null, null].map(RealmValue.from),
mapOfMixedAnyValues: {'null': RealmValue.nullValue()})
]);
});
var calledCount = 0;
// :snippet-start: realm-value-type-value
final data = realm.all<RealmValueExample>();
for (var obj in data) {
switch (obj.singleAnyValue.type) {
// Use RealmValueType data type enums
case RealmValueType.int:
print('Int value: ${obj.singleAnyValue.value}');
calledCount++; // :remove:
break;
case RealmValueType.string:
print('String value: ${obj.singleAnyValue.value}');
break;
// Handle additional cases ...
default:
print('Unhandled type: ${obj.singleAnyValue.type}');
}
}
}
// :snippet-end:
expect(calledCount, 1);
cleanUpRealm(realm);
expect(calledCount, 1);
// :snippet-end:
// :snippet-start: realm-value-runtime-type
for (var obj in data) {
for (var mixedValue in obj.listOfMixedAnyValues) {
var actualValue = mixedValue.value;
// Use RealmValue.value.runtimeType to access stored value at runtime
if (actualValue == null) {
print('Null value');
} else {
switch (actualValue.runtimeType) {
case const (int):
print('Int value: $actualValue');
calledCount++; // :remove:
break;
case const (String):
print('String value: $actualValue');
break;
// Add cases for other expected types...
default:
print(
'Unhandled type: ${actualValue.runtimeType} with value $actualValue');
}
}
}
}
// :snippet-end:
expect(calledCount, 2);
cleanUpRealm(realm);
});
test('Nested collections of mixed data', () {
final realm = Realm(Configuration.local([RealmValueExample.schema]));

// :snippet-start: realm-value-nested-collections
final singleAnyValue = RealmValue.from(1);
final listOfAnyValue = RealmValue.from([singleAnyValue]);
final mapOfAnyValue = RealmValue.from({
'1': singleAnyValue,
'2': singleAnyValue,
'3': RealmValue.nullValue()
});

realm.write(() {
var collectionsOfMixed = realm.add(RealmValueExample(
singleAnyValue: singleAnyValue,
listOfMixedAnyValues: [singleAnyValue, singleAnyValue],
mapOfMixedAnyValues: {'key': singleAnyValue}));
var nestedCollectionsOfMixed = realm.add(RealmValueExample(
singleAnyValue: singleAnyValue,
listOfMixedAnyValues: [
RealmValue.from([
listOfAnyValue,
RealmValue.from([
listOfAnyValue,
RealmValue.from([
listOfAnyValue, mapOfAnyValue, singleAnyValue
])
]),
])
],
mapOfMixedAnyValues: {
'key': RealmValue.from({
'nestedKey_1': RealmValue.from({mapOfAnyValue}),
'nestedKey_2': RealmValue.from({
'nestedNestedKey_1': RealmValue.from({listOfAnyValue}),
'nestedNestedKey_2': RealmValue.from({singleAnyValue})
})
})
}));
// :snippet-end:
expect(collectionsOfMixed.singleAnyValue.value, 1);
expect(nestedCollectionsOfMixed.singleAnyValue.value, 1);
expect(collectionsOfMixed.listOfMixedAnyValues[0].type,
RealmValueType.int);
expect(collectionsOfMixed.listOfMixedAnyValues[0].value,
1);
expect(nestedCollectionsOfMixed.listOfMixedAnyValues[0].type,
RealmValueType.list);
expect(
nestedCollectionsOfMixed.listOfMixedAnyValues[0]
.asList()
.first
.asList()
.first
.value,
1);
expect(collectionsOfMixed.mapOfMixedAnyValues.containsKey('key'), true);
expect(
nestedCollectionsOfMixed.mapOfMixedAnyValues['key']?.asMap().containsKey('nestedKey_1'),
true);
});
cleanUpRealm(realm);
});
});
test('DateTime', () {
final config = Configuration.local([Vehicle.schema]);
Expand Down Expand Up @@ -266,7 +384,7 @@ main() {
// Query RealmList with Realm Query Language
final playersWithBodyArmor =
realm.query<Player>("inventory.name == \$0", ['body armor']);
print("LEN " + playersWithBodyArmor.length.toString());
print("LEN ${playersWithBodyArmor.length}");
// :snippet-end:
expect(brave, 'brave');
expect(elvishSword.name, 'elvish sword');
Expand Down
Loading

0 comments on commit 6af071d

Please sign in to comment.