realm / realm-dart

Realm is a mobile database: a replacement for SQLite & ORMs.
Apache License 2.0
772 stars 86 forks source link

RDART-1062: Allow missing values (implicit null) #1736

Closed nielsenko closed 3 months ago

nielsenko commented 4 months ago

Assuming:

@RealmModel()
class _Player {
  @PrimaryKey()
  late String name;
  _Game? game;
  final scoresByRound = <int?>[]; // null means player didn't finish
}

then the following now works:

Realm(Configuration.inMemory([Player.schema, Game.schema]));
expect(() => fromEJson<Player>({}), throwsA(isA<InvalidEJson>())); // illegal since mandatory name is missing
final p = fromEJson<Player>({'name': 'Ericsen'}); // legal, since game and scoresByRound are optional
expect(p.toEJson(), {'name': 'Ericsen', 'game': null, 'scoresByRound': <int?>[]});

Previously the call to fromEJson<Player>(ejson) would fail unless ejson was a map with shape { 'name': ..., 'game': ..., scoresByRound: ...} despite game being nullable, and scoresByRound being optional.

This PR also rectifies and oversight regarding RealmValue and Decimal128. These types are now supported without explicit calls to register.

  test('RealmValue', () {
    final r = RealmValue.int(42);
    expect(r.toEJson(), {'\$numberInt': '42'});
    expect(fromEJson<RealmValue>({'\$numberInt': '42'}), r);
  });

  test('Decimal128', () {
    final d = Decimal128.fromInt(42);
    expect(toEJson(d), {'\$numberDecimal': '+42E+0'});
    expect(fromEJson<Decimal128>({'\$numberDecimal': '42'}), d);
  });

Also, sets are now supported:

  test('Set on RealmObject', () {
    final realm = Realm(Configuration.inMemory([AllCollections.schema]));
    final o = realm.write(() => realm.add(AllCollections(intSet: {1, 2, 3})));
    expect(o.intSet, {1, 2, 3});
    final deserialized = fromEJsonString<AllCollections>(toEJsonString(o));
    // deserialized is unmanaged, so will never compare equal, but we can test properties
    expect(deserialized.intSet, o.intSet);
  });

and RealValue can be deserialized from DBRef:

    test('RealmObject', () {
      final p = Player('Christian Eriksen');
      final rv = RealmValue.from(p);
      expect(rv.toEJson(), {'\$id': 'Christian Eriksen', '\$ref': 'Player'});
      expect(fromEJson<DBRef<String>>(rv.toEJson()), isA<DBRef<String>>().having((r) => r.id, '\$id', 'Christian Eriksen'));
      expect((fromEJson<RealmValue>(rv.toEJson()).value as Player).name, p.name);
    });

Fixes: #1735 Fixes: #1737 Fixes: #1761 Fixes: #1757

coveralls-official[bot] commented 4 months ago

Pull Request Test Coverage Report for Build 9696676877

Details


Totals Coverage Status
Change from base Build 9695895354: 0.007%
Covered Lines: 5988
Relevant Lines: 6887

💛 - Coveralls
coveralls-official[bot] commented 4 months ago

Pull Request Test Coverage Report for Build 9697576923

Details


Totals Coverage Status
Change from base Build 9695895354: 0.007%
Covered Lines: 5988
Relevant Lines: 6887

💛 - Coveralls
coveralls-official[bot] commented 4 months ago

Pull Request Test Coverage Report for Build 9699703151

Details


Totals Coverage Status
Change from base Build 9695895354: 0.02%
Covered Lines: 5992
Relevant Lines: 6891

💛 - Coveralls
coveralls-official[bot] commented 4 months ago

Pull Request Test Coverage Report for Build 9710791691

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/realm_dart/lib/src/handles/native/init.dart 8 9 88.89%
<!-- Total: 29 30 96.67% -->
Totals Coverage Status
Change from base Build 9695895354: 0.02%
Covered Lines: 6000
Relevant Lines: 6900

💛 - Coveralls
coveralls-official[bot] commented 4 months ago

Pull Request Test Coverage Report for Build 9761656848

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/realm_dart/lib/src/handles/native/init.dart 13 14 92.86%
packages/ejson/lib/src/types.dart 3 5 60.0%
<!-- Total: 64 67 95.52% -->
Files with Coverage Reduction New Missed Lines %
packages/realm_dart/lib/src/configuration.dart 2 73.11%
<!-- Total: 2 -->
Totals Coverage Status
Change from base Build 9695895354: 0.003%
Covered Lines: 6019
Relevant Lines: 6923

💛 - Coveralls
coveralls-official[bot] commented 3 months ago

Pull Request Test Coverage Report for Build 10217657561

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/realm_dart/lib/src/handles/native/init.dart 21 22 95.45%
packages/ejson/lib/src/types.dart 3 5 60.0%
<!-- Total: 83 86 96.51% -->
Files with Coverage Reduction New Missed Lines %
packages/ejson/lib/src/decoding.dart 1 99.19%
<!-- Total: 1 -->
Totals Coverage Status
Change from base Build 10212460480: 0.06%
Covered Lines: 6078
Relevant Lines: 6972

💛 - Coveralls