openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Moderations/ModerationTests.cs
69lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Moderations; |
| 3 | using OpenAI.Tests.Utility; |
| 4 | using System.Collections.Generic; |
| 5 | using System.Threading.Tasks; |
| 6 | using static OpenAI.Tests.TestHelpers; |
| 7 | |
| 8 | namespace OpenAI.Tests.Moderations; |
| 9 | |
| 10 | [TestFixture(true)] |
| 11 | [TestFixture(false)] |
| 12 | [Parallelizable(ParallelScope.All)] |
| 13 | [Category("Moderations")] |
| 14 | public partial class ModerationTests : SyncAsyncTestBase |
| 15 | { |
| 16 | public ModerationTests(bool isAsync) : base(isAsync) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | [Test] |
| 21 | public async Task ClassifySingleInput() |
| 22 | { |
| 23 | ModerationClient client = GetTestClient<ModerationClient>(TestScenario.Moderations); |
| 24 | |
| 25 | const string input = "I am killing all my houseplants!"; |
| 26 | |
| 27 | ModerationResult moderation = IsAsync |
| 28 | ? await client.ClassifyTextInputAsync(input) |
| 29 | : client.ClassifyTextInput(input); |
| 30 | Assert.That(moderation, Is.Not.Null); |
| 31 | Assert.That(moderation.Flagged, Is.True); |
| 32 | Assert.That(moderation.Categories.Violence, Is.True); |
| 33 | Assert.That(moderation.CategoryScores.Violence, Is.GreaterThan(0.5)); |
| 34 | } |
| 35 | |
| 36 | [Test] |
| 37 | public async Task ClassifyMultipleInputs() |
| 38 | { |
| 39 | ModerationClient client = GetTestClient<ModerationClient>(TestScenario.Moderations); |
| 40 | |
| 41 | List<string> inputs = |
| 42 | [ |
| 43 | "I forgot to water my houseplants!", |
| 44 | "I am killing all my houseplants!" |
| 45 | ]; |
| 46 | |
| 47 | ModerationCollection moderations = IsAsync |
| 48 | ? await client.ClassifyTextInputsAsync(inputs) |
| 49 | : client.ClassifyTextInputs(inputs); |
| 50 | Assert.That(moderations, Is.Not.Null); |
| 51 | Assert.That(moderations.Count, Is.EqualTo(2)); |
| 52 | Assert.That(moderations.Model, Does.StartWith("text-moderation")); |
| 53 | Assert.That(moderations.Id, Is.Not.Null.Or.Empty); |
| 54 | |
| 55 | Assert.That(moderations[0], Is.Not.Null); |
| 56 | Assert.That(moderations[0].Flagged, Is.False); |
| 57 | |
| 58 | Assert.That(moderations[1], Is.Not.Null); |
| 59 | Assert.That(moderations[1].Flagged, Is.True); |
| 60 | Assert.That(moderations[1].Categories.Violence, Is.True); |
| 61 | Assert.That(moderations[1].CategoryScores.Violence, Is.GreaterThan(0.5)); |
| 62 | } |
| 63 | |
| 64 | [Test] |
| 65 | public void SerializeModerationCollection() |
| 66 | { |
| 67 | // TODO: Add this test. |
| 68 | } |
| 69 | } |
| 70 | |