Files
sdk/samples/ffi/sqlite/test/sqlite_test.dart
T
Daco Harkes ee0b1df4a6 [vm/ffi] Migrate tests/samples/benchmarks to extension methods
I used the regex replaces documented on the `load` and `store` deprecated methods.
I manually replaced some `.val` to `.ref` when a regex could not detect whether something was a primitive value or a struct.

Issue: https://github.com/dart-lang/sdk/issues/37773

Change-Id: I3534b6dd00d9ac45fa1a11fe75c80fb3cccc07dc
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-mac-debug-simdbc64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-reload-mac-release-simdbc64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/118993
Reviewed-by: Samir Jindel <sjindel@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
2019-10-08 16:49:41 +00:00

176 lines
5.1 KiB
Dart

// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// VMOptions=--optimization-counter-threshold=5
import 'dart:ffi';
import "package:test/test.dart";
import '../lib/sqlite.dart';
import '../lib/src/ffi/utf8.dart';
void main() {
test("sqlite integration test", () {
// TODO(dacoharkes): Put the database relative to this file,
// instead of where the script was invoked from.
Database d = Database("test.db");
d.execute("drop table if exists Cookies;");
d.execute("""
create table Cookies (
id integer primary key,
name text not null,
alternative_name text
);""");
d.execute("""
insert into Cookies (id, name, alternative_name)
values
(1,'Chocolade chip cookie', 'Chocolade cookie'),
(2,'Ginger cookie', null),
(3,'Cinnamon roll', null)
;""");
Result result = d.query("""
select
id,
name,
alternative_name,
case
when id=1 then 'foo'
when id=2 then 42
when id=3 then null
end as multi_typed_column
from Cookies
;""");
for (Row r in result) {
int id = r.readColumnAsInt("id");
expect(true, 1 <= id && id <= 3);
String name = r.readColumnByIndex(1);
expect(true, name is String);
String alternativeName = r.readColumn("alternative_name");
expect(true, alternativeName is String || alternativeName == null);
dynamic multiTypedValue = r.readColumn("multi_typed_column");
expect(
true,
multiTypedValue == 42 ||
multiTypedValue == 'foo' ||
multiTypedValue == null);
print("$id $name $alternativeName $multiTypedValue");
}
result = d.query("""
select
id,
name,
alternative_name,
case
when id=1 then 'foo'
when id=2 then 42
when id=3 then null
end as multi_typed_column
from Cookies
;""");
for (Row r in result) {
int id = r.readColumnAsInt("id");
expect(true, 1 <= id && id <= 3);
String name = r.readColumnByIndex(1);
expect(true, name is String);
String alternativeName = r.readColumn("alternative_name");
expect(true, alternativeName is String || alternativeName == null);
dynamic multiTypedValue = r.readColumn("multi_typed_column");
expect(
true,
multiTypedValue == 42 ||
multiTypedValue == 'foo' ||
multiTypedValue == null);
print("$id $name $alternativeName $multiTypedValue");
if (id == 2) {
result.close();
break;
}
}
try {
result.iterator.moveNext();
} on SQLiteException catch (e) {
print("expected exception on accessing result data after close: $e");
}
try {
d.query("""
select
id,
non_existing_column
from Cookies
;""");
} on SQLiteException catch (e) {
print("expected this query to fail: $e");
}
d.execute("drop table Cookies;");
d.close();
});
test("concurrent db open and queries", () {
Database d = Database("test.db");
Database d2 = Database("test.db");
d.execute("drop table if exists Cookies;");
d.execute("""
create table Cookies (
id integer primary key,
name text not null,
alternative_name text
);""");
d.execute("""
insert into Cookies (id, name, alternative_name)
values
(1,'Chocolade chip cookie', 'Chocolade cookie'),
(2,'Ginger cookie', null),
(3,'Cinnamon roll', null)
;""");
Result r = d.query("select * from Cookies;");
Result r2 = d2.query("select * from Cookies;");
r.iterator..moveNext();
r2.iterator..moveNext();
r.iterator..moveNext();
Result r3 = d2.query("select * from Cookies;");
r3.iterator..moveNext();
expect(2, r.iterator.current.readColumn("id"));
expect(1, r2.iterator.current.readColumn("id"));
expect(1, r3.iterator.current.readColumn("id"));
r.close();
r2.close();
r3.close();
d.close();
d2.close();
});
test("stress test", () {
Database d = Database("test.db");
d.execute("drop table if exists Cookies;");
d.execute("""
create table Cookies (
id integer primary key,
name text not null,
alternative_name text
);""");
int repeats = 100;
for (int i = 0; i < repeats; i++) {
d.execute("""
insert into Cookies (name, alternative_name)
values
('Chocolade chip cookie', 'Chocolade cookie'),
('Ginger cookie', null),
('Cinnamon roll', null)
;""");
}
Result r = d.query("select count(*) from Cookies;");
int count = r.first.readColumnByIndexAsInt(0);
expect(count, 3 * repeats);
r.close();
d.close();
});
test("Utf8 unit test", () {
final String test = 'Hasta Mañana';
final medium = Utf8.allocate(test);
expect(test, medium.ref.toString());
medium.free();
});
}