Skip to content

Commit

Permalink
test: add tests for extractors
Browse files Browse the repository at this point in the history
Signed-off-by: SphericalKat <amolele@gmail.com>
  • Loading branch information
SphericalKat committed Mar 28, 2021
1 parent 29e345e commit 72e126d
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 13 deletions.
6 changes: 3 additions & 3 deletions coverage_badge.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions example/fuzzywuzzy.dart
Expand Up @@ -40,4 +40,38 @@ void main() {
cutoff: 50
),
);

print(
extractAllSorted(
query: 'goolge',
choices: [
'google',
'bing',
'facebook',
'linkedin',
'twitter',
'googleplus',
'bingnews',
'plexoogl'
],
cutoff: 10
),
);

print(
extractAll(
query: 'goolge',
choices: [
'google',
'bing',
'facebook',
'linkedin',
'twitter',
'googleplus',
'bingnews',
'plexoogl'
],
cutoff: 10
),
);
}
14 changes: 13 additions & 1 deletion lib/fuzzywuzzy.dart
Expand Up @@ -74,9 +74,21 @@ List<ExtractedResult> extractTop(
return extractor.extractTop(query, choices, ratio, limit);
}

/// Creates a list of [ExtractedResult] which contains all the choices with
/// their corresponding score where higher is more similar.
/// Uses [WeightedRatio] as the default algorithm
List<ExtractedResult> extractAll(
{required String query,
required List<String> choices,
int cutoff = 0,
Applicable ratio = const WeightedRatio()}) {
var extractor = Extractor(cutoff);
return extractor.extractWithoutOrder(query, choices, ratio);
}

/// Returns a sorted list of [ExtractedResult] without any cutoffs.
/// Uses [WeightedRatio] as the default algorithm.
List<ExtractedResult> extractSorted(
List<ExtractedResult> extractAllSorted(
{required String query,
required List<String> choices,
int cutoff = 0,
Expand Down
78 changes: 69 additions & 9 deletions test/fuzzywuzzy_test.dart
Expand Up @@ -4,55 +4,115 @@ import 'package:test/test.dart';
void main() {
group('Simple ratio', () {
test('simple ratio returns appropriate values', () {
final result = ratio("mysmilarstring", "myawfullysimilarstirng");
final result = ratio('mysmilarstring', 'myawfullysimilarstirng');
expect(result, 72);
});

test('simple ratio returns appropriate values', () {
final result = ratio("mysmilarstring", "mysimilarstring");
final result = ratio('mysmilarstring', 'mysimilarstring');
expect(result, 97);
});
});

group('Partial ratio', () {
test('partial ratio returns appropriate values', () {
final result = partialRatio("similar", "somewhresimlrbetweenthisstring");
final result = partialRatio('similar', 'somewhresimlrbetweenthisstring');
expect(result, 71);
});
});

group('Token sort ratio', () {
test('token sort ratio returns appropriate values', () {
final result =
tokenSortPartialRatio("order words out of", "words out of order");
tokenSortPartialRatio('order words out of', 'words out of order');
expect(result, 100);
});

test('token sort partial ratio returns appropriate values', () {
final result = tokenSortRatio("order words out of", "words out of order");
final result = tokenSortRatio('order words out of', 'words out of order');
expect(result, 100);
});
});

group('Token set ratio', () {
test('token set ratio returns appropriate values', () {
final result =
tokenSetPartialRatio("order words out of", "words out of order");
tokenSetPartialRatio('order words out of', 'words out of order');
expect(result, 100);
});

test('token set partial ratio returns appropriate values', () {
final result = tokenSetRatio("order words out of", "words out of order");
final result = tokenSetRatio('order words out of', 'words out of order');
expect(result, 100);
});
});

group('Weighted ratio', () {
test('weighted ratio returns appropriate values', () {
final result = weightedRatio(
"The quick brown fox jimps ofver the small lazy dog",
"the quick brown fox jumps over the small lazy dog");
'The quick brown fox jimps ofver the small lazy dog',
'the quick brown fox jumps over the small lazy dog');
expect(result, 97);
});
});

group('Extractors', () {
test('extract top returns appropriate values', () {
final result = extractTop(
query: 'goolge',
choices: [
'google',
'bing',
'facebook',
'linkedin',
'twitter',
'googleplus',
'bingnews',
'plexoogl'
],
limit: 4,
cutoff: 50,
).toString();
expect(result,
'[(string google, score: 83, index: 0), (string googleplus, score: 75, index: 5)]');
});

test('extract all sorted returns appropriate values', () {
final result = extractAllSorted(
query: 'goolge',
choices: [
'google',
'bing',
'facebook',
'linkedin',
'twitter',
'googleplus',
'bingnews',
'plexoogl'
],
cutoff: 10,
).toString();
expect(result,
'[(string google, score: 83, index: 0), (string googleplus, score: 75, index: 5), (string plexoogl, score: 43, index: 7), (string bingnews, score: 29, index: 6), (string linkedin, score: 29, index: 3), (string facebook, score: 29, index: 2), (string bing, score: 23, index: 1), (string twitter, score: 15, index: 4)]');
});

test('extract all returns appropriate values', () {
final result = extractAll(
query: 'goolge',
choices: [
'google',
'bing',
'facebook',
'linkedin',
'twitter',
'googleplus',
'bingnews',
'plexoogl'
],
cutoff: 10,
).toString();
expect(result,
'[(string google, score: 83, index: 0), (string bing, score: 23, index: 1), (string facebook, score: 29, index: 2), (string linkedin, score: 29, index: 3), (string twitter, score: 15, index: 4), (string googleplus, score: 75, index: 5), (string bingnews, score: 29, index: 6), (string plexoogl, score: 43, index: 7)]');
});
});
}

0 comments on commit 72e126d

Please sign in to comment.