forked from ash-project/ash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrange_test.exs
More file actions
761 lines (617 loc) · 27.7 KB
/
Copy pathrange_test.exs
File metadata and controls
761 lines (617 loc) · 27.7 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
# SPDX-FileCopyrightText: 2019 ash contributors <https://github.com/ash-project/ash/graphs/contributors>
#
# SPDX-License-Identifier: MIT
defmodule Ash.Type.RangeTest do
use ExUnit.Case, async: true
alias Ash.Range
{:ok, constraints} =
Ash.Type.init(Ash.Type.Range,
inner_type: :datetime,
inner_constraints: [precision: :microsecond]
)
{:ok, date_constraints} = Ash.Type.init(Ash.Type.Range, inner_type: :date)
{:ok, int_constraints} = Ash.Type.init(Ash.Type.Range, inner_type: :integer)
@constraints constraints
@date_constraints date_constraints
@int_constraints int_constraints
@lower ~U[2026-01-01 00:00:00.000000Z]
@upper ~U[2026-02-01 00:00:00.000000Z]
test "the :range short name resolves to Ash.Type.Range" do
assert Ash.Type.get_type(:range) == Ash.Type.Range
end
test "storage_type is the logical :range (data layer chooses the concrete type)" do
assert Ash.Type.Range.storage_type(@constraints) == :range
assert Ash.Type.Range.storage_type(@date_constraints) == :range
end
test "generator/1 produces ordered ranges of the inner type" do
{:ok, int_constraints} =
Ash.Type.init(Ash.Type.Range, inner_type: :integer, inner_constraints: [min: 0, max: 100])
int_constraints
|> Ash.Type.Range.generator()
|> Enum.take(50)
|> Enum.each(fn %Range{lower: lower, upper: upper, bounds: :"[)"} ->
assert is_integer(lower) and lower in 0..100
assert is_integer(upper) and upper in 0..100
assert lower <= upper
end)
end
test "cast_input from an Ash.Range struct casts the bounds via the inner type" do
assert {:ok, %Range{lower: @lower, upper: @upper, bounds: :"[)"}} =
Ash.Type.cast_input(
Ash.Type.Range,
%Range{lower: @lower, upper: @upper},
@constraints
)
end
test "cast_input from a {lower, upper} tuple defaults bounds to [)" do
assert {:ok, %Range{lower: @lower, upper: @upper, bounds: :"[)"}} =
Ash.Type.cast_input(Ash.Type.Range, {@lower, @upper}, @constraints)
end
test "cast_input from a bounds string resolves it to the atom" do
assert {:ok, %Range{lower: @lower, upper: @upper, bounds: :"[]"}} =
Ash.Type.cast_input(
Ash.Type.Range,
%{lower: @lower, upper: @upper, bounds: "[]"},
@constraints
)
end
test "cast_input refuses a bounds that is not a bounds specifier" do
for bounds <- [:not_a_bound, :inclusive_inclusive, :"[x", "nonsense", 5] do
assert {:error, _} =
Ash.Type.cast_input(
Ash.Type.Range,
%{lower: 1, upper: 9, bounds: bounds},
@int_constraints
),
"expected #{inspect(bounds)} to be refused"
end
end
test "an unrecognized bounds is refused rather than read as exclusive at both ends" do
assert {:error, _} =
Ash.Type.cast_input(
Ash.Type.Range,
%Range{lower: 1, upper: 9, bounds: :not_a_bound},
@int_constraints
)
end
test "cast_input refuses a value that is not a range" do
for value <- [~U[2026-01-01 00:00:00Z], ~D[2026-01-01], Decimal.new(1), "nope", 5] do
assert {:error, _} = Ash.Type.cast_input(Ash.Type.Range, value, @constraints),
"expected #{inspect(value)} to be refused"
end
end
test "cast_stored refuses a value that is not a range" do
assert {:error, _} =
Ash.Type.cast_stored(Ash.Type.Range, ~U[2026-01-01 00:00:00Z], @constraints)
end
test "apply_constraints refuses a value that is not a range" do
assert {:error, _} =
Ash.Type.apply_constraints(Ash.Type.Range, ~U[2026-01-01 00:00:00Z], @constraints)
assert {:error, _} =
Ash.Type.apply_constraints(Ash.Type.Range, %{lower: 1, upper: 5}, @int_constraints)
end
test "cast_stored refuses a bounds that is not a bounds specifier" do
assert {:error, _} =
Ash.Type.cast_stored(
Ash.Type.Range,
%{"lower" => 1, "upper" => 9, "bounds" => "nonsense"},
@int_constraints
)
end
test "dump_to_native refuses a bounds that is not a bounds specifier" do
assert :error =
Ash.Type.dump_to_native(
Ash.Type.Range,
%Range{lower: 1, upper: 9, bounds: :not_a_bound},
@int_constraints
)
assert {:ok, %Range{bounds: :"[]"}} =
Ash.Type.dump_to_native(
Ash.Type.Range,
%Range{lower: 1, upper: 9, bounds: :"[]"},
@int_constraints
)
end
test "apply_constraints refuses a bounds that is not a bounds specifier" do
assert {:error, _} =
Ash.Type.apply_constraints(
Ash.Type.Range,
%Range{lower: 1, upper: 9, bounds: :not_a_bound},
@int_constraints
)
end
test "a nil bound is an unbounded end" do
assert {:ok, %Range{lower: @lower, upper: nil}} =
Ash.Type.cast_input(Ash.Type.Range, %Range{lower: @lower, upper: nil}, @constraints)
end
test "round-trips through dump_to_native and cast_stored" do
{:ok, range} =
Ash.Type.cast_input(Ash.Type.Range, %Range{lower: @lower, upper: @upper}, @constraints)
{:ok, native} = Ash.Type.dump_to_native(Ash.Type.Range, range, @constraints)
assert %{lower: _, upper: _, bounds: :"[)"} = native
assert {:ok, ^range} = Ash.Type.cast_stored(Ash.Type.Range, native, @constraints)
end
test "apply_constraints rejects a lower bound greater than the upper" do
{:ok, range} =
Ash.Type.cast_input(Ash.Type.Range, %Range{lower: @upper, upper: @lower}, @constraints)
assert {:error, _} = Ash.Type.apply_constraints(Ash.Type.Range, range, @constraints)
end
describe "bound constraints" do
defp init!(constraints), do: Ash.Type.init(Ash.Type.Range, constraints) |> elem(1)
defp apply!(range, constraints),
do: Ash.Type.apply_constraints(Ash.Type.Range, range, constraints)
test "a required bound must be there" do
constraints = init!(inner_type: :integer, lower: [required?: true])
assert {:error, _} = apply!(%Range{lower: nil, upper: 5, bounds: :"()"}, constraints)
assert {:ok, %Range{lower: 1}} = apply!(%Range{lower: 1, upper: 5}, constraints)
end
test "each end is required independently" do
constraints = init!(inner_type: :integer, upper: [required?: true])
assert {:error, _} = apply!(%Range{lower: 1, upper: nil}, constraints)
assert {:ok, _} = apply!(%Range{lower: nil, upper: 5, bounds: :"()"}, constraints)
end
test "a bound must carry the inclusivity asked for" do
constraints = init!(inner_type: :datetime, upper: [inclusive?: false])
assert {:ok, _} = apply!(%Range{lower: @lower, upper: @upper, bounds: :"[)"}, constraints)
assert {:error, _} =
apply!(%Range{lower: @lower, upper: @upper, bounds: :"[]"}, constraints)
end
test "inclusivity is not checked on an end that is not there" do
constraints = init!(inner_type: :datetime, lower: [inclusive?: true])
assert {:ok, _} = apply!(%Range{lower: nil, upper: @upper, bounds: :"()"}, constraints)
end
test "a discrete range canonicalized to an exclusive lower still satisfies inclusive?" do
# `(,5]` canonicalizes to `(,6)`, so the lower is exclusive only because it is absent.
constraints = init!(inner_type: :integer, lower: [inclusive?: true])
{:ok, range} =
Ash.Type.cast_input(
Ash.Type.Range,
%Range{lower: nil, upper: 5, bounds: :"(]"},
constraints
)
assert {:ok, _} = apply!(range, constraints)
end
test "unconstrained by default" do
constraints = init!(inner_type: :integer)
assert {:ok, _} = apply!(%Range{lower: nil, upper: nil, bounds: :"()"}, constraints)
end
test "a period is the three constraints together" do
constraints =
init!(
inner_type: :datetime,
inner_constraints: [precision: :microsecond],
lower: [required?: true, inclusive?: true],
upper: [inclusive?: false]
)
assert {:ok, _} = apply!(%Range{lower: @lower, upper: nil, bounds: :"[)"}, constraints)
assert {:error, _} = apply!(%Range{lower: nil, upper: @upper, bounds: :"()"}, constraints)
assert {:error, _} =
apply!(%Range{lower: @lower, upper: @upper, bounds: :"[]"}, constraints)
assert {:error, _} = apply!(Range.empty(), constraints)
end
end
describe "Allen relations" do
defp ar(lower, upper, bounds \\ :"[)"),
do: %Range{lower: lower, upper: upper, bounds: bounds}
defp pairs do
[
precedes: {ar(1, 3), ar(5, 7)},
meets: {ar(1, 5), ar(5, 7)},
overlaps: {ar(1, 5), ar(3, 7)},
finished_by: {ar(1, 7), ar(3, 7)},
contains: {ar(1, 9), ar(3, 7)},
starts: {ar(1, 5), ar(1, 7)},
equals: {ar(1, 5), ar(1, 5)},
started_by: {ar(1, 7), ar(1, 5)},
during: {ar(3, 5), ar(1, 7)},
finishes: {ar(3, 7), ar(1, 7)},
overlapped_by: {ar(3, 7), ar(1, 5)},
met_by: {ar(5, 7), ar(1, 5)},
preceded_by: {ar(5, 7), ar(1, 3)}
]
end
test "each of the thirteen" do
for {expected, {left, right}} <- pairs() do
assert Range.relation(left, right) == expected
end
end
test "the pairs cover every relation, in canonical order" do
assert Keyword.keys(pairs()) == Range.relations()
end
test "converses mirror: swapping the operands walks the order inward" do
for {expected, {left, right}} <- pairs() do
converse =
Enum.at(Range.relations(), 12 - Enum.find_index(Range.relations(), &(&1 == expected)))
assert Range.relation(right, left) == converse
end
end
test "meets needs exactly one side to include the point" do
assert Range.relation(ar(1, 5, :"[)"), ar(5, 9, :"[)")) == :meets
assert Range.relation(ar(1, 5, :"[]"), ar(5, 9, :"[)")) == :overlaps
assert Range.relation(ar(1, 5, :"[)"), ar(5, 9, :"()")) == :precedes
end
test "an unbounded end reaches past every bound" do
assert Range.relation(ar(nil, nil, :"()"), ar(1, 5)) == :contains
assert Range.relation(ar(1, nil), ar(1, 5)) == :started_by
assert Range.relation(ar(nil, 5, :"()"), ar(1, 5)) == :finished_by
end
test "an empty range has no relation" do
assert Range.relation(Range.empty(), ar(1, 5)) == nil
assert Range.relation(ar(1, 5), Range.empty()) == nil
assert Range.relation(Range.empty(), Range.empty()) == nil
end
end
describe "adjacent?/2" do
# Cross-checked against Postgres 19: `-|-` on numrange and int4range agrees case
# for case, canonical form included.
defp adj(lower, upper, bounds), do: %Range{lower: lower, upper: upper, bounds: bounds}
test "one ends where the other begins, with exactly one side including the seam" do
assert Range.adjacent?(adj(1, 5, :"[)"), adj(5, 9, :"[)"))
assert Range.adjacent?(adj(1, 5, :"()"), adj(5, 9, :"[)"))
assert Range.adjacent?(adj(1, 5, :"[)"), adj(5, 9, :"[]"))
end
test "sharing the seam is overlap, and excluding it from both leaves a gap" do
refute Range.adjacent?(adj(1, 5, :"[]"), adj(5, 9, :"[)"))
refute Range.adjacent?(adj(1, 5, :"()"), adj(5, 9, :"()"))
refute Range.adjacent?(adj(1, 5, :"[)"), adj(6, 9, :"[)"))
end
test "is symmetric, where Allen's meets is directional" do
left = adj(1, 5, :"[)")
right = adj(5, 9, :"[)")
assert Range.relation(left, right) == :meets
assert Range.relation(right, left) == :met_by
assert Range.adjacent?(left, right)
assert Range.adjacent?(right, left)
end
test "an empty range is adjacent to nothing, not even itself" do
refute Range.adjacent?(Range.empty(), adj(1, 9, :"[)"))
refute Range.adjacent?(adj(1, 9, :"[)"), Range.empty())
refute Range.adjacent?(Range.empty(), Range.empty())
end
test "a discrete inner type answers on the canonical form" do
{:ok, constraints} = Ash.Type.init(Ash.Type.Range, inner_type: :integer)
{:ok, left} = Ash.Type.cast_input(Ash.Type.Range, adj(1, 4, :"[]"), constraints)
{:ok, right} = Ash.Type.cast_input(Ash.Type.Range, adj(5, 9, :"[)"), constraints)
assert {left.lower, left.upper, left.bounds} == {1, 5, :"[)"}
assert Range.adjacent?(left, right)
refute Range.adjacent?(adj(1, 4, :"[]"), adj(5, 9, :"[)"))
end
test "adjacent ranges tile, covering everything between without overlapping" do
tiles = [adj(1, 5, :"[)"), adj(5, 9, :"[)"), adj(9, 13, :"[)")]
assert tiles
|> Enum.chunk_every(2, 1, :discard)
|> Enum.all?(fn [a, b] ->
Range.adjacent?(a, b)
end)
end
end
describe "ordering" do
# Expectations are Postgres's own answers, on `numrange` so canonicalization
# doesn't rewrite the bounds first.
defp r(lower, upper, bounds \\ :"[)"), do: %Range{lower: lower, upper: upper, bounds: bounds}
test "orders by lower bound first" do
assert Ash.Range.compare(r(1, 9), r(5, 9)) == :lt
assert Ash.Range.compare(r(5, 9), r(1, 9)) == :gt
end
test "orders by upper bound where lowers are equal" do
assert Ash.Range.compare(r(1, 3), r(1, 9)) == :lt
end
test "identical ranges are equal" do
assert Ash.Range.compare(r(1, 9), r(1, 9)) == :eq
end
test "an inclusive lower starts before an exclusive one at the same value" do
assert Ash.Range.compare(r(1, 5, :"[)"), r(1, 5, :"()")) == :lt
end
test "an exclusive upper ends before an inclusive one at the same value" do
assert Ash.Range.compare(r(1, 5, :"[)"), r(1, 5, :"[]")) == :lt
end
test "an unbounded lower is minus infinity" do
assert Ash.Range.compare(r(nil, 5, :"()"), r(1, 5)) == :lt
end
test "an unbounded upper is plus infinity" do
assert Ash.Range.compare(r(1, nil), r(1, 5)) == :gt
end
test "the empty range sorts first" do
assert Ash.Range.compare(Range.empty(), r(nil, nil, :"()")) == :lt
assert Ash.Range.compare(Range.empty(), Range.empty()) == :eq
end
test "Comp uses the comparator rather than term order" do
assert Comp.compare(r(1, 9, :"()"), r(1, 3, :"[)")) == :gt
end
test "sorting agrees with Postgres" do
sorted =
[r(1, 5), r(nil, nil, :"()"), r(1, 3), Range.empty(), r(5, 9)]
|> Enum.sort(Ash.Range)
assert [
%Range{empty?: true},
%Range{lower: nil, upper: nil},
%Range{lower: 1, upper: 3},
%Range{lower: 1, upper: 5},
%Range{lower: 5, upper: 9}
] = sorted
end
end
describe "discrete ranges" do
# Every expectation here is what Postgres renders for the same range, e.g.
# `'[1,5]'::int4range` is `[1,6)` and `'(1,2)'::int4range` is `empty`.
setup do
{:ok, int} = Ash.Type.init(Ash.Type.Range, inner_type: :integer)
{:ok, date} = Ash.Type.init(Ash.Type.Range, inner_type: :date)
%{int: int, date: date}
end
defp cast!(range, constraints) do
{:ok, cast} = Ash.Type.cast_input(Ash.Type.Range, range, constraints)
cast
end
test "an inclusive upper moves on to the next value", %{int: int} do
assert %Range{lower: 1, upper: 6, bounds: :"[)"} =
cast!(%Range{lower: 1, upper: 5, bounds: :"[]"}, int)
end
test "an exclusive lower moves on to the next value", %{int: int} do
assert %Range{lower: 2, upper: 5, bounds: :"[)"} =
cast!(%Range{lower: 1, upper: 5, bounds: :"()"}, int)
end
test "both bounds move where both are non-canonical", %{int: int} do
assert %Range{lower: 2, upper: 6, bounds: :"[)"} =
cast!(%Range{lower: 1, upper: 5, bounds: :"(]"}, int)
end
test "a range already in canonical form is unchanged", %{int: int} do
assert %Range{lower: 1, upper: 5, bounds: :"[)"} =
cast!(%Range{lower: 1, upper: 5, bounds: :"[)"}, int)
end
test "spellings of the same set cast to one value", %{int: int} do
assert cast!(%Range{lower: 1, upper: 5, bounds: :"[]"}, int) ==
cast!(%Range{lower: 1, upper: 6, bounds: :"[)"}, int)
end
test "a single point keeps its point", %{int: int} do
assert %Range{lower: 5, upper: 6, bounds: :"[)"} =
cast!(%Range{lower: 5, upper: 5, bounds: :"[]"}, int)
end
test "bounds with no value between them are empty", %{int: int} do
assert %Range{empty?: true} = cast!(%Range{lower: 1, upper: 2, bounds: :"()"}, int)
end
test "an unbounded end is exclusive", %{int: int} do
assert %Range{lower: nil, upper: 6, bounds: :"()"} =
cast!(%Range{lower: nil, upper: 5, bounds: :"[]"}, int)
assert %Range{lower: 1, upper: nil, bounds: :"[)"} =
cast!(%Range{lower: 1, upper: nil, bounds: :"[]"}, int)
end
test "dates canonicalise by day", %{date: date} do
assert %Range{lower: ~D[2026-01-01], upper: ~D[2026-01-09], bounds: :"[)"} =
cast!(%Range{lower: ~D[2026-01-01], upper: ~D[2026-01-08], bounds: :"[]"}, date)
end
test "a lower bound above its upper is left for apply_constraints to reject", %{int: int} do
range = cast!(%Range{lower: 5, upper: 1, bounds: :"[]"}, int)
assert %Range{lower: 5, upper: 1, bounds: :"[]"} = range
assert {:error, _} = Ash.Type.apply_constraints(Ash.Type.Range, range, int)
end
test "a continuous inner type is left as written" do
assert %Range{lower: @lower, upper: @upper, bounds: :"[]"} =
cast!(%Range{lower: @lower, upper: @upper, bounds: :"[]"}, @constraints)
end
test "canonicalisation applies on the way out of storage too", %{int: int} do
assert {:ok, %Range{lower: 1, upper: 6, bounds: :"[)"}} =
Ash.Type.cast_stored(
Ash.Type.Range,
%Range{lower: 1, upper: 5, bounds: :"[]"},
int
)
end
test "apply_constraints canonicalises a value that skipped casting", %{int: int} do
assert {:ok, %Range{lower: 1, upper: 6, bounds: :"[)"}} =
Ash.Type.apply_constraints(
Ash.Type.Range,
%Range{lower: 1, upper: 5, bounds: :"[]"},
int
)
end
end
describe "empty ranges" do
test "a discrete range on one point is empty unless both bounds include it" do
{:ok, constraints} = Ash.Type.init(Ash.Type.Range, inner_type: :integer)
cast = fn bounds ->
{:ok, cast} =
Ash.Type.cast_input(
Ash.Type.Range,
%Range{lower: 5, upper: 5, bounds: bounds},
constraints
)
cast
end
# Excluding either end leaves no points. Canonicalizing must see that before the
# discrete shift, which would otherwise turn `(5, 5)` into `[6, 5)` and lose the
# equality the rule tests. Postgres agrees on all four.
assert cast.(:"[)") == Range.empty()
assert cast.(:"(]") == Range.empty()
assert cast.(:"()") == Range.empty()
# Including both ends holds the point, so it is the single-point range.
assert cast.(:"[]") == %Range{lower: 5, upper: 6, bounds: :"[)"}
end
test "an inverted range is still refused, as Postgres refuses it" do
{:ok, constraints} = Ash.Type.init(Ash.Type.Range, inner_type: :integer)
{:ok, cast} =
Ash.Type.cast_input(
Ash.Type.Range,
%Range{lower: 9, upper: 2, bounds: :"[)"},
constraints
)
assert {:error, _} = Ash.Type.apply_constraints(Ash.Type.Range, cast, constraints)
end
test "a range admitting no points casts to the empty range" do
assert {:ok, %Range{empty?: true, lower: nil, upper: nil}} =
Ash.Type.cast_input(
Ash.Type.Range,
%Range{lower: @lower, upper: @lower},
@constraints
)
end
test "every empty range casts to the same value" do
{:ok, at_lower} =
Ash.Type.cast_input(Ash.Type.Range, %Range{lower: @lower, upper: @lower}, @constraints)
{:ok, at_upper} =
Ash.Type.cast_input(Ash.Type.Range, %Range{lower: @upper, upper: @upper}, @constraints)
assert at_lower == at_upper
assert at_lower == Range.empty()
end
test "a range with both bounds inclusive is the single point, not empty" do
assert {:ok, %Range{empty?: false, lower: @lower, upper: @lower}} =
Ash.Type.cast_input(
Ash.Type.Range,
%Range{lower: @lower, upper: @lower, bounds: :"[]"},
@constraints
)
end
test "an unbounded range is not empty" do
assert {:ok, %Range{empty?: false}} =
Ash.Type.cast_input(Ash.Type.Range, %Range{lower: nil, upper: nil}, @constraints)
end
test "the empty range survives a round trip through a data layer" do
{:ok, range} =
Ash.Type.cast_input(Ash.Type.Range, %Range{lower: @lower, upper: @lower}, @constraints)
{:ok, native} = Ash.Type.dump_to_native(Ash.Type.Range, range, @constraints)
assert {:ok, %Range{empty?: true}} =
Ash.Type.cast_stored(Ash.Type.Range, native, @constraints)
end
test "a lower bound above the upper is rejected, not emptied" do
{:ok, range} =
Ash.Type.cast_input(Ash.Type.Range, %Range{lower: @upper, upper: @lower}, @constraints)
refute range.empty?
assert {:error, _} = Ash.Type.apply_constraints(Ash.Type.Range, range, @constraints)
end
test "an empty range is refused unless the attribute allows it" do
assert {:error, _} =
Ash.Type.apply_constraints(Ash.Type.Range, Range.empty(), @constraints)
end
test "allow_empty? keeps the empty range" do
{:ok, constraints} =
Ash.Type.init(Ash.Type.Range, inner_type: :integer, allow_empty?: true)
assert {:ok, %Range{empty?: true}} =
Ash.Type.apply_constraints(Ash.Type.Range, Range.empty(), constraints)
end
test "empty?/1 reports emptiness whether canonical or implied by the bounds" do
assert Range.empty?(Range.empty())
assert Range.empty?(%Range{lower: 5, upper: 5, bounds: :"[)"})
assert Range.empty?(%Range{lower: 6, upper: 5, bounds: :"[]"})
refute Range.empty?(%Range{lower: 5, upper: 5, bounds: :"[]"})
refute Range.empty?(%Range{lower: 5, upper: 6, bounds: :"[)"})
refute Range.empty?(%Range{lower: nil, upper: nil})
end
end
describe "contains?/2" do
test "a bounded range holds values between its bounds" do
range = %Ash.Range{lower: 1, upper: 5, bounds: :"[)"}
refute Ash.Range.contains?(range, 0)
assert Ash.Range.contains?(range, 1)
assert Ash.Range.contains?(range, 4)
refute Ash.Range.contains?(range, 5)
end
test "a bound holds its own value only when inclusive" do
assert Ash.Range.contains?(%Ash.Range{lower: 1, upper: 5, bounds: :"[]"}, 5)
refute Ash.Range.contains?(%Ash.Range{lower: 1, upper: 5, bounds: :"()"}, 1)
assert Ash.Range.contains?(%Ash.Range{lower: 1, upper: 5, bounds: :"(]"}, 5)
refute Ash.Range.contains?(%Ash.Range{lower: 1, upper: 5, bounds: :"(]"}, 1)
end
test "an unbounded end holds everything beyond it" do
assert Ash.Range.contains?(%Ash.Range{lower: nil, upper: 5}, -1_000)
assert Ash.Range.contains?(%Ash.Range{lower: 1, upper: nil}, 1_000)
assert Ash.Range.contains?(%Ash.Range{lower: nil, upper: nil}, 0)
end
test "an empty range holds nothing" do
refute Ash.Range.contains?(%Ash.Range{lower: 5, upper: 5, bounds: :"[)"}, 5)
refute Ash.Range.contains?(%Ash.Range{lower: 9, upper: 5}, 7)
end
test "holds datetimes by Comp, not by term order" do
range = %Ash.Range{
lower: ~U[2026-01-31 00:00:00Z],
upper: ~U[2026-03-01 00:00:00Z],
bounds: :"[)"
}
assert Ash.Range.contains?(range, ~U[2026-02-01 00:00:00Z])
refute Ash.Range.contains?(range, ~U[2026-03-02 00:00:00Z])
end
end
describe "contains?/2 with a range" do
# Cross-checked against Postgres 19: `@>` on int4range agrees case for case.
test "a range holds one that lies within it, sharing an endpoint or equal included" do
outer = %Range{lower: 1, upper: 10, bounds: :"[)"}
assert Range.contains?(outer, %Range{lower: 3, upper: 5, bounds: :"[)"})
assert Range.contains?(outer, %Range{lower: 1, upper: 5, bounds: :"[)"})
assert Range.contains?(outer, %Range{lower: 5, upper: 10, bounds: :"[)"})
assert Range.contains?(outer, outer)
refute Range.contains?(outer, %Range{lower: 5, upper: 20, bounds: :"[)"})
end
test "every range holds the empty range, and the empty range holds nothing else" do
empty = Range.empty()
range = %Range{lower: 1, upper: 10, bounds: :"[)"}
assert Range.contains?(range, empty)
assert Range.contains?(empty, empty)
refute Range.contains?(empty, range)
refute Range.contains?(empty, 5)
end
test "an unbounded end holds everything beyond it" do
assert Range.contains?(
%Range{lower: nil, upper: nil, bounds: :"[)"},
%Range{lower: 1, upper: 10, bounds: :"[)"}
)
refute Range.contains?(
%Range{lower: 1, upper: 10, bounds: :"[)"},
%Range{lower: nil, upper: 10, bounds: :"[)"}
)
end
end
describe "intersects?/2" do
test "two ranges sharing points intersect, in either order" do
left = %Range{lower: 1, upper: 5, bounds: :"[)"}
right = %Range{lower: 4, upper: 9, bounds: :"[)"}
assert Range.intersects?(left, right)
assert Range.intersects?(right, left)
end
test "a range contained in another intersects it" do
assert Range.intersects?(
%Range{lower: 1, upper: 9, bounds: :"[)"},
%Range{lower: 3, upper: 4, bounds: :"[)"}
)
end
test "adjacent ranges do not intersect, which is what lets them tile" do
refute Range.intersects?(
%Range{lower: 1, upper: 3, bounds: :"[)"},
%Range{lower: 3, upper: 5, bounds: :"[)"}
)
end
test "a shared boundary is a shared point only when both sides include it" do
assert Range.intersects?(
%Range{lower: 1, upper: 3, bounds: :"[]"},
%Range{lower: 3, upper: 5, bounds: :"[)"}
)
refute Range.intersects?(
%Range{lower: 1, upper: 3, bounds: :"[]"},
%Range{lower: 3, upper: 5, bounds: :"()"}
)
end
test "an unbounded end intersects everything beyond it" do
assert Range.intersects?(
%Range{lower: 1, upper: nil, bounds: :"[)"},
%Range{lower: 1_000, upper: nil, bounds: :"[)"}
)
assert Range.intersects?(
%Range{lower: nil, upper: nil, bounds: :"[)"},
%Range{lower: 3, upper: 4, bounds: :"[)"}
)
end
test "an empty range intersects nothing, not even itself" do
empty = %Range{lower: 5, upper: 5, bounds: :"[)"}
refute Range.intersects?(empty, empty)
refute Range.intersects?(empty, %Range{lower: nil, upper: nil, bounds: :"[)"})
end
test "compares datetimes by Comp, not by term order" do
assert Range.intersects?(
%Range{lower: ~U[2026-01-31 00:00:00Z], upper: nil, bounds: :"[)"},
%Range{lower: ~U[2026-02-01 00:00:00Z], upper: nil, bounds: :"[)"}
)
refute Range.intersects?(
%Range{lower: ~U[2026-01-31 00:00:00Z], upper: ~U[2026-02-01 00:00:00Z]},
%Range{lower: ~U[2026-02-01 00:00:00Z], upper: ~U[2026-03-01 00:00:00Z]}
)
end
end
end