117 lines
No EOL
3.6 KiB
Dart
117 lines
No EOL
3.6 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'package:test/test.dart';
|
|
import 'package:mocktail/mocktail.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:rugreefer/rugreefer.dart'; // Replace with actual package name
|
|
|
|
class MockClient extends Mock implements http.Client {}
|
|
|
|
void main() {
|
|
late GeoMetadataLookup lookup;
|
|
late Directory cacheDir;
|
|
late MockClient mockHttpClient;
|
|
|
|
setUp(() {
|
|
cacheDir = Directory.systemTemp.createTempSync();
|
|
mockHttpClient = MockClient();
|
|
lookup = GeoMetadataLookup(
|
|
apiUrl: 'http://localhost:8000/get_geodata/?format=geojson&geohash=',
|
|
cacheDir: cacheDir,
|
|
);
|
|
});
|
|
|
|
tearDown(() {
|
|
cacheDir.deleteSync(recursive: true);
|
|
});
|
|
|
|
test('Fetches and correctly parses GeoJSON with a residential landuse feature', () async {
|
|
final String geohash = 'sv2m';
|
|
final File cacheFile = File('${cacheDir.path}/$geohash.geojson.gz');
|
|
|
|
final Map<String, dynamic> mockGeoJson = {
|
|
"type": "FeatureCollection",
|
|
"features": [
|
|
{
|
|
"id": "0",
|
|
"type": "Feature",
|
|
"properties": {
|
|
"feature_id": "way/579610585",
|
|
"tags": {"waterway": "stream"}
|
|
},
|
|
"geometry": {
|
|
"type": "LineString",
|
|
"coordinates": [
|
|
[34.453125, 30.5735040],
|
|
[34.4515371, 30.5729953],
|
|
[34.4507646, 30.5725335]
|
|
]
|
|
}
|
|
}
|
|
]
|
|
};
|
|
|
|
final mockResponse = utf8.encode(jsonEncode(mockGeoJson));
|
|
cacheFile.writeAsBytesSync(GZipCodec().encode(mockResponse));
|
|
|
|
final results = await lookup.lookup(32.827714492634676, 34.96983862139979);
|
|
|
|
String prettyJson = const JsonEncoder.withIndent(' ').convert(results);
|
|
print(prettyJson);
|
|
|
|
expect(results, isNotEmpty);
|
|
expect(results.any((r) => r['type'] == 'polygon' && r['tags']['landuse'] == 'residential'), isTrue);
|
|
});
|
|
|
|
test('Ensures caching of GeoJSON data', () async {
|
|
final String geohash = 'sv2m';
|
|
final File cacheFile = File('${cacheDir.path}/$geohash.geojson.gz');
|
|
|
|
final mockGeoJson = jsonEncode({
|
|
"type": "FeatureCollection",
|
|
"features": []
|
|
});
|
|
|
|
final mockResponse = utf8.encode(mockGeoJson);
|
|
cacheFile.writeAsBytesSync(GZipCodec().encode(mockResponse));
|
|
|
|
expect(cacheFile.existsSync(), isTrue);
|
|
|
|
await lookup.lookup(30.5730, 34.4520);
|
|
expect(cacheFile.existsSync(), isTrue);
|
|
});
|
|
|
|
test('Handles API call correctly when file is not cached', () async {
|
|
final String geohash = 'sv2m';
|
|
|
|
when(() => mockHttpClient.get(Uri.parse('http://localhost:8000/get_geodata/?format=geojson&geohash=$geohash')))
|
|
.thenAnswer((_) async => http.Response(jsonEncode({
|
|
"type": "FeatureCollection",
|
|
"features": [
|
|
{
|
|
"id": "0",
|
|
"type": "Feature",
|
|
"properties": {
|
|
"feature_id": "way/579610585",
|
|
"tags": {"waterway": "stream"}
|
|
},
|
|
"geometry": {
|
|
"type": "LineString",
|
|
"coordinates": [
|
|
[34.453125, 30.5735040],
|
|
[34.4515371, 30.5729953]
|
|
]
|
|
}
|
|
}
|
|
]
|
|
}), 200));
|
|
|
|
final results = await lookup.lookup(31.776673242005337, 35.23441728409178);
|
|
|
|
String prettyJson = const JsonEncoder.withIndent(' ').convert(results);
|
|
print(prettyJson);
|
|
|
|
expect(results, isNotEmpty);
|
|
expect(results.any((r) => r['type'] == 'polygon' && r['tags']['historic'] == 'heritage'), isTrue);
|
|
});
|
|
} |