Skip to content

Commit

Permalink
Adding tests for time value parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Apr 16, 2024
1 parent 3fcbe48 commit 3f65235
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,6 @@ private AzureOpenAiEmbeddingsModel updateModelWithEmbeddingDetails(AzureOpenAiEm

@Override
public TransportVersion getMinimalSupportedVersion() {
return TransportVersions.ML_INFERENCE_AZURE_OPENAI_EMBEDDINGS;
return TransportVersions.ML_INFERENCE_RATE_LIMIT_SETTINGS_ADDED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class HuggingFaceServiceSettings implements ServiceSettings, HuggingFaceRateLimitServiceSettings {
public static final String NAME = "hugging_face_service_settings";

// At the time of writing HuggingFace hasn't posted the default rate limit for inference endpoints so the value his is only a guess
// At the time of writing HuggingFace hasn't posted the default rate limit for inference endpoints so the value here is only a guess
private static final RateLimitSettings DEFAULT_RATE_LIMIT_SETTINGS = new RateLimitSettings(TimeValue.timeValueMinutes(3000));

public static HuggingFaceServiceSettings fromMap(Map<String, Object> map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.elasticsearch.xpack.inference.services.ServiceUtils.createUri;
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractOptionalEnum;
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractOptionalString;
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractOptionalTimeValue;
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractRequiredSecureString;
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractRequiredString;
import static org.elasticsearch.xpack.inference.services.ServiceUtils.getEmbeddingSize;
Expand Down Expand Up @@ -328,6 +329,60 @@ public void testExtractOptionalEnum_ReturnsClassification_WhenValueIsAcceptable(
assertTrue(map.isEmpty());
}

public void testExtractOptionalTimeValue_ReturnsNull_WhenKeyDoesNotExist() {
var validation = new ValidationException();
Map<String, Object> map = modifiableMap(Map.of("key", 1));
var timeValue = extractOptionalTimeValue(map, "a", "scope", validation);

assertNull(timeValue);
assertTrue(validation.validationErrors().isEmpty());
}

public void testExtractOptionalTimeValue_CreatesTimeValue_Of3Seconds() {
var validation = new ValidationException();
Map<String, Object> map = modifiableMap(Map.of("key", "3s"));
var timeValue = extractOptionalTimeValue(map, "key", "scope", validation);

assertTrue(validation.validationErrors().isEmpty());
assertNotNull(timeValue);
assertThat(timeValue, is(TimeValue.timeValueSeconds(3)));
assertTrue(map.isEmpty());
}

public void testExtractOptionalTimeValue_ReturnsNullAndAddsException_WhenTimeValueIsInvalid_InvalidUnit() {
var validation = new ValidationException();
Map<String, Object> map = modifiableMap(Map.of("key", "3abc"));
var timeValue = extractOptionalTimeValue(map, "key", "scope", validation);

assertFalse(validation.validationErrors().isEmpty());
assertNull(timeValue);
assertTrue(map.isEmpty());
assertThat(
validation.validationErrors().get(0),
is(
"[scope] Invalid time value [3abc]. [key] must be a valid time value string: failed to parse setting [key] "
+ "with value [3abc] as a time value: unit is missing or unrecognized"
)
);
}

public void testExtractOptionalTimeValue_ReturnsNullAndAddsException_WhenTimeValueIsInvalid_NegativeNumber() {
var validation = new ValidationException();
Map<String, Object> map = modifiableMap(Map.of("key", "-3d"));
var timeValue = extractOptionalTimeValue(map, "key", "scope", validation);

assertFalse(validation.validationErrors().isEmpty());
assertNull(timeValue);
assertTrue(map.isEmpty());
assertThat(
validation.validationErrors().get(0),
is(
"[scope] Invalid time value [-3d]. [key] must be a valid time value string: failed to parse setting [key] "
+ "with value [-3d] as a time value: negative durations are not supported"
)
);
}

public void testGetEmbeddingSize_ReturnsError_WhenTextEmbeddingResults_IsEmpty() {
var service = mock(InferenceService.class);

Expand Down

0 comments on commit 3f65235

Please sign in to comment.