Skip to content

Commit

Permalink
Add very basic db tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Aug 28, 2023
1 parent cff3d8a commit 51c1dbe
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
4 changes: 1 addition & 3 deletions packages/db/lib/behavior.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:db/query.dart';
import 'package:postgres/postgres.dart';
import 'package:types/types.dart';

/// Query a ship behavior state by symbol.
Expand All @@ -17,7 +16,6 @@ Map<String, dynamic> behaviorStateToColumnMap(BehaviorState state) => {
};

/// Convert a result row to a BehaviorState.
BehaviorState behaviorStateFromResultRow(PostgreSQLResultRow row) {
final values = row.toColumnMap();
BehaviorState behaviorStateFromColumnMap(Map<String, dynamic> values) {
return BehaviorState.fromJson(values['json'] as Map<String, dynamic>);
}
13 changes: 10 additions & 3 deletions packages/db/lib/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ class Database {
query.fmtString,
substitutionValues: query.substitutionValues,
);
return result.map<HistoricalSurvey>(surveyFromResultRow);
return result
.map((r) => r.toColumnMap())
.map<HistoricalSurvey>(surveyFromColumnMap);
}

/// Mark the given survey as exhausted.
Expand All @@ -91,15 +93,20 @@ class Database {
final query = factionBySymbolQuery(symbol);
return connection
.query(query.fmtString, substitutionValues: query.substitutionValues)
.then((result) => factionFromResultRow(result.first));
.then((result) => factionFromColumnMap(result.first.toColumnMap()));
}

/// Gets all factions.
Future<List<Faction>> allFactions() {
final query = allFactionsQuery();
return connection
.query(query.fmtString, substitutionValues: query.substitutionValues)
.then((result) => result.map(factionFromResultRow).toList());
.then(
(result) => result
.map((r) => r.toColumnMap())
.map(factionFromColumnMap)
.toList(),
);
}

/// Cache the given factions.
Expand Down
6 changes: 2 additions & 4 deletions packages/db/lib/faction.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:db/query.dart';
import 'package:postgres/postgres.dart';
import 'package:types/types.dart';

/// Query all factions.
Expand All @@ -25,12 +24,11 @@ Query insertFactionQuery(Faction faction) => Query(

/// Convert a faction to a column map.
Map<String, dynamic> factionToColumnMap(Faction faction) => {
'symbol': faction.symbol.toString(),
'symbol': faction.symbol.toJson(),
'json': faction.toJson(),
};

/// Convert a result row to a faction.
Faction factionFromResultRow(PostgreSQLResultRow row) {
final values = row.toColumnMap();
Faction factionFromColumnMap(Map<String, dynamic> values) {
return Faction.fromJson(values['json'] as Map<String, dynamic>)!;
}
4 changes: 1 addition & 3 deletions packages/db/lib/survey.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'package:db/query.dart';
import 'package:postgres/postgres.dart';
import 'package:types/types.dart';

/// Convert a row result into a survey.
HistoricalSurvey surveyFromResultRow(PostgreSQLResultRow row) {
final values = row.toColumnMap();
HistoricalSurvey surveyFromColumnMap(Map<String, dynamic> values) {
return HistoricalSurvey(
survey: Survey(
signature: values['signature'] as String,
Expand Down
8 changes: 3 additions & 5 deletions packages/db/lib/transaction.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:db/db.dart';
import 'package:db/query.dart';
import 'package:postgres/postgres.dart';
import 'package:types/types.dart';

/// Create the insertion query for a transaction.
Expand Down Expand Up @@ -33,8 +32,7 @@ Map<String, dynamic> transactionToColumnMap(Transaction transaction) {
}

/// Create a new transaction from a result row.
Transaction transactionFromResultRow(PostgreSQLResultRow row) {
final values = row.toColumnMap();
Transaction transactionFromColumnMap(Map<String, dynamic> values) {
return Transaction(
transactionType:
TransactionType.fromName(values['transaction_type'] as String),
Expand Down Expand Up @@ -63,7 +61,7 @@ Future<Set<String>> uniqueShipSymbols(Database db) async {
/// Get all transactions from the database.
Future<Iterable<Transaction>> allTransactions(Database db) async {
final result = await db.connection.query('SELECT * FROM transaction_');
return result.map(transactionFromResultRow);
return result.map((r) => r.toColumnMap()).map(transactionFromColumnMap);
}

/// Get transactions after a given timestamp.
Expand All @@ -75,5 +73,5 @@ Future<Iterable<Transaction>> transactionsAfter(
'SELECT * FROM transaction_ WHERE timestamp > @timestamp',
substitutionValues: {'timestamp': timestamp},
);
return result.map(transactionFromResultRow);
return result.map((r) => r.toColumnMap()).map(transactionFromColumnMap);
}

0 comments on commit 51c1dbe

Please sign in to comment.