mirror of
https://github.com/immich-app/isar.git
synced 2026-09-30 13:23:02 +08:00
Generator tests
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
name: Dart CI
|
||||
|
||||
on: push
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
format:
|
||||
@@ -57,6 +63,17 @@ jobs:
|
||||
flutter test -j 1
|
||||
working-directory: packages/isar_test
|
||||
|
||||
test_generator:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: subosito/flutter-action@v2
|
||||
- name: Run Generator Unit tests
|
||||
run: |
|
||||
dart pub get
|
||||
dart test
|
||||
working-directory: packages/isar_generator
|
||||
|
||||
drive_ios:
|
||||
strategy:
|
||||
matrix:
|
||||
|
||||
@@ -10,7 +10,9 @@ import 'package:isar_generator/src/object_info.dart';
|
||||
|
||||
class IsarAnalyzer {
|
||||
ObjectInfo analyze(Element modelClass) {
|
||||
if (modelClass is! ClassElement) {
|
||||
if (modelClass is! ClassElement ||
|
||||
modelClass.isEnum ||
|
||||
modelClass.isMixin) {
|
||||
err('Only classes may be annotated with @Collection.', modelClass);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,4 +18,7 @@ dependencies:
|
||||
source_gen: ^1.2.2
|
||||
|
||||
dev_dependencies:
|
||||
build_test: ^2.1.5
|
||||
matcher: ^0.12.12
|
||||
test: ^1.21.0
|
||||
very_good_analysis: ^3.0.1
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:build/build.dart';
|
||||
import 'package:build_test/build_test.dart';
|
||||
import 'package:isar_generator/isar_generator.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('Error case', () {
|
||||
for (final file in Directory('test/errors').listSync(recursive: true)) {
|
||||
if (file is! File || !file.path.endsWith('.dart')) continue;
|
||||
|
||||
test(file.path, () async {
|
||||
final content = await file.readAsLines();
|
||||
|
||||
final errorMessage = content.first.split('//').last.trim();
|
||||
|
||||
var error = '';
|
||||
try {
|
||||
await testBuilder(
|
||||
getIsarGenerator(BuilderOptions.empty),
|
||||
{'a|${file.path}': content.join('\n')},
|
||||
reader: await PackageAssetReader.currentIsolate(),
|
||||
);
|
||||
} catch (e) {
|
||||
error = e.toString();
|
||||
}
|
||||
|
||||
expect(error.toLowerCase(), contains(errorMessage.toLowerCase()));
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// must not be abstract
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
abstract class Model {
|
||||
int? id;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// unnamed constructor
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
class Model {
|
||||
Model.create();
|
||||
|
||||
int? id;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// constructor parameter does not match a property
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
class Model {
|
||||
// ignore: avoid_unused_constructor_parameters
|
||||
Model(this.prop1, String somethingElse);
|
||||
|
||||
int? id;
|
||||
|
||||
final String prop1;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// constructor parameter type does not match property type
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
class Model {
|
||||
// ignore: avoid_unused_constructor_parameters
|
||||
Model(int prop1);
|
||||
|
||||
int? id;
|
||||
|
||||
String prop1 = '5';
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// only classes
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
// ignore: invalid_annotation_target
|
||||
@Collection()
|
||||
enum Test { a, b, c }
|
||||
@@ -0,0 +1,8 @@
|
||||
// must not be abstract
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
abstract class Model {
|
||||
int? id;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// only classes
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
// ignore: invalid_annotation_target
|
||||
@Collection()
|
||||
mixin Test {}
|
||||
@@ -0,0 +1,9 @@
|
||||
// must be public
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
// ignore: unused_element
|
||||
class _Model {
|
||||
int? id;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// only classes
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
// ignore: invalid_annotation_target
|
||||
@Collection()
|
||||
const t = 'hello';
|
||||
@@ -0,0 +1,24 @@
|
||||
// Converters are not allowed for ids
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
class Test {
|
||||
@Id()
|
||||
@Converter()
|
||||
late String id;
|
||||
}
|
||||
|
||||
class Converter extends TypeConverter<String, int> {
|
||||
const Converter();
|
||||
|
||||
@override
|
||||
String fromIsar(int object) {
|
||||
return object.toString();
|
||||
}
|
||||
|
||||
@override
|
||||
int toIsar(String object) {
|
||||
return int.parse(object);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Two or more properties annotated with @Id()
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
class Test {
|
||||
@Id()
|
||||
int? id1;
|
||||
|
||||
@Id()
|
||||
int? id2;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// no int property named "id"
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
class Test {
|
||||
late String name;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// Only int ids are allowed
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
class Test {
|
||||
@Id()
|
||||
String? id;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Only int ids are allowed
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
class Test {
|
||||
@Id()
|
||||
@Size32()
|
||||
int? id;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// same name
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
class Model {
|
||||
int? id;
|
||||
|
||||
@Index(name: 'myindex')
|
||||
String? prop1;
|
||||
|
||||
@Index(name: 'myindex')
|
||||
String? prop2;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// same name
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
class Model {
|
||||
int? id;
|
||||
|
||||
final IsarLink<Model2> prop1 = IsarLink();
|
||||
|
||||
@Name('prop1')
|
||||
final IsarLinks<Model2> prop2 = IsarLinks();
|
||||
}
|
||||
|
||||
@Collection()
|
||||
class Model2 {
|
||||
int? id;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// must not be late
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
class Model {
|
||||
int? id;
|
||||
|
||||
late IsarLink<Model2> link;
|
||||
}
|
||||
|
||||
@Collection()
|
||||
class Model2 {
|
||||
int? id;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// must not be nullable
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
class Model {
|
||||
int? id;
|
||||
|
||||
IsarLink<Model2>? link;
|
||||
}
|
||||
|
||||
@Collection()
|
||||
class Model2 {
|
||||
int? id;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// same name
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
@Collection()
|
||||
class Model {
|
||||
int? id;
|
||||
|
||||
String? prop1;
|
||||
|
||||
@Name('prop1')
|
||||
String? prop2;
|
||||
}
|
||||
Reference in New Issue
Block a user