Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
wip test updates for last bit of cov
Browse files Browse the repository at this point in the history
  • Loading branch information
verygoodstefan committed Oct 1, 2024
1 parent 532afcc commit da39508
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 118 deletions.
25 changes: 0 additions & 25 deletions api/packages/fluttercon_cache/test/src/fluttercon_cache_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:convert';

import 'package:fluttercon_cache/fluttercon_cache.dart';
import 'package:test/test.dart';

Expand All @@ -14,28 +12,5 @@ void main() {
tearDown(() {
cache.clear();
});

group('getOrElse', () {
const cacheData = {'data': 'value'};
const orElseData = {'data': 'orElse value'};
test('returns the value if the key is found', () async {
await cache.set('key', jsonEncode(cacheData));
final value = await cache.getOrElse(
key: 'key',
fromJson: (json) => json,
orElse: () async => orElseData,
);
expect(value, equals(cacheData));
});

test('calls orElse if the key is not found', () async {
final value = await cache.getOrElse(
key: 'key',
fromJson: (json) => json,
orElse: () async => orElseData,
);
expect(value, equals(orElseData));
});
});
});
}
28 changes: 28 additions & 0 deletions api/packages/fluttercon_cache/test/src/keys_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:fluttercon_cache/fluttercon_cache.dart';
import 'package:test/test.dart';

void main() {
group('speakerCacheKey', () {
test('returns the correct cache key', () {
expect(speakerCacheKey('123'), equals('speaker_123'));
});
});

group('speakerTalksCacheKey', () {
test('returns the correct cache key', () {
expect(speakerTalksCacheKey('1,2,3'), equals('speaker_talks_1,2,3'));
});
});

group('favoritesCacheKey', () {
test('returns the correct cache key', () {
expect(favoritesCacheKey('123'), equals('favorites_123'));
});
});

group('talkCacheKey', () {
test('returns the correct cache key', () {
expect(talkCacheKey('123'), equals('talk_123'));
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'dart:convert';

import 'package:fluttercon_cache/fluttercon_cache.dart';
import 'package:test/test.dart';

void main() {
group('tryGetFromCache', () {
const cacheData = {'data': 'value'};
const orElseData = {'data': 'orElse value'};
test('returns from cache if found', () async {
final result = await tryGetFromCache(
getFromCache: () async => jsonEncode(cacheData),
fromJson: (json) => json,
orElse: () async => orElseData,
);

expect(result, equals(cacheData));
});

test('calls orElse if cache returns null', () async {
final result = await tryGetFromCache(
getFromCache: () async => null,
fromJson: (json) => json,
orElse: () async => orElseData,
);
expect(result, equals(orElseData));
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SpeakersRepository {
json,
(val) => SpeakerPreview.fromJson((val ?? {}) as Map<String, dynamic>),
),
orElse: getSpeakersFromApi,
orElse: _getSpeakersFromApi,
);

/// Fetches a [SpeakerDetail] with a given [id].
Expand All @@ -42,12 +42,11 @@ class SpeakersRepository {
return tryGetFromCache(
getFromCache: () => _cache.get(speakerCacheKey(id)),
fromJson: SpeakerDetail.fromJson,
orElse: () => getSpeakerDetailFromApi(id: id, userId: userId),
orElse: () => _getSpeakerDetailFromApi(id: id, userId: userId),
);
}

/// Fetches a paginated list of speakers from the api.
Future<PaginatedData<SpeakerPreview>> getSpeakersFromApi() async {
Future<PaginatedData<SpeakerPreview>> _getSpeakersFromApi() async {
final speakers = await _dataSource.getSpeakers();

final speakerPreviews = speakers.items
Expand Down Expand Up @@ -82,9 +81,7 @@ class SpeakersRepository {
return result;
}

/// Fetches a [SpeakerDetail] from the api with a given [id] and
/// the current [userId].
Future<SpeakerDetail> getSpeakerDetailFromApi({
Future<SpeakerDetail> _getSpeakerDetailFromApi({
required String id,
required String userId,
}) async {
Expand Down
88 changes: 84 additions & 4 deletions api/packages/speakers_repository/test/helpers/test_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import 'package:fluttercon_shared_models/fluttercon_shared_models.dart';

class TestHelpers {
static const userId = 'userId';
static const favoritesId = 'favoritesId';
static final talkDateTimeTemporal = TemporalDateTime(DateTime(2024));
static final talkDateTime = talkDateTimeTemporal.getDateTimeInUtc();

static final speakers = PaginatedResult(
[
Expand All @@ -11,18 +14,21 @@ class TestHelpers {
name: 'John Doe',
title: 'Test Title 1',
imageUrl: 'Test Image Url 1',
bio: 'Sample bio',
),
Speaker(
id: '2',
name: 'Jane Doe',
title: 'Test Title 2',
imageUrl: 'Test Image Url 2',
bio: 'Sample bio',
),
Speaker(
id: '3',
name: 'John Smith',
title: 'Test Title 3',
imageUrl: 'Test Image Url 3',
bio: 'Sample bio',
),
],
null,
Expand Down Expand Up @@ -78,19 +84,66 @@ class TestHelpers {
],
};

static const speakerDetail = SpeakerDetail(
static final speakerDetail = SpeakerDetail(
id: '1',
name: 'John Doe',
title: 'Test Title 1',
imageUrl: 'Test Image Url 1',
bio: 'Sample bio',
links: [],
talks: [],
links: const [
SpeakerLink(
id: '1',
url: 'https://test.com',
type: SpeakerLinkType.other,
),
],
talks: [
TalkPreview(
id: '1',
title: 'Test Talk',
room: 'Test Room',
startTime: talkDateTime,
speakerNames: const ['John Doe'],
isFavorite: true,
),
],
);

static final speakerDetailJson = {
'id': '1',
'name': 'John Doe',
'title': 'Test Title 1',
'imageUrl': 'Test Image Url 1',
'bio': 'Sample bio',
'links': [
{
'id': '1',
'speaker': {
'id': '1',
'name': 'John Doe',
'title': 'Test Title 1',
'imageUrl': 'Test Image Url 1',
},
'url': 'https://test.com',
'type': 'other',
}
],
'talks': [
{
'id': '1',
'title': 'Test Talk',
'room': 'Test Room',
'startTime': talkDateTime.toIso8601String(),
'speakerNames': ['John Doe'],
'isFavorite': true,
},
],
};

static final links = PaginatedResult(
[
Link(
id: '1',
speaker: speakers.items[0],
url: 'https://test.com',
type: LinkType.other,
Expand All @@ -107,6 +160,7 @@ class TestHelpers {
id: '1',
title: 'Test Talk',
room: 'Test Room',
startTime: talkDateTimeTemporal,
);

static final talks = PaginatedResult(
Expand All @@ -124,12 +178,38 @@ class TestHelpers {
);

static final favorites = Favorites(
id: 'favoritesId',
id: favoritesId,
userId: userId,
talks: [
FavoritesTalk(
id: '1',
talk: talk,
),
],
);

static final favoritesJson = {
'id': favoritesId,
'userId': userId,
'talks': [
{
'id': '1',
'talk': {
'id': '1',
'title': 'Test Talk',
'room': 'Test Room',
'startTime': talkDateTime.toIso8601String(),
},
},
],
};

static final favoritesResult = PaginatedResult(
[favorites],
null,
null,
null,
Favorites.classType,
null,
);
}
Loading

0 comments on commit da39508

Please sign in to comment.