Skip to content

Commit

Permalink
added : Tests for AlbumDatabase class
Browse files Browse the repository at this point in the history
  • Loading branch information
nikeight committed Feb 13, 2023
1 parent f7d3f6f commit 8740a82
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
5 changes: 5 additions & 0 deletions praxis_data/lib/sources/local/album_db/album_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ class AlbumDatabase {
final db = await database;
db.close();
}

Future<int> getDatabaseVersion() async {
final db = await database;
return db.getVersion();
}
}
57 changes: 41 additions & 16 deletions praxis_data/test/sources/local/album_db/album_database_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,69 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:praxis_data/models/album/model/dt_song.dart';
import 'package:praxis_data/sources/local/album_db/album_database.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite/sqlite_api.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

class MockAlbumDatabase extends Mock implements AlbumDatabase{}
class MockAlbumDatabase extends Mock implements AlbumDatabase {}

class MockDtSong extends Mock implements DtSong {}

const String TEST_ALBUM_DB_TABLE_NAME = "test_album_table";

void main() {
late Database sqfDatabase;
late MockAlbumDatabase mockAlbumDatabase;
late MockAlbumDatabase sut;

DtSong fakeDtSongOb = DtSong(
1, "Discovery", "Daft Punk", "htps://daftpunk/discovery.png", false);
1, "Discovery", "Daft Punk", "https://daftpunk/discovery.png", false);

List<DtSong> fakeListDtSong = List.generate(
5,
(index) => DtSong(1 + index, "Discovery", "Daft Punk",
"htps://daftpunk/discovery.png", false));
"https://daftpunk/discovery.png", false));

setUpAll(() async {
registerFallbackValue(MockDtSong());
sqfliteFfiInit();
sqfDatabase = await databaseFactoryFfi.openDatabase(inMemoryDatabasePath);
// await sqfDatabase.execute(databaseRules);
mockAlbumDatabase = MockAlbumDatabase();
mockAlbumDatabase.albumDatabase = sqfDatabase;
when(() => mockAlbumDatabase.insertToSpotifyDb(any())).thenAnswer((invocation) async => true);
when(() => mockAlbumDatabase.getAllAlbums()).thenAnswer((invocation) async => fakeListDtSong);

// TODO : Avoid creating a DB table and use Testing rules for the same
// Better ways to approach the same.

await sqfDatabase.execute("""
CREATE TABLE $TEST_ALBUM_DB_TABLE_NAME (
${SpotifyDbSongsField.album_primary_id} integer primary key autoincrement,
${SpotifyDbSongsField.album_name} TEXT NOT NULL,
${SpotifyDbSongsField.album_title} TEXT NOT NULL,
${SpotifyDbSongsField.album_image_url} TEXT NOT NULL,
${SpotifyDbSongsField.album_is_fav} INTEGER NOT NULL
)
""");
sut = MockAlbumDatabase();
sut.albumDatabase = sqfDatabase;
});

group("album database test", () {
test("check sqflite database version", () async {
when(() => sqfDatabase.getVersion()).thenAnswer((_) async => 1);
expect(await sqfDatabase.getVersion(), 1);
sqfDatabase.setVersion(1);
when(() => sut.getDatabaseVersion()).thenAnswer((_) async => 1);
expect(await sqfDatabase.getVersion(), await sqfDatabase.getVersion());
});

test("item inserted at db successfully", () async {
var isItemInjected = await sqfDatabase.insert(TEST_ALBUM_DB_TABLE_NAME, fakeDtSongOb.toMap());
when(() => sut.insertToSpotifyDb(any())).thenAnswer((invocation) async => true);
expect(isItemInjected, 1);
});

test("add data model to database", () async {
var isItemInjected =
await sqfDatabase.insert("album_table", fakeDtSongOb.toMap());
var getItems = await sqfDatabase.query("album_table");
expect(getItems.length, equals(isItemInjected));
test("item retrieved from db successfully", () async {
fakeListDtSong.forEach((dtSongObj) {
sqfDatabase.insert(TEST_ALBUM_DB_TABLE_NAME, dtSongObj.toMap());
});
final testAlbumList = await sqfDatabase.query(TEST_ALBUM_DB_TABLE_NAME);
when(() => sut.getAllAlbums()).thenAnswer((invocation) async => fakeListDtSong);
expect(testAlbumList.length, fakeListDtSong.length);
});
});
}

0 comments on commit 8740a82

Please sign in to comment.