]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/csharp/test/Apache.Arrow.Tests/Date64ArrayTests.cs
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / csharp / test / Apache.Arrow.Tests / Date64ArrayTests.cs
CommitLineData
1d09f67e
TL
1// Licensed to the Apache Software Foundation (ASF) under one or more
2// contributor license agreements. See the NOTICE file distributed with
3// this work for additional information regarding copyright ownership.
4// The ASF licenses this file to You under the Apache License, Version 2.0
5// (the "License"); you may not use this file except in compliance with
6// the License. You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16using System;
17using System.Collections.Generic;
18using System.Linq;
19using Xunit;
20
21namespace Apache.Arrow.Tests
22{
23 public class Date64ArrayTests
24 {
25 private const long MillisecondsPerDay = 86400000;
26
27 public static IEnumerable<object[]> GetDatesData() =>
28 TestDateAndTimeData.ExampleDates.Select(d => new object[] { d });
29
30 public static IEnumerable<object[]> GetDateTimesData() =>
31 TestDateAndTimeData.ExampleDateTimes.Select(dt => new object[] { dt });
32
33 public static IEnumerable<object[]> GetDateTimeOffsetsData() =>
34 TestDateAndTimeData.ExampleDateTimeOffsets.Select(dto => new object[] { dto });
35
36 public class AppendNull
37 {
38 [Fact]
39 public void AppendThenGetGivesNull()
40 {
41 // Arrange
42 var builder = new Date64Array.Builder();
43
44 // Act
45 builder = builder.AppendNull();
46
47 // Assert
48 var array = builder.Build();
49 Assert.Equal(1, array.Length);
50 Assert.Null(array.GetDateTime(0));
51 Assert.Null(array.GetDateTimeOffset(0));
52 Assert.Null(array.GetValue(0));
53 }
54 }
55
56 public class AppendDateTime
57 {
58 [Theory]
59 [MemberData(nameof(GetDatesData), MemberType = typeof(Date64ArrayTests))]
60 public void AppendDateGivesSameDate(DateTime date)
61 {
62 // Arrange
63 var builder = new Date64Array.Builder();
64 var expectedDateTime = date;
65 var expectedDateTimeOffset =
66 new DateTimeOffset(DateTime.SpecifyKind(date, DateTimeKind.Unspecified), TimeSpan.Zero);
67 long expectedValue = (long)date.Subtract(new DateTime(1970, 1, 1)).TotalDays * MillisecondsPerDay;
68
69 // Act
70 builder = builder.Append(date);
71
72 // Assert
73 var array = builder.Build();
74 Assert.Equal(1, array.Length);
75 Assert.Equal(expectedDateTime, array.GetDateTime(0));
76 Assert.Equal(expectedDateTimeOffset, array.GetDateTimeOffset(0));
77 Assert.Equal(expectedValue, array.GetValue(0));
78 Assert.Equal(0, array.GetValue(0).Value % MillisecondsPerDay);
79 }
80
81 [Theory]
82 [MemberData(nameof(GetDateTimesData), MemberType = typeof(Date64ArrayTests))]
83 public void AppendWithTimeGivesSameWithTimeIgnored(DateTime dateTime)
84 {
85 // Arrange
86 var builder = new Date64Array.Builder();
87 var expectedDateTime = dateTime.Date;
88 var expectedDateTimeOffset =
89 new DateTimeOffset(DateTime.SpecifyKind(dateTime.Date, DateTimeKind.Unspecified), TimeSpan.Zero);
90 long expectedValue =
91 (long)dateTime.Date.Subtract(new DateTime(1970, 1, 1)).TotalDays * MillisecondsPerDay;
92
93 // Act
94 builder = builder.Append(dateTime);
95
96 // Assert
97 var array = builder.Build();
98 Assert.Equal(1, array.Length);
99 Assert.Equal(expectedDateTime, array.GetDateTime(0));
100 Assert.Equal(expectedDateTimeOffset, array.GetDateTimeOffset(0));
101 Assert.Equal(expectedValue, array.GetValue(0));
102 Assert.Equal(0, array.GetValue(0).Value % MillisecondsPerDay);
103 }
104 }
105
106 public class AppendDateTimeOffset
107 {
108 [Theory]
109 [MemberData(nameof(GetDateTimeOffsetsData), MemberType = typeof(Date64ArrayTests))]
110 public void AppendGivesUtcDate(DateTimeOffset dateTimeOffset)
111 {
112 // Arrange
113 var builder = new Date64Array.Builder();
114 var expectedDateTime = dateTimeOffset.UtcDateTime.Date;
115 var expectedDateTimeOffset = new DateTimeOffset(dateTimeOffset.UtcDateTime.Date, TimeSpan.Zero);
116 long expectedValue =
117 (long)dateTimeOffset.UtcDateTime.Date.Subtract(new DateTime(1970, 1, 1)).TotalDays *
118 MillisecondsPerDay;
119
120 // Act
121 builder = builder.Append(dateTimeOffset);
122
123 // Assert
124 var array = builder.Build();
125 Assert.Equal(1, array.Length);
126 Assert.Equal(expectedDateTime, array.GetDateTime(0));
127 Assert.Equal(expectedDateTimeOffset, array.GetDateTimeOffset(0));
128 Assert.Equal(expectedValue, array.GetValue(0));
129 Assert.Equal(0, array.GetValue(0).Value % MillisecondsPerDay);
130 }
131 }
132 }
133}