Skip to content

Commit

Permalink
feat(cloud_firestore_odm_generator): Support all serializable types (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexios80 authored Oct 10, 2023
1 parent 925f560 commit f4c21f8
Show file tree
Hide file tree
Showing 25 changed files with 4,870 additions and 1,839 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ targets:
- lib/**
options:
create_field_map: true
create_per_field_to_json: true
source_gen|combining_builder:
options:
ignore_for_file:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:async';
import 'package:async/async.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cloud_firestore_odm_example/integration.dart';
import 'package:cloud_firestore_odm_example/integration/enums.dart';
import 'package:cloud_firestore_odm_example/movie.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -975,4 +976,81 @@ void main() {
});
});
});

group('enums', () {
test('enumValue', () async {
final collection = await initializeTest(EnumsCollectionReference());

await collection.add(Enums(id: 'A'));
await collection.add(Enums(id: 'B', enumValue: TestEnum.two));
await collection.add(Enums(id: 'C', enumValue: TestEnum.two));

final querySnap =
await collection.whereEnumValue(isEqualTo: TestEnum.two).get();

expect(
querySnap.docs.map((e) => e.data.id),
['B', 'C'],
);
});

test('nullable enumValue', () async {
final collection = await initializeTest(EnumsCollectionReference());

await collection.add(Enums(id: 'A'));
await collection.add(Enums(id: 'B', nullableEnumValue: TestEnum.two));
await collection.add(Enums(id: 'C', nullableEnumValue: TestEnum.two));

final querySnap = await collection
.whereNullableEnumValue(isEqualTo: TestEnum.two)
.get();

expect(
querySnap.docs.map((e) => e.data.id),
['B', 'C'],
);
});

test('enumList', () async {
final collection = await initializeTest(EnumsCollectionReference());

await collection
.add(Enums(id: 'A', enumList: [TestEnum.one, TestEnum.three]));
await collection
.add(Enums(id: 'B', enumList: [TestEnum.two, TestEnum.three]));
await collection
.add(Enums(id: 'C', enumList: [TestEnum.two, TestEnum.three]));

final querySnap =
await collection.whereEnumList(arrayContains: TestEnum.two).get();

expect(
querySnap.docs.map((e) => e.data.id),
['B', 'C'],
);
});

test('nullable enumList', () async {
final collection = await initializeTest(EnumsCollectionReference());

await collection.add(
Enums(id: 'A', nullableEnumList: [TestEnum.one, TestEnum.three]),
);
await collection.add(
Enums(id: 'B', nullableEnumList: [TestEnum.two, TestEnum.three]),
);
await collection.add(
Enums(id: 'C', nullableEnumList: [TestEnum.two, TestEnum.three]),
);

final querySnap = await collection
.whereNullableEnumList(arrayContains: TestEnum.two)
.get();

expect(
querySnap.docs.map((e) => e.data.id),
['B', 'C'],
);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ void main() {
});
});

test('supports Durations', () async {
final ref = await initializeTest(durationQueryRef);

await ref.add(DurationQuery(const Duration(days: 1)));
await ref.add(DurationQuery(const Duration(days: 2)));
await ref.add(DurationQuery(const Duration(days: 3)));

final snapshot =
await ref.orderByDuration(startAt: const Duration(days: 2)).get();

expect(snapshot.docs.length, 2);

expect(snapshot.docs[0].data.duration, const Duration(days: 2));
expect(snapshot.docs[1].data.duration, const Duration(days: 3));
});

test('supports DateTimes', () async {
final ref = await initializeTest(dateTimeQueryRef);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,6 @@ class EmptyModel {
@Collection<EmptyModel>('firestore-example-app/test/config')
final emptyModelRef = EmptyModelCollectionReference();

@Collection<ManualJson>('root')
class ManualJson {
ManualJson(this.value);

factory ManualJson.fromJson(Map<String, Object?> json) {
return ManualJson(json['value']! as String);
}

final String value;

Map<String, Object?> toJson() => {'value': value};
}

@Collection<AdvancedJson>('firestore-example-app/test/advanced')
@JsonSerializable(fieldRename: FieldRename.snake)
class AdvancedJson {
Expand Down
Loading

0 comments on commit f4c21f8

Please sign in to comment.