Skip to content

Commit 275f56e

Browse files
committed
add deployments tests
1 parent a2593a2 commit 275f56e

File tree

4 files changed

+574
-0
lines changed

4 files changed

+574
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import 'dart:convert';
2+
3+
import 'package:http/http.dart' as http;
4+
import 'package:mockito/annotations.dart';
5+
import 'package:mockito/mockito.dart';
6+
import 'package:raygun_cli/src/deployments/deployments_api.dart';
7+
import 'package:test/test.dart';
8+
9+
import 'deployments_api_test.mocks.dart';
10+
11+
@GenerateMocks([http.Client])
12+
void main() {
13+
group('DeploymentsApi', () {
14+
late MockClient mockClient;
15+
late DeploymentsApi deploymentsApi;
16+
17+
setUp(() {
18+
mockClient = MockClient();
19+
deploymentsApi = DeploymentsApi(mockClient);
20+
});
21+
22+
group('createDeployment', () {
23+
test('returns true when deployment creation is successful (201)',
24+
() async {
25+
final responseBody = jsonEncode({'identifier': 'test-deployment-id'});
26+
final response = http.StreamedResponse(
27+
Stream.value(utf8.encode(responseBody)),
28+
201,
29+
);
30+
31+
when(mockClient.send(any)).thenAnswer((_) async => response);
32+
33+
final result = await deploymentsApi.createDeployment(
34+
token: 'test-token',
35+
apiKey: 'test-api-key',
36+
version: '1.0.0',
37+
);
38+
39+
expect(result, true);
40+
verify(mockClient.send(any)).called(1);
41+
});
42+
43+
test('returns true with all optional parameters', () async {
44+
final responseBody = jsonEncode({'identifier': 'test-deployment-id'});
45+
final response = http.StreamedResponse(
46+
Stream.value(utf8.encode(responseBody)),
47+
200,
48+
);
49+
50+
when(mockClient.send(any)).thenAnswer((_) async => response);
51+
52+
final result = await deploymentsApi.createDeployment(
53+
token: 'test-token',
54+
apiKey: 'test-api-key',
55+
version: '1.0.0',
56+
ownerName: 'John Doe',
57+
emailAddress: 'john@example.com',
58+
comment: 'Test deployment',
59+
scmIdentifier: 'abc123',
60+
scmType: 'GitHub',
61+
);
62+
63+
expect(result, true);
64+
verify(mockClient.send(any)).called(1);
65+
});
66+
67+
test('returns false when deployment creation fails (400)', () async {
68+
final response = http.StreamedResponse(
69+
Stream.value(utf8.encode('Bad request')),
70+
400,
71+
);
72+
73+
when(mockClient.send(any)).thenAnswer((_) async => response);
74+
75+
final result = await deploymentsApi.createDeployment(
76+
token: 'test-token',
77+
apiKey: 'test-api-key',
78+
version: '1.0.0',
79+
);
80+
81+
expect(result, false);
82+
verify(mockClient.send(any)).called(1);
83+
});
84+
85+
test('handles network exceptions and returns false', () async {
86+
when(mockClient.send(any)).thenThrow(Exception('Network error'));
87+
88+
final result = await deploymentsApi.createDeployment(
89+
token: 'test-token',
90+
apiKey: 'test-api-key',
91+
version: '1.0.0',
92+
);
93+
94+
expect(result, false);
95+
verify(mockClient.send(any)).called(1);
96+
});
97+
98+
test('handles JSON decode error and returns false', () async {
99+
final response = http.StreamedResponse(
100+
Stream.value(utf8.encode('invalid json')),
101+
200,
102+
);
103+
104+
when(mockClient.send(any)).thenAnswer((_) async => response);
105+
106+
final result = await deploymentsApi.createDeployment(
107+
token: 'test-token',
108+
apiKey: 'test-api-key',
109+
version: '1.0.0',
110+
);
111+
112+
expect(result, false);
113+
verify(mockClient.send(any)).called(1);
114+
});
115+
});
116+
});
117+
}
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
// Mocks generated by Mockito 5.4.5 from annotations
2+
// in raygun_cli/test/deployments/deployments_api_test.dart.
3+
// Do not manually edit this file.
4+
5+
// ignore_for_file: no_leading_underscores_for_library_prefixes
6+
import 'dart:async' as _i3;
7+
import 'dart:convert' as _i4;
8+
import 'dart:typed_data' as _i6;
9+
10+
import 'package:http/http.dart' as _i2;
11+
import 'package:mockito/mockito.dart' as _i1;
12+
import 'package:mockito/src/dummies.dart' as _i5;
13+
14+
// ignore_for_file: type=lint
15+
// ignore_for_file: avoid_redundant_argument_values
16+
// ignore_for_file: avoid_setters_without_getters
17+
// ignore_for_file: comment_references
18+
// ignore_for_file: deprecated_member_use
19+
// ignore_for_file: deprecated_member_use_from_same_package
20+
// ignore_for_file: implementation_imports
21+
// ignore_for_file: invalid_use_of_visible_for_testing_member
22+
// ignore_for_file: must_be_immutable
23+
// ignore_for_file: prefer_const_constructors
24+
// ignore_for_file: unnecessary_parenthesis
25+
// ignore_for_file: camel_case_types
26+
// ignore_for_file: subtype_of_sealed_class
27+
28+
class _FakeResponse_0 extends _i1.SmartFake implements _i2.Response {
29+
_FakeResponse_0(Object parent, Invocation parentInvocation)
30+
: super(parent, parentInvocation);
31+
}
32+
33+
class _FakeStreamedResponse_1 extends _i1.SmartFake
34+
implements _i2.StreamedResponse {
35+
_FakeStreamedResponse_1(Object parent, Invocation parentInvocation)
36+
: super(parent, parentInvocation);
37+
}
38+
39+
/// A class which mocks [Client].
40+
///
41+
/// See the documentation for Mockito's code generation for more information.
42+
class MockClient extends _i1.Mock implements _i2.Client {
43+
MockClient() {
44+
_i1.throwOnMissingStub(this);
45+
}
46+
47+
@override
48+
_i3.Future<_i2.Response> head(Uri? url, {Map<String, String>? headers}) =>
49+
(super.noSuchMethod(
50+
Invocation.method(#head, [url], {#headers: headers}),
51+
returnValue: _i3.Future<_i2.Response>.value(
52+
_FakeResponse_0(
53+
this,
54+
Invocation.method(#head, [url], {#headers: headers}),
55+
),
56+
),
57+
) as _i3.Future<_i2.Response>);
58+
59+
@override
60+
_i3.Future<_i2.Response> get(Uri? url, {Map<String, String>? headers}) =>
61+
(super.noSuchMethod(
62+
Invocation.method(#get, [url], {#headers: headers}),
63+
returnValue: _i3.Future<_i2.Response>.value(
64+
_FakeResponse_0(
65+
this,
66+
Invocation.method(#get, [url], {#headers: headers}),
67+
),
68+
),
69+
) as _i3.Future<_i2.Response>);
70+
71+
@override
72+
_i3.Future<_i2.Response> post(
73+
Uri? url, {
74+
Map<String, String>? headers,
75+
Object? body,
76+
_i4.Encoding? encoding,
77+
}) =>
78+
(super.noSuchMethod(
79+
Invocation.method(
80+
#post,
81+
[url],
82+
{#headers: headers, #body: body, #encoding: encoding},
83+
),
84+
returnValue: _i3.Future<_i2.Response>.value(
85+
_FakeResponse_0(
86+
this,
87+
Invocation.method(
88+
#post,
89+
[url],
90+
{#headers: headers, #body: body, #encoding: encoding},
91+
),
92+
),
93+
),
94+
) as _i3.Future<_i2.Response>);
95+
96+
@override
97+
_i3.Future<_i2.Response> put(
98+
Uri? url, {
99+
Map<String, String>? headers,
100+
Object? body,
101+
_i4.Encoding? encoding,
102+
}) =>
103+
(super.noSuchMethod(
104+
Invocation.method(
105+
#put,
106+
[url],
107+
{#headers: headers, #body: body, #encoding: encoding},
108+
),
109+
returnValue: _i3.Future<_i2.Response>.value(
110+
_FakeResponse_0(
111+
this,
112+
Invocation.method(
113+
#put,
114+
[url],
115+
{#headers: headers, #body: body, #encoding: encoding},
116+
),
117+
),
118+
),
119+
) as _i3.Future<_i2.Response>);
120+
121+
@override
122+
_i3.Future<_i2.Response> patch(
123+
Uri? url, {
124+
Map<String, String>? headers,
125+
Object? body,
126+
_i4.Encoding? encoding,
127+
}) =>
128+
(super.noSuchMethod(
129+
Invocation.method(
130+
#patch,
131+
[url],
132+
{#headers: headers, #body: body, #encoding: encoding},
133+
),
134+
returnValue: _i3.Future<_i2.Response>.value(
135+
_FakeResponse_0(
136+
this,
137+
Invocation.method(
138+
#patch,
139+
[url],
140+
{#headers: headers, #body: body, #encoding: encoding},
141+
),
142+
),
143+
),
144+
) as _i3.Future<_i2.Response>);
145+
146+
@override
147+
_i3.Future<_i2.Response> delete(
148+
Uri? url, {
149+
Map<String, String>? headers,
150+
Object? body,
151+
_i4.Encoding? encoding,
152+
}) =>
153+
(super.noSuchMethod(
154+
Invocation.method(
155+
#delete,
156+
[url],
157+
{#headers: headers, #body: body, #encoding: encoding},
158+
),
159+
returnValue: _i3.Future<_i2.Response>.value(
160+
_FakeResponse_0(
161+
this,
162+
Invocation.method(
163+
#delete,
164+
[url],
165+
{#headers: headers, #body: body, #encoding: encoding},
166+
),
167+
),
168+
),
169+
) as _i3.Future<_i2.Response>);
170+
171+
@override
172+
_i3.Future<String> read(Uri? url, {Map<String, String>? headers}) =>
173+
(super.noSuchMethod(
174+
Invocation.method(#read, [url], {#headers: headers}),
175+
returnValue: _i3.Future<String>.value(
176+
_i5.dummyValue<String>(
177+
this,
178+
Invocation.method(#read, [url], {#headers: headers}),
179+
),
180+
),
181+
) as _i3.Future<String>);
182+
183+
@override
184+
_i3.Future<_i6.Uint8List> readBytes(
185+
Uri? url, {
186+
Map<String, String>? headers,
187+
}) =>
188+
(super.noSuchMethod(
189+
Invocation.method(#readBytes, [url], {#headers: headers}),
190+
returnValue: _i3.Future<_i6.Uint8List>.value(_i6.Uint8List(0)),
191+
) as _i3.Future<_i6.Uint8List>);
192+
193+
@override
194+
_i3.Future<_i2.StreamedResponse> send(_i2.BaseRequest? request) =>
195+
(super.noSuchMethod(
196+
Invocation.method(#send, [request]),
197+
returnValue: _i3.Future<_i2.StreamedResponse>.value(
198+
_FakeStreamedResponse_1(
199+
this,
200+
Invocation.method(#send, [request]),
201+
),
202+
),
203+
) as _i3.Future<_i2.StreamedResponse>);
204+
205+
@override
206+
void close() => super.noSuchMethod(
207+
Invocation.method(#close, []),
208+
returnValueForMissingStub: null,
209+
);
210+
}

0 commit comments

Comments
 (0)