Skip to content

Commit

Permalink
Merge pull request apache#17226 from [BEAM-14204] [Playground] Tests …
Browse files Browse the repository at this point in the history
…for example repository

* Improved test coverage of app states

* Fixed constants in example state test

* Improved test coverage, created request mocks

* Fixed tests after merge

* Added tests for the example repository, added GetExampleCodeResponse mock

* [BEAM-14204] Changed hashCode field getters
  • Loading branch information
miamihotline authored Apr 27, 2022
1 parent bb53425 commit da6acf2
Show file tree
Hide file tree
Showing 13 changed files with 802 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@
* limitations under the License.
*/

import 'package:flutter/cupertino.dart';
import 'package:playground/modules/sdk/models/sdk.dart';

class GetExampleRequestWrapper {
final String path;
final SDK sdk;

GetExampleRequestWrapper(this.path, this.sdk);

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is GetExampleRequestWrapper &&
path == other.path &&
sdk == other.sdk;

@override
int get hashCode => hashValues(path.hashCode, sdk.hashCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@
* limitations under the License.
*/

import 'package:flutter/cupertino.dart';
import 'package:playground/modules/sdk/models/sdk.dart';

class GetListOfExamplesRequestWrapper {
final SDK? sdk;
final String? category;

GetListOfExamplesRequestWrapper({required this.sdk, required this.category});

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is GetListOfExamplesRequestWrapper &&
category == other.category &&
sdk == other.sdk;

@override
int get hashCode => hashValues(category.hashCode, sdk.hashCode);
}
15 changes: 4 additions & 11 deletions playground/frontend/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ packages:
name: google_fonts
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.1"
version: "2.3.0"
googleapis_auth:
dependency: transitive
description:
Expand Down Expand Up @@ -385,13 +385,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
meta:
dependency: transitive
description:
Expand Down Expand Up @@ -690,7 +683,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.8"
version: "0.4.3"
timing:
dependency: transitive
description:
Expand Down Expand Up @@ -753,7 +746,7 @@ packages:
name: url_launcher_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.9"
version: "2.0.6"
url_launcher_windows:
dependency: transitive
description:
Expand Down Expand Up @@ -819,4 +812,4 @@ packages:
version: "3.1.0"
sdks:
dart: ">=2.15.0 <3.0.0"
flutter: ">=2.10.0"
flutter: ">=2.8.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:playground/modules/examples/repositories/example_client/example_client.dart';
import 'package:playground/modules/examples/repositories/example_repository.dart';

import '../../../../pages/playground/states/mocks/request_mock.dart';
import 'example_repository_test.mocks.dart';

@GenerateMocks([ExampleClient])
void main() {
late ExampleRepository repo;
late ExampleClient client;

setUp(
() {
client = MockExampleClient();
repo = ExampleRepository(client);
},
);

test(
'Example repository getListOfExamples should return response with categories',
() async {
when(client.getListOfExamples(kGetListOfExamplesRequestMock))
.thenAnswer((_) async => kGetListOfExamplesResponseMock);
expect(
await repo.getListOfExamples(kGetListOfExamplesRequestMock),
kGetListOfExamplesResponseMock.categories,
);
verify(client.getListOfExamples(kGetListOfExamplesRequestMock)).called(1);
},
);

test(
'Example repository getDefaultExample should return defaultExample for chosen Sdk',
() async {
when(client.getDefaultExample(kGetExampleRequestMock))
.thenAnswer((_) async => kGetExampleResponseMock);
expect(
await repo.getDefaultExample(kGetExampleRequestMock),
kGetExampleResponseMock.example,
);
verify(client.getDefaultExample(kGetExampleRequestMock)).called(1);
},
);

test(
'Example repository getExampleSource should return source code for example',
() async {
when(client.getExampleSource(kGetExampleRequestMock))
.thenAnswer((_) async => kGetExampleCodeResponseMock);
expect(
await repo.getExampleSource(kGetExampleRequestMock),
kGetExampleCodeResponseMock.code,
);
verify(client.getExampleSource(kGetExampleRequestMock)).called(1);
},
);

test(
'Example repository getExampleOutput should return output for example',
() async {
when(client.getExampleOutput(kGetExampleRequestMock))
.thenAnswer((_) async => kOutputResponseMock);
expect(
await repo.getExampleOutput(kGetExampleRequestMock),
kOutputResponseMock.output,
);
verify(client.getExampleOutput(kGetExampleRequestMock)).called(1);
},
);

test(
'Example repository getExampleLogs should return logs for example',
() async {
when(client.getExampleLogs(kGetExampleRequestMock))
.thenAnswer((_) async => kOutputResponseMock);
expect(
await repo.getExampleLogs(kGetExampleRequestMock),
kOutputResponseMock.output,
);
verify(client.getExampleLogs(kGetExampleRequestMock)).called(1);
},
);

test(
'Example repository getExampleLogs should return logs for example',
() async {
when(client.getExampleGraph(kGetExampleRequestMock))
.thenAnswer((_) async => kOutputResponseMock);
expect(
await repo.getExampleGraph(kGetExampleRequestMock),
kOutputResponseMock.output,
);
verify(client.getExampleGraph(kGetExampleRequestMock)).called(1);
},
);

test(
'Example repository getExample should return ExampleModel',
() async {
when(client.getExample(kGetExampleRequestMock))
.thenAnswer((_) async => kGetExampleResponseMock);
expect(
await repo.getExample(kGetExampleRequestMock),
kGetExampleResponseMock.example,
);
verify(client.getExample(kGetExampleRequestMock)).called(1);
},
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Mocks generated by Mockito 5.1.0 from annotations
// in playground/test/modules/editor/repository/example_repository/example_repository_test.dart.
// Do not manually edit this file.

import 'dart:async' as _i7;

import 'package:mockito/mockito.dart' as _i1;
import 'package:playground/modules/editor/repository/code_repository/code_client/output_response.dart'
as _i5;
import 'package:playground/modules/examples/repositories/example_client/example_client.dart'
as _i6;
import 'package:playground/modules/examples/repositories/models/get_example_code_response.dart'
as _i3;
import 'package:playground/modules/examples/repositories/models/get_example_request.dart'
as _i9;
import 'package:playground/modules/examples/repositories/models/get_example_response.dart'
as _i4;
import 'package:playground/modules/examples/repositories/models/get_list_of_examples_request.dart'
as _i8;
import 'package:playground/modules/examples/repositories/models/get_list_of_examples_response.dart'
as _i2;

// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
// ignore_for_file: avoid_setters_without_getters
// ignore_for_file: comment_references
// ignore_for_file: implementation_imports
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types

class _FakeGetListOfExampleResponse_0 extends _i1.Fake
implements _i2.GetListOfExampleResponse {}

class _FakeGetExampleCodeResponse_1 extends _i1.Fake
implements _i3.GetExampleCodeResponse {}

class _FakeGetExampleResponse_2 extends _i1.Fake
implements _i4.GetExampleResponse {}

class _FakeOutputResponse_3 extends _i1.Fake implements _i5.OutputResponse {}

/// A class which mocks [ExampleClient].
///
/// See the documentation for Mockito's code generation for more information.
class MockExampleClient extends _i1.Mock implements _i6.ExampleClient {
MockExampleClient() {
_i1.throwOnMissingStub(this);
}

@override
_i7.Future<_i2.GetListOfExampleResponse> getListOfExamples(
_i8.GetListOfExamplesRequestWrapper? request) =>
(super.noSuchMethod(Invocation.method(#getListOfExamples, [request]),
returnValue: Future<_i2.GetListOfExampleResponse>.value(
_FakeGetListOfExampleResponse_0()))
as _i7.Future<_i2.GetListOfExampleResponse>);
@override
_i7.Future<_i3.GetExampleCodeResponse> getExampleSource(
_i9.GetExampleRequestWrapper? request) =>
(super.noSuchMethod(Invocation.method(#getExampleSource, [request]),
returnValue: Future<_i3.GetExampleCodeResponse>.value(
_FakeGetExampleCodeResponse_1()))
as _i7.Future<_i3.GetExampleCodeResponse>);
@override
_i7.Future<_i4.GetExampleResponse> getDefaultExample(
_i9.GetExampleRequestWrapper? request) =>
(super.noSuchMethod(Invocation.method(#getDefaultExample, [request]),
returnValue: Future<_i4.GetExampleResponse>.value(
_FakeGetExampleResponse_2()))
as _i7.Future<_i4.GetExampleResponse>);
@override
_i7.Future<_i4.GetExampleResponse> getExample(
_i9.GetExampleRequestWrapper? request) =>
(super.noSuchMethod(Invocation.method(#getExample, [request]),
returnValue: Future<_i4.GetExampleResponse>.value(
_FakeGetExampleResponse_2()))
as _i7.Future<_i4.GetExampleResponse>);
@override
_i7.Future<_i5.OutputResponse> getExampleOutput(
_i9.GetExampleRequestWrapper? request) =>
(super.noSuchMethod(Invocation.method(#getExampleOutput, [request]),
returnValue:
Future<_i5.OutputResponse>.value(_FakeOutputResponse_3()))
as _i7.Future<_i5.OutputResponse>);
@override
_i7.Future<_i5.OutputResponse> getExampleLogs(
_i9.GetExampleRequestWrapper? request) =>
(super.noSuchMethod(Invocation.method(#getExampleLogs, [request]),
returnValue:
Future<_i5.OutputResponse>.value(_FakeOutputResponse_3()))
as _i7.Future<_i5.OutputResponse>);
@override
_i7.Future<_i5.OutputResponse> getExampleGraph(
_i9.GetExampleRequestWrapper? request) =>
(super.noSuchMethod(Invocation.method(#getExampleGraph, [request]),
returnValue:
Future<_i5.OutputResponse>.value(_FakeOutputResponse_3()))
as _i7.Future<_i5.OutputResponse>);
}
Loading

0 comments on commit da6acf2

Please sign in to comment.