-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathFieldTest.cs
154 lines (120 loc) · 4.72 KB
/
FieldTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using FaunaDB.Collections;
using FaunaDB.Types;
using NUnit.Framework;
using static FaunaDB.Types.Option;
namespace Test
{
[TestFixture]
public class FieldTest
{
[Test]
public void TestObjectKey()
{
var obj = ObjectV.With("foo", "bar");
Assert.AreEqual(StringV.Of("bar"),
obj.Get(Field.At("foo")));
Assert.AreEqual(None(),
obj.GetOption(Field.At("nonexistent")));
Assert.Throws(typeof(InvalidOperationException),
() => obj.Get(Field.At("nonexistent")),
"Cannot find path \"nonexistent\". Object key \"nonexistent\" not found");
}
[Test]
public void TestArrayIndex()
{
var array = ArrayV.Of("a string", 10);
Assert.AreEqual(StringV.Of("a string"),
array.Get(Field.At(0)));
Assert.AreEqual(LongV.Of(10),
array.Get(Field.At(1)));
Assert.AreEqual(None(),
array.GetOption(Field.At(1234)));
Assert.Throws(typeof(InvalidOperationException),
() => array.Get(Field.At(1234)),
"Cannot find path \"1234\". Array index \"1234\" not found");
}
[Test]
public void TestNestedObject()
{
var nested = ObjectV.With("foo", ObjectV.With("bar", "a string"));
Assert.AreEqual(StringV.Of("a string"),
nested.Get(Field.At("foo", "bar")));
Assert.Throws(typeof(InvalidOperationException),
() => nested.Get(Field.At("foo", "nonexistent")),
"Cannot find path \"foo/nonexistent\". Object key \"nonexistent\" not found");
}
[Test]
public void TestNestedArray()
{
var nested = ArrayV.Of(10, ArrayV.Of(1234, ArrayV.Of(4321)));
Assert.AreEqual(LongV.Of(10),
nested.Get(Field.At(0)));
Assert.AreEqual(LongV.Of(1234),
nested.Get(Field.At(1, 0)));
Assert.AreEqual(LongV.Of(4321),
nested.Get(Field.At(1, 1, 0)));
Assert.Throws(typeof(InvalidOperationException),
() => nested.Get(Field.At(1, 1, 1)),
"Cannot find path \"1/1/1\". Array index \"1\" not found");
}
[Test]
public void TestFieldConvertion()
{
var setRef = new Dictionary<string, Value>
{
{"@ref", "databases" },
};
var obj = ObjectV.With(
"string", "a string",
"bool", true,
"double", 3.14,
"long", 1234,
"ref", Native.DATABASES,
"setref", new SetRefV(setRef));
Assert.AreEqual("a string",
obj.Get(Field.At("string").To<string>()));
Assert.AreEqual(true,
obj.Get(Field.At("bool").To<bool>()));
Assert.AreEqual(3.14,
obj.Get(Field.At("double").To<double>()));
Assert.AreEqual(1234L,
obj.Get(Field.At("long").To<long>()));
Assert.AreEqual(Native.DATABASES,
obj.Get(Field.At("ref").To<RefV>()));
Assert.AreEqual(new SetRefV(setRef),
obj.Get(Field.At("setref").To<SetRefV>()));
}
[Test]
public void TestComplex()
{
var obj = ObjectV.With("foo", ArrayV.Of(1, 2, ObjectV.With("bar", "a string")));
Assert.AreEqual("a string",
obj.Get(Field.At("foo").At(Field.At(2)).At(Field.At("bar").To<string>())));
}
[Test]
public void TestCollect()
{
var array = ArrayV.Of("John", "Bill");
Assert.That(array.Collect(Field.To<string>()),
Is.EquivalentTo(new List<string> { "John", "Bill" }));
var obj = ObjectV.With("arrayOfNames", array);
Assert.That(obj.Get(Field.At("arrayOfNames").Collect(Field.To<string>())),
Is.EquivalentTo(new List<string> { "John", "Bill" }));
Assert.Throws(typeof(InvalidOperationException),
() => obj.Collect(Field.To<string>()),
"Cannot convert ObjectV to ArrayV");
}
[Test]
public void TestCollectComplex()
{
var array = ArrayV.Of(
ObjectV.With("name", ArrayV.Of("John")),
ObjectV.With("name", ArrayV.Of("Bill"))
);
Assert.That(array.Collect(Field.At("name").At(Field.At(0)).To<string>()),
Is.EquivalentTo(new List<string> { "John", "Bill", }));
}
}
}