Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Replace document/ref models with generic ref #182

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/workflows/pr_validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
jobs:
dotnet-test:

runs-on: ubuntu-latest
runs-on: ubuntu-22.04
strategy:
matrix:
dotnet-version: [ '6.0.x', '8.0.x' ]
Expand All @@ -23,10 +23,9 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Setup dotnet ${{ matrix.dotnet-version }}
uses: actions/setup-dotnet@v4
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ matrix.dotnet-version }}

- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
97 changes: 97 additions & 0 deletions Fauna.Test/E2E/E2ELinq.Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System.Diagnostics.CodeAnalysis;
using Fauna.Exceptions;
using Fauna.Linq;
using Fauna.Mapping;
using Fauna.Types;
using NUnit.Framework;
using static Fauna.Query;
using static Fauna.Test.Helpers.TestClientHelper;

namespace Fauna.Test.E2E;


public class E2ELinqTestDb : DataContext
{
public class E2ELinqTestAuthor
{
[Id] public string? Id { get; set; }
[AllowNull] public string Name { get; set; }
}

public class E2ELinqTestBook
{
[Id] public string? Id { get; set; }
[AllowNull] public string Name { get; set; }
[AllowNull] public Ref<E2ELinqTestAuthor> Author { get; set; }
}


[Name("E2ELinqTest_Author")]
public class AuthorCol : Collection<E2ELinqTestAuthor> { }

[Name("E2ELinqTest_Book")]
public class BookCol : Collection<E2ELinqTestBook> { }

public AuthorCol Author { get => GetCollection<AuthorCol>(); }

public BookCol Book { get => GetCollection<BookCol>(); }
}

public class E2ELinqTest
{
private static readonly Client s_client = GetLocalhostClient();
private static readonly E2ELinqTestDb s_db = s_client.DataContext<E2ELinqTestDb>();

[OneTimeSetUp]
public void SetUp()
{
s_db.QueryAsync(FQL($"Collection.byName('E2ELinqTest_Author')?.delete()")).Wait();
s_db.QueryAsync(FQL($"Collection.byName('E2ELinqTest_Book')?.delete()")).Wait();
s_db.QueryAsync(FQL($"Collection.create({{name: 'E2ELinqTest_Author'}})")).Wait();
s_db.QueryAsync(FQL($"Collection.create({{name: 'E2ELinqTest_Book'}})")).Wait();
var res = s_db.QueryAsync<Ref<Dictionary<string, object>>>(FQL($"E2ELinqTest_Author.create({{name: 'Leo Tolstoy'}})")).Result;
s_db.QueryAsync(FQL($"E2ELinqTest_Book.create({{name: 'War and Peace', author: {res.Data} }})")).Wait();
}

[Test]
public async Task E2ELinq_ObtainRefWithoutProjection()
{
var res = await s_db.Book
.Where(b => b.Name == "War and Peace")
.SingleAsync();
Assert.AreEqual("War and Peace", res.Name);
Assert.IsNotNull(res.Author.Id);
Assert.AreEqual(new Module("E2ELinqTest_Author"), res.Author.Collection);
Assert.IsNull(res.Author.Exists);
Assert.IsFalse(res.Author.IsLoaded);
}

[Test]
public async Task E2ELinq_ProjectRefIntoDocument()
{
var res = await s_db.Book
.Where(b => b.Name == "War and Peace")
.Select(b => new { b.Name, Author = new { b.Author.Get().Name } })
.SingleAsync();
Assert.AreEqual("War and Peace", res.Name);
Assert.AreEqual("Leo Tolstoy", res.Author.Name);
}

[Test]
public void E2ELinq_ProjectDeletedRefThrows()
{
var aut = s_db.QueryAsync<Ref<Dictionary<string, object>>>(FQL($"E2ELinqTest_Author.create({{name: 'Mary Shelley'}})")).Result;
s_db.QueryAsync(FQL($"E2ELinqTest_Book.create({{name: 'Frankenstein', author: {aut.Data} }})")).Wait();
s_db.QueryAsync(FQL($"E2ELinqTest_Author.where(a => a.name == 'Mary Shelley').forEach(d => d.delete())")).Wait();

var ex = Assert.Throws<NullDocumentException>(() => s_db.Book
.Where(b => b.Name == "Frankenstein")
.Select(b => new { b.Name, Author = new { b.Author.Get().Name } })
.Single())!;

Assert.IsNotNull(ex.Id);
Assert.IsNull(ex.Name);
Assert.AreEqual(new Module("E2ELinqTest_Author"), ex.Collection);
Assert.AreEqual("not found", ex.Cause);
}
}
3 changes: 3 additions & 0 deletions Fauna.Test/Fauna.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<Link>setup/fauna/%(Filename)%(Extension)</Link>
</Content>
</ItemGroup>
<ItemGroup>
<Folder Include="Types\" />
</ItemGroup>
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
28 changes: 8 additions & 20 deletions Fauna.Test/Integration.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,34 +248,22 @@ public async Task Paginate_EmbeddedSetFlattened()
}

[Test]
public void NullNamedDocumentThrowsNullDocumentException()
public async Task NullNamedDocument()
{
var q = FQL($"Collection.byName('Fake')");
var e = Assert.ThrowsAsync<NullDocumentException>(async () => await _client.QueryAsync<NamedDocument>(q));
var r = await _client.QueryAsync<NamedRef<Dictionary<string, object>>>(q);
var d = r.Data;
Assert.AreEqual("Fake", d.Name);
Assert.AreEqual("Collection", d.Collection.Name);
Assert.AreEqual("not found", d.Cause);

var e = Assert.Throws<NullDocumentException>(() => d.Get());
Assert.NotNull(e);
Assert.AreEqual("Fake", e!.Name);
Assert.AreEqual("Collection", e.Collection.Name);
Assert.AreEqual("not found", e.Cause);
}

[Test]
public async Task NullNamedDocument()
{
var q = FQL($"Collection.byName('Fake')");
var r = await _client.QueryAsync<NullableDocument<NamedDocument>>(q);
switch (r.Data)
{
case NullDocument<NamedDocument> d:
Assert.AreEqual("Fake", d.Name);
Assert.AreEqual("Collection", d.Collection.Name);
Assert.AreEqual("not found", d.Cause);
break;
default:
Assert.Fail($"Expected NullDocument<NamedDocument> but received {r.Data.GetType()}");
break;
}
}

[Test]
public async Task StatsCollector()
{
Expand Down
2 changes: 1 addition & 1 deletion Fauna.Test/Performance/TestDataModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal class Product
public bool InStock { get; init; } = false;

[Field]
public Ref? Manufacturer { get; init; }
public Ref<Manufacturer>? Manufacturer { get; init; }
}

/// <summary>
Expand Down
Loading
Loading