Skip to content

Commit

Permalink
add testing for sslMode
Browse files Browse the repository at this point in the history
  • Loading branch information
j4qfrost committed May 18, 2024
1 parent 37c56df commit ea8ae5f
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 71 deletions.
2 changes: 0 additions & 2 deletions packages/cli/lib/src/command.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// ignore_for_file: avoid_print

import 'dart:async';
import 'dart:io';
import 'dart:mirrors';
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/lib/src/commands/db_upgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class CLIDatabaseUpgrade extends CLICommand
s.port,
s.databaseName,
s.timeZone,
useSSL: s.isSSLConnection,
sslMode: s.sslMode,
);
}

Expand Down
10 changes: 5 additions & 5 deletions packages/cli/lib/src/migration_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class MigrationSource {
MigrationSource(this.source, this.uri, int nameStartIndex, int nameEndIndex) {
originalName = source!.substring(nameStartIndex, nameEndIndex);
name = "M${md5.convert(source!.codeUnits)}";
source = source!.replaceRange(nameStartIndex, nameEndIndex, name!);
source = source!.replaceRange(nameStartIndex, nameEndIndex, name);
}

MigrationSource.fromMap(Map<String, dynamic> map) {
originalName = map["originalName"] as String?;
originalName = map["originalName"] as String;
source = map["source"] as String?;
name = map["name"] as String?;
name = map["name"] as String;
uri = map["uri"] as String?;
}

Expand Down Expand Up @@ -58,9 +58,9 @@ class MigrationSource {

String? source;

String? originalName;
late final String originalName;

String? name;
late final String name;

String? uri;

Expand Down
10 changes: 8 additions & 2 deletions packages/cli/lib/src/mixins/database_connecting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ mixin CLIDatabaseConnectingCommand implements CLICommand, CLIProject {

@Flag(
"use-ssl",
help: "Whether or not the database connection should use SSL",
help: "DEPRECATED: Use ssl-mode instead",
defaultsTo: false,
)
bool get useSSL => decode("use-ssl");

@Option("ssl-mode",
help:
"Whether or not the database connection should use SSL (disable/require/verifyFull)",
defaultsTo: "disable")
String get sslMode => decode("ssl-mode");

@Option(
"connect",
abbr: "c",
Expand Down Expand Up @@ -102,7 +108,7 @@ mixin CLIDatabaseConnectingCommand implements CLICommand, CLIProject {
connectedDatabase.host,
connectedDatabase.port,
connectedDatabase.databaseName,
useSSL: useSSL,
sslMode: sslMode,
);
}

Expand Down
10 changes: 5 additions & 5 deletions packages/cli/lib/src/scripts/run_upgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class RunUpgradeExecutable extends Executable<Map<String, dynamic>> {
dbInfo.port,
dbInfo.databaseName,
timeZone: dbInfo.timeZone,
useSSL: dbInfo.useSSL,
sslMode: dbInfo.sslMode,
);
}

Expand Down Expand Up @@ -129,7 +129,7 @@ class DBInfo {
this.port,
this.databaseName,
this.timeZone, {
this.useSSL = false,
this.sslMode,
});

DBInfo.fromMap(Map<String, dynamic> map)
Expand All @@ -140,7 +140,7 @@ class DBInfo {
port = map["port"] as int?,
databaseName = map["databaseName"] as String?,
timeZone = map["timeZone"] as String?,
useSSL = (map["useSSL"] ?? false) as bool;
sslMode = map["sslMode"] as String?;

final String? flavor;
final String? username;
Expand All @@ -149,7 +149,7 @@ class DBInfo {
final int? port;
final String? databaseName;
final String? timeZone;
final bool useSSL;
final String? sslMode;

Map<String, dynamic>? asMap() {
return {
Expand All @@ -160,7 +160,7 @@ class DBInfo {
"port": port,
"databaseName": databaseName,
"timeZone": timeZone,
"useSSL": useSSL
"sslMode": sslMode
};
}
}
2 changes: 1 addition & 1 deletion packages/cli/lib/src/scripts/schema_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SchemaBuilderExecutable extends Executable<Map<String, dynamic>> {
Schema outputSchema = inputSchema;
for (final source in sources) {
final Migration instance = instanceOf(
source.name!,
source.name,
positionalArguments: const [],
namedArguments: const <Symbol, dynamic>{},
constructorName: Symbol.empty,
Expand Down
26 changes: 21 additions & 5 deletions packages/cli/test/command_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:conduit/src/command.dart';
import 'package:conduit/src/metadata.dart';
import 'package:conduit_postgresql/conduit_postgresql.dart';
import 'package:postgres/postgres.dart';
import 'package:test/test.dart';

void main() {
Expand Down Expand Up @@ -153,7 +155,7 @@ void main() {
final results = cmd.options.parse(args);
cmd.process(results);

expect(cmd.useSSl, equals(true));
expect(cmd.useSSL, equals(true));
});

test('Command bool conversion - negated ', () {
Expand All @@ -164,7 +166,18 @@ void main() {
final results = cmd.options.parse(args);
cmd.process(results);

expect(cmd.useSSl, equals(false));
expect(cmd.useSSL, equals(false));
});

test('Enum checks on sslMode, correct value', () {
final cmd = TestCLICommand();

final args = ['--sslMode=require'];

final results = cmd.options.parse(args);
cmd.process(results);

expect(cmd.sslMode.toSslMode(), equals(SslMode.require));
});
}

Expand Down Expand Up @@ -208,10 +221,13 @@ class TestCLICommand extends CLICommand {
int get guaranteed => decodeOptional("guaranteed", orElse: () => 1)!;

@Flag("useSSL", abbr: "u", help: "UseSSL.", negatable: true)
bool get useSSl => decode("useSSL");
bool get useSSL => decode("useSSL");

@Flag("useSSLWithDefault", abbr: "d", help: "useSSLWithDefault.")
bool get useSSLWithDefault => decode("useSSLWithDefault");

@Flag("useSSLWithDefault", abbr: "d", help: "useSSlWithDefault.")
bool get useSSlWithDefault => decode("useSSlWithDefault");
@Option("sslMode")
String get sslMode => decode("sslMode");

@Option(
"scopes",
Expand Down
70 changes: 36 additions & 34 deletions packages/cli/test/migration_execution_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ String connectString =
"postgres://${connectInfo.username}:${connectInfo.password}@${connectInfo.host}:${connectInfo.port}/${connectInfo.databaseName}";

void main() {
late PostgreSQLPersistentStore store;
PostgreSQLPersistentStore? store;

setUpAll(() async {
final t =
Expand Down Expand Up @@ -54,10 +54,10 @@ void main() {

await Future.wait(
tables.map((t) {
return store.execute("DROP TABLE IF EXISTS $t");
}),
return store?.execute("DROP TABLE IF EXISTS $t");
}).nonNulls,
);
await store.close();
await store?.close();
});

tearDownAll(DartProjectAgent.tearDownAll);
Expand All @@ -69,28 +69,28 @@ void main() {

test("Generate and execute initial schema makes workable DB", () async {
expect(await runMigrationCases(["Case1"]), isZero);
final version =
await store.execute("SELECT versionNumber FROM _conduit_version_pgsql");
final version = await store!
.execute("SELECT versionNumber FROM _conduit_version_pgsql");
expect(version, [
[1]
]);
expect(await columnsOfTable(store, "_testobject"), ["id", "foo"]);
expect(await columnsOfTable(store!, "_testobject"), ["id", "foo"]);
});

test(
"Database already up to date returns 0 status code, does not change version",
() async {
expect(await runMigrationCases(["Case2"]), isZero);

var versionRow = await store.execute(
var versionRow = await store!.execute(
"SELECT versionNumber, dateOfUpgrade FROM _conduit_version_pgsql",
) as List<List<dynamic>>;
expect(versionRow.first.first, 1);
final updateDate = versionRow.first.last;

cli.clearOutput();
expect(await runMigrationCases(["Case2"]), isZero);
versionRow = await store.execute(
versionRow = await store!.execute(
"SELECT versionNumber, dateOfUpgrade FROM _conduit_version_pgsql",
) as List<List>;
expect(versionRow.length, 1);
Expand All @@ -102,45 +102,45 @@ void main() {
test("Multiple migration files are ran", () async {
expect(await runMigrationCases(["Case31", "Case32"]), isZero);

final version =
await store.execute("SELECT versionNumber FROM _conduit_version_pgsql");
final version = await store!
.execute("SELECT versionNumber FROM _conduit_version_pgsql");
expect(version, [
[1],
[2]
]);
expect(await columnsOfTable(store, "_testobject"), ["id", "foo"]);
expect(await columnsOfTable(store, "_foo"), ["id", "testobject_id"]);
expect(await columnsOfTable(store!, "_testobject"), ["id", "foo"]);
expect(await columnsOfTable(store!, "_foo"), ["id", "testobject_id"]);
});

test("Only later migration files are ran if already at a version", () async {
expect(await runMigrationCases(["Case41"]), isZero);
var version =
await store.execute("SELECT versionNumber FROM _conduit_version_pgsql");
var version = await store!
.execute("SELECT versionNumber FROM _conduit_version_pgsql");
expect(version, [
[1]
]);
cli.clearOutput();

expect(await columnsOfTable(store, "_testobject"), ["id", "foo"]);
expect(await tableExists(store, "_foo"), isFalse);
expect(await columnsOfTable(store!, "_testobject"), ["id", "foo"]);
expect(await tableExists(store!, "_foo"), isFalse);

expect(await runMigrationCases(["Case42"], fromVersion: 1), isZero);
version =
await store.execute("SELECT versionNumber FROM _conduit_version_pgsql");
version = await store!
.execute("SELECT versionNumber FROM _conduit_version_pgsql");
expect(version, [
[1],
[2]
]);

expect(await columnsOfTable(store, "_testobject"), ["id", "foo"]);
expect(await columnsOfTable(store, "_foo"), ["id", "testobject_id"]);
expect(await columnsOfTable(store!, "_testobject"), ["id", "foo"]);
expect(await columnsOfTable(store!, "_foo"), ["id", "testobject_id"]);
});

test("If migration throws exception, rollback any changes", () async {
expect(await runMigrationCases(["Case5"]), isNonZero);

expect(await tableExists(store, store.versionTable.name), isFalse);
expect(await tableExists(store, "_testobject"), isFalse);
expect(await tableExists(store!, store!.versionTable.name), isFalse);
expect(await tableExists(store!, "_testobject"), isFalse);
});

test(
Expand All @@ -164,9 +164,9 @@ void main() {
);
expect(cli.output, contains('relation "_unknowntable" does not exist'));

expect(await tableExists(store, store.versionTable.name), isFalse);
expect(await tableExists(store, "_testobject"), isFalse);
expect(await tableExists(store, "_foo"), isFalse);
expect(await tableExists(store!, store!.versionTable.name), isFalse);
expect(await tableExists(store!, "_testobject"), isFalse);
expect(await tableExists(store!, "_foo"), isFalse);
},
);

Expand All @@ -187,23 +187,23 @@ void main() {

expect(cli.output, contains('relation "_unknowntable" does not exist'));

final version = await store
final version = await store!
.execute("SELECT versionNumber FROM _conduit_version_pgsql");
expect(version, [
[1],
]);

expect(await tableExists(store, store.versionTable.name), isTrue);
expect(await tableExists(store, "_testobject"), isTrue);
expect(await tableExists(store, "_foo"), isFalse);
expect(await tableExists(store!, store!.versionTable.name), isTrue);
expect(await tableExists(store!, "_testobject"), isTrue);
expect(await tableExists(store!, "_foo"), isFalse);
},
);

test("If seed fails, all schema changes are rolled back", () async {
expect(await runMigrationCases(["Case7"]), isNonZero);

expect(await tableExists(store, store.versionTable.name), isFalse);
expect(await tableExists(store, "_testobject"), isFalse);
expect(await tableExists(store!, store!.versionTable.name), isFalse);
expect(await tableExists(store!, "_testobject"), isFalse);
});

test(
Expand Down Expand Up @@ -256,7 +256,7 @@ MigrationSource migrationSourceFromClassDeclaration(ClassDeclaration cu) {
}

List<MigrationSource> getOrderedTestMigrations(
List<String?> names, {
List<String> names, {
int fromVersion = 0,
}) {
final uri = Directory.current.uri
Expand All @@ -279,7 +279,7 @@ List<MigrationSource> getOrderedTestMigrations(
}

Future runMigrationCases(
List<String?> migrationNames, {
List<String> migrationNames, {
int fromVersion = 0,
StringSink? log,
}) async {
Expand All @@ -298,6 +298,8 @@ Future runMigrationCases(

final String useSsl = Platform.environment["USE_SSL"] ?? "";

print(useSsl);

final res =
await cli.run("db", ["upgrade", useSsl, "--connect", connectString]);

Expand Down
3 changes: 3 additions & 0 deletions packages/isolate_exec/lib/src/executor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class IsolateExecutor<U> {
automaticPackageResolution: packageConfigURI == null,
);
return await completer.future;
} catch (e) {
print(e);
rethrow;
} finally {
onErrorPort.close();
controlPort.close();
Expand Down
Loading

0 comments on commit ea8ae5f

Please sign in to comment.