From 4514df9f954a39d13d1aa353cd28b001e6c06031 Mon Sep 17 00:00:00 2001 From: Guillaume Bernos Date: Wed, 11 Sep 2024 16:03:42 +0200 Subject: [PATCH] test --- .../example/dataconnect/connector/queries.gql | 7 ++ .../example/integration_test/query_e2e.dart | 46 ++++----- .../example/lib/generated/list_persons.dart | 97 +++++++++++++++++++ .../example/lib/generated/movies.dart | 28 +++--- 4 files changed, 144 insertions(+), 34 deletions(-) create mode 100644 packages/firebase_data_connect/firebase_data_connect/example/lib/generated/list_persons.dart diff --git a/packages/firebase_data_connect/firebase_data_connect/example/dataconnect/connector/queries.gql b/packages/firebase_data_connect/firebase_data_connect/example/dataconnect/connector/queries.gql index e44d279ba049..6759f1a165be 100644 --- a/packages/firebase_data_connect/firebase_data_connect/example/dataconnect/connector/queries.gql +++ b/packages/firebase_data_connect/firebase_data_connect/example/dataconnect/connector/queries.gql @@ -19,6 +19,13 @@ query ListMoviesByPartialTitle($input: String!) @auth(level: PUBLIC) { } } +query ListPersons @auth(level: USER) { + person { + id + name + } +} + # List subset of fields for users # query ListUsers @auth(level: PUBLIC) { # users { diff --git a/packages/firebase_data_connect/firebase_data_connect/example/integration_test/query_e2e.dart b/packages/firebase_data_connect/firebase_data_connect/example/integration_test/query_e2e.dart index 986c6f5c6533..12c07b565509 100644 --- a/packages/firebase_data_connect/firebase_data_connect/example/integration_test/query_e2e.dart +++ b/packages/firebase_data_connect/firebase_data_connect/example/integration_test/query_e2e.dart @@ -54,51 +54,51 @@ void runQueryTests() { expect(result.movies[0].title, 'The Matrix'); }); - testWidgets('can add a person', (WidgetTester tester) async { + testWidgets('can add a director to a movie', (WidgetTester tester) async { MutationRef ref = MoviesConnector.instance.addPerson.ref( name: 'Keanu Reeves', - addPersonVariables: AddPersonVariables( - name: 'Keanu Reeves', - ), ); await ref.execute(); + final personId = + (await MoviesConnector.instance.listPersons.ref().execute()) + .data + .people[0] + .id; + final value = await MoviesConnector.instance.listMovies.ref().execute(); final result = value.data; - expect(result.movies.length, 1); - }); + expect(result.movies.length, 0); - testWidgets('can add a director to a movie', (WidgetTester tester) async { - MutationRef ref = MoviesConnector.instance.addPerson.ref( - name: 'Keanu Reeves', - addPersonVariables: AddPersonVariables( - name: 'Keanu Reeves', - ), + ref = MoviesConnector.instance.createMovie.ref( + genre: 'Action', + title: 'The Matrix', + releaseYear: 1999, + rating: 4.5, ); await ref.execute(); - final value = await MoviesConnector.instance.listMovies.ref().execute(); - final result = value.data; - expect(result.movies.length, 1); + final value2 = + await MoviesConnector.instance.listMovies.ref().execute(); + final result2 = value2.data; + expect(result2.movies.length, 1); - final movieId = result.movies[0].id; + final movieId = result2.movies[0].id; ref = MoviesConnector.instance.addDirectorToMovie.ref( movieId: movieId, - addDirectorToMovieVariables: AddDirectorToMovieVariables( - personId: AddDirectorToMovieVariablesPersonId(id: 'personId'), - ), + personId: AddDirectorToMovieVariablesPersonId(id: personId), ); await ref.execute(); - final value2 = + final value3 = await MoviesConnector.instance.listMovies.ref().execute(); - final result2 = value2.data; - expect(result2.movies.length, 1); - expect(result2.movies[0].directed_by.length, 1); + final result3 = value3.data; + expect(result3.movies.length, 1); + expect(result3.movies[0].directed_by.length, 1); }); testWidgets('can add a director to a movie using id', diff --git a/packages/firebase_data_connect/firebase_data_connect/example/lib/generated/list_persons.dart b/packages/firebase_data_connect/firebase_data_connect/example/lib/generated/list_persons.dart new file mode 100644 index 000000000000..bda2b336b51b --- /dev/null +++ b/packages/firebase_data_connect/firebase_data_connect/example/lib/generated/list_persons.dart @@ -0,0 +1,97 @@ +part of movies; + +class ListPersons { + String name = "ListPersons"; + ListPersons({required this.dataConnect}); + + Deserializer dataDeserializer = (String json) => + ListPersonsResponse.fromJson(jsonDecode(json) as Map); + + QueryRef ref() { + return dataConnect.query(this.name, dataDeserializer, null, null); + } + + FirebaseDataConnect dataConnect; +} + +class ListPersonsPeople { + late String id; + + late String name; + + late List directed_movies; + + ListPersonsPeople.fromJson(Map json) + : id = json['id'], + name = json['name'], + directed_movies = (json['directed_movies'] as List) + .map((e) => ListPersonsPeopleDirectedMovies.fromJson(e)) + .toList() {} + + // TODO(mtewani): Fix up to create a map on the fly + Map toJson() { + Map json = {}; + + json['id'] = id; + + json['name'] = name; + + json['directed_movies'] = directed_movies.map((e) => e.toJson()).toList(); + + return json; + } + + ListPersonsPeople({ + required this.id, + required this.name, + required this.directed_movies, + }) { + // TODO(mtewani): Only show this if there are optional fields. + } +} + +class ListPersonsPeopleDirectedMovies { + late String title; + + ListPersonsPeopleDirectedMovies.fromJson(Map json) + : title = json['title'] {} + + // TODO(mtewani): Fix up to create a map on the fly + Map toJson() { + Map json = {}; + + json['title'] = title; + + return json; + } + + ListPersonsPeopleDirectedMovies({ + required this.title, + }) { + // TODO(mtewani): Only show this if there are optional fields. + } +} + +class ListPersonsResponse { + late List people; + + ListPersonsResponse.fromJson(Map json) + : people = (json['people'] as List) + .map((e) => ListPersonsPeople.fromJson(e)) + .toList() {} + + // TODO(mtewani): Fix up to create a map on the fly + Map toJson() { + Map json = {}; + + json['people'] = people.map((e) => e.toJson()).toList(); + + return json; + } + + ListPersonsResponse({ + required this.people, + }) { + // TODO(mtewani): Only show this if there are optional fields. + } +} diff --git a/packages/firebase_data_connect/firebase_data_connect/example/lib/generated/movies.dart b/packages/firebase_data_connect/firebase_data_connect/example/lib/generated/movies.dart index 5fe2a97e65bc..b8bb12526357 100644 --- a/packages/firebase_data_connect/firebase_data_connect/example/lib/generated/movies.dart +++ b/packages/firebase_data_connect/firebase_data_connect/example/lib/generated/movies.dart @@ -3,6 +3,12 @@ library movies; import 'package:firebase_data_connect/firebase_data_connect.dart'; import 'dart:convert'; +part 'list_movies.dart'; + +part 'list_movies_by_partial_title.dart'; + +part 'list_persons.dart'; + part 'add_person.dart'; part 'add_director_to_movie.dart'; @@ -11,11 +17,19 @@ part 'create_movie.dart'; part 'delete_movie.dart'; -part 'list_movies.dart'; +class MoviesConnector { + ListMovies get listMovies { + return ListMovies(dataConnect: dataConnect); + } -part 'list_movies_by_partial_title.dart'; + ListMoviesByPartialTitle get listMoviesByPartialTitle { + return ListMoviesByPartialTitle(dataConnect: dataConnect); + } + + ListPersons get listPersons { + return ListPersons(dataConnect: dataConnect); + } -class MoviesConnector { AddPerson get addPerson { return AddPerson(dataConnect: dataConnect); } @@ -32,14 +46,6 @@ class MoviesConnector { return DeleteMovie(dataConnect: dataConnect); } - ListMovies get listMovies { - return ListMovies(dataConnect: dataConnect); - } - - ListMoviesByPartialTitle get listMoviesByPartialTitle { - return ListMoviesByPartialTitle(dataConnect: dataConnect); - } - static ConnectorConfig connectorConfig = ConnectorConfig( 'us-west2', 'movies',