Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fdc): Fix NativeToJSON #13401

Merged
merged 10 commits into from
Sep 26, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ query ListMovies @auth(level: USER) {
directed_by: people_via_DirectedBy {
name
}
rating
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ void runGenerationTest() {
rating: 4.5,
);
expect(ref, isNotNull);
expect(ref.execute, isNotNull);
expect(ref.build().execute, isNotNull);
});

testWidgets('should have generated correct QueryRef',
(WidgetTester tester) async {
final ref = MoviesConnector.instance.listMovies.ref();
expect(ref, isNotNull);
expect(ref.execute, isNotNull);
expect(ref.build().execute, isNotNull);
});

testWidgets('should have generated correct MutationRef using name',
Expand All @@ -53,7 +53,7 @@ void runGenerationTest() {
name: 'Keanu Reeves',
);
expect(ref, isNotNull);
expect(ref.execute, isNotNull);
expect(ref.build().execute, isNotNull);
});

testWidgets('should have generated correct MutationRef using nested id',
Expand All @@ -63,7 +63,7 @@ void runGenerationTest() {
personId: AddDirectorToMovieVariablesPersonId(id: 'personId'),
);
expect(ref, isNotNull);
expect(ref.execute, isNotNull);
expect(ref.build().execute, isNotNull);
});
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void runListenTests() {
testWidgets('should be able to listen to the list of movies',
(WidgetTester tester) async {
final initialValue =
await MoviesConnector.instance.listMovies.ref().execute();
await MoviesConnector.instance.listMovies.ref().build().execute();
expect(initialValue.data.movies.length, 0,
reason: 'Initial movie list should be empty');

Expand All @@ -31,6 +31,7 @@ void runListenTests() {

final listener = MoviesConnector.instance.listMovies
.ref()
.build()
.subscribe()
.listen((value) {
final movies = value.data.movies;
Expand Down Expand Up @@ -60,9 +61,10 @@ void runListenTests() {
releaseYear: 1999,
rating: 4.5,
)
.build()
.execute();

await MoviesConnector.instance.listMovies.ref().execute();
await MoviesConnector.instance.listMovies.ref().build().execute();

// Wait for the listener to receive the movie update
final bool hasListenerReceived = await hasBeenListened.future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import 'package:firebase_data_connect_example/generated/movies.dart';
import 'package:flutter_test/flutter_test.dart';

Future<void> deleteAllMovies() async {
final value = await MoviesConnector.instance.listMovies.ref().execute();
final value =
await MoviesConnector.instance.listMovies.ref().build().execute();
final result = value.data;
for (var movie in result.movies) {
await MoviesConnector.instance.deleteMovie.ref(id: movie.id).execute();
await MoviesConnector.instance.deleteMovie
.ref(id: movie.id)
.build()
.execute();
}
}

Expand All @@ -23,98 +27,112 @@ void runQueryTests() {
});

testWidgets('can query', (WidgetTester tester) async {
final value = await MoviesConnector.instance.listMovies.ref().execute();
final value =
await MoviesConnector.instance.listMovies.ref().build().execute();

final result = value.data;
expect(result.movies.length, 0);
});

testWidgets('can add a movie', (WidgetTester tester) async {
MutationRef ref = MoviesConnector.instance.createMovie.ref(
genre: 'Action',
title: 'The Matrix',
releaseYear: 1999,
rating: 4.5,
);
MutationRef ref = MoviesConnector.instance.createMovie
.ref(
genre: 'Action',
title: 'The Matrix',
releaseYear: 1999,
rating: 4.5,
)
.build();

await ref.execute();

final value = await MoviesConnector.instance.listMovies.ref().execute();
final value =
await MoviesConnector.instance.listMovies.ref().build().execute();
final result = value.data;
expect(result.movies.length, 1);
expect(result.movies[0].title, 'The Matrix');
});

testWidgets('can add a director to a movie', (WidgetTester tester) async {
MutationRef ref = MoviesConnector.instance.addPerson.ref(
name: 'Keanu Reeves',
);
MutationRef ref = MoviesConnector.instance.addPerson
.ref(
name: 'Keanu Reeves',
)
.build();

await ref.execute();

final personId =
(await MoviesConnector.instance.listPersons.ref().execute())
(await MoviesConnector.instance.listPersons.ref().build().execute())
.data
.people[0]
.id;

final value = await MoviesConnector.instance.listMovies.ref().execute();
final value =
await MoviesConnector.instance.listMovies.ref().build().execute();
final result = value.data;
expect(result.movies.length, 0);

ref = MoviesConnector.instance.createMovie.ref(
genre: 'Action',
title: 'The Matrix',
releaseYear: 1999,
rating: 4.5,
);
ref = MoviesConnector.instance.createMovie
.ref(
genre: 'Action',
title: 'The Matrix',
releaseYear: 1999,
rating: 4.5,
)
.build();

await ref.execute();

final value2 =
await MoviesConnector.instance.listMovies.ref().execute();
await MoviesConnector.instance.listMovies.ref().build().execute();
final result2 = value2.data;
expect(result2.movies.length, 1);

final movieId = result2.movies[0].id;

ref = MoviesConnector.instance.addDirectorToMovie.ref(
movieId: movieId,
personId: AddDirectorToMovieVariablesPersonId(id: personId),
);
ref = MoviesConnector.instance.addDirectorToMovie
.ref(
movieId: movieId,
personId: AddDirectorToMovieVariablesPersonId(id: personId),
)
.build();

await ref.execute();

final value3 =
await MoviesConnector.instance.listMovies.ref().execute();
await MoviesConnector.instance.listMovies.ref().build().execute();
final result3 = value3.data;
expect(result3.movies.length, 1);
expect(result3.movies[0].directed_by.length, 1);
expect(result3.movies[0].directed_by[0].name, 'Keanu Reeves');
});

testWidgets('can delete a movie', (WidgetTester tester) async {
MutationRef ref = MoviesConnector.instance.createMovie.ref(
genre: 'Action',
title: 'The Matrix',
releaseYear: 1999,
rating: 4.5,
);
MutationRef ref = MoviesConnector.instance.createMovie
.ref(
genre: 'Action',
title: 'The Matrix',
releaseYear: 1999,
rating: 4.5,
)
.build();

await ref.execute();

final value = await MoviesConnector.instance.listMovies.ref().execute();
final value =
await MoviesConnector.instance.listMovies.ref().build().execute();
final result = value.data;
expect(result.movies.length, 1);

final movieId = result.movies[0].id;

ref = MoviesConnector.instance.deleteMovie.ref(id: movieId);
ref = MoviesConnector.instance.deleteMovie.ref(id: movieId).build();

await ref.execute();

final value2 =
await MoviesConnector.instance.listMovies.ref().execute();
await MoviesConnector.instance.listMovies.ref().build().execute();
final result2 = value2.data;
expect(result2.movies.length, 0);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
part of movies;

class AddDateAndTimestamp {
String name = "addDateAndTimestamp";
AddDateAndTimestamp({required this.dataConnect});
class AddDateAndTimestampVariablesBuilder {
DateTime date;
Timestamp timestamp;

FirebaseDataConnect dataConnect;

AddDateAndTimestampVariablesBuilder(
this.dataConnect, {
required DateTime this.date,
required Timestamp this.timestamp,
});
Deserializer<AddDateAndTimestampData> dataDeserializer = (String json) =>
AddDateAndTimestampData.fromJson(
jsonDecode(json) as Map<String, dynamic>);
Serializer<AddDateAndTimestampVariables> varsSerializer =
(AddDateAndTimestampVariables vars) => jsonEncode(vars.toJson());
MutationRef<AddDateAndTimestampData, AddDateAndTimestampVariables> ref({
required DateTime date,
required Timestamp timestamp,
}) {
MutationRef<AddDateAndTimestampData, AddDateAndTimestampVariables> build() {
AddDateAndTimestampVariables vars = AddDateAndTimestampVariables(
date: date,
timestamp: timestamp,
);

return dataConnect.mutation(
this.name, dataDeserializer, varsSerializer, vars);
"addDateAndTimestamp", dataDeserializer, varsSerializer, vars);
}
}

class AddDateAndTimestamp {
String name = "addDateAndTimestamp";
AddDateAndTimestamp({required this.dataConnect});
AddDateAndTimestampVariablesBuilder ref({
required DateTime date,
required Timestamp timestamp,
}) {
return AddDateAndTimestampVariablesBuilder(
dataConnect,
date: date,
timestamp: timestamp,
);
}

FirebaseDataConnect dataConnect;
Expand All @@ -28,6 +47,7 @@ class AddDateAndTimestamp {
class AddDateAndTimestampTimestampHolderInsert {
String id;

// TODO(mtewani): Check what happens when an optional field is retrieved from json.
AddDateAndTimestampTimestampHolderInsert.fromJson(Map<String, dynamic> json)
: id = nativeFromJson<String>(json['id']) {}

Expand All @@ -41,14 +61,13 @@ class AddDateAndTimestampTimestampHolderInsert {

AddDateAndTimestampTimestampHolderInsert({
required this.id,
}) {
// TODO: Only show this if there are optional fields.
}
});
}

class AddDateAndTimestampData {
AddDateAndTimestampTimestampHolderInsert timestampHolder_insert;

// TODO(mtewani): Check what happens when an optional field is retrieved from json.
AddDateAndTimestampData.fromJson(Map<String, dynamic> json)
: timestampHolder_insert =
AddDateAndTimestampTimestampHolderInsert.fromJson(
Expand All @@ -64,16 +83,15 @@ class AddDateAndTimestampData {

AddDateAndTimestampData({
required this.timestampHolder_insert,
}) {
// TODO: Only show this if there are optional fields.
}
});
}

class AddDateAndTimestampVariables {
DateTime date;

Timestamp timestamp;

// TODO(mtewani): Check what happens when an optional field is retrieved from json.
AddDateAndTimestampVariables.fromJson(Map<String, dynamic> json)
: date = nativeFromJson<DateTime>(json['date']),
timestamp = Timestamp.fromJson(json['timestamp']) {}
Expand All @@ -91,7 +109,5 @@ class AddDateAndTimestampVariables {
AddDateAndTimestampVariables({
required this.date,
required this.timestamp,
}) {
// TODO: Only show this if there are optional fields.
}
});
}
Loading
Loading