|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * 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 | + */ |
| 16 | + |
| 17 | +package com.mongodb.client.model.expressions; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import java.time.Instant; |
| 22 | + |
| 23 | +import static com.mongodb.client.model.expressions.Expressions.of; |
| 24 | + |
| 25 | +class DateExpressionsFunctionalTest extends AbstractExpressionsFunctionalTest { |
| 26 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/#date-expression-operators |
| 27 | + |
| 28 | + private final Instant instant = Instant.parse("2007-12-03T10:15:30.005Z"); |
| 29 | + |
| 30 | + @Test |
| 31 | + public void literalsTest() { |
| 32 | + assertExpression( |
| 33 | + instant, |
| 34 | + of(instant), |
| 35 | + "{'$date': '2007-12-03T10:15:30.005Z'}"); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void yearTest() { |
| 40 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/year/ |
| 41 | + assertExpression( |
| 42 | + 2007, |
| 43 | + of(instant).year(), |
| 44 | + "{'$year': {'$date': '2007-12-03T10:15:30.005Z'}}"); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void monthTest() { |
| 49 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/month/ |
| 50 | + assertExpression( |
| 51 | + 12, |
| 52 | + of(instant).month(), |
| 53 | + "{'$month': {'$date': '2007-12-03T10:15:30.005Z'}}"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void dayOfMonthTest() { |
| 58 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfMonth/ |
| 59 | + assertExpression( |
| 60 | + 3, |
| 61 | + of(instant).dayOfMonth(), |
| 62 | + "{'$dayOfMonth': {'$date': '2007-12-03T10:15:30.005Z'}}"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void dayOfWeekTest() { |
| 67 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/ |
| 68 | + assertExpression( |
| 69 | + 2, |
| 70 | + of(instant).dayOfWeek(), |
| 71 | + "{'$dayOfWeek': {'$date': '2007-12-03T10:15:30.005Z'}}"); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void dayOfYearTest() { |
| 76 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfYear/ |
| 77 | + assertExpression( |
| 78 | + 337, |
| 79 | + of(instant).dayOfYear(), |
| 80 | + "{'$dayOfYear': {'$date': '2007-12-03T10:15:30.005Z'}}"); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void hourTest() { |
| 85 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/hour/ |
| 86 | + assertExpression( |
| 87 | + 10, |
| 88 | + of(instant).hour(), |
| 89 | + "{'$hour': {'$date': '2007-12-03T10:15:30.005Z'}}"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void minuteTest() { |
| 94 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/minute/ |
| 95 | + assertExpression( |
| 96 | + 15, |
| 97 | + of(instant).minute(), |
| 98 | + "{'$minute': {'$date': '2007-12-03T10:15:30.005Z'}}"); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void secondTest() { |
| 103 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/second/ |
| 104 | + assertExpression( |
| 105 | + 30, |
| 106 | + of(instant).second(), |
| 107 | + "{'$second': {'$date': '2007-12-03T10:15:30.005Z'}}"); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void weekTest() { |
| 112 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/week/ |
| 113 | + assertExpression( |
| 114 | + 48, |
| 115 | + of(instant).week(), |
| 116 | + "{'$week': {'$date': '2007-12-03T10:15:30.005Z'}}"); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void millisecondTest() { |
| 121 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/millisecond/ |
| 122 | + assertExpression( |
| 123 | + 5, |
| 124 | + of(instant).millisecond(), |
| 125 | + "{'$millisecond': {'$date': '2007-12-03T10:15:30.005Z'}}"); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void dateToStringTest() { |
| 130 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateToString/ |
| 131 | + assertExpression( |
| 132 | + "2007-12-03T10:15:30.005Z", |
| 133 | + of(instant).dateToString()); |
| 134 | + // with parameters |
| 135 | + assertExpression( |
| 136 | + "2007-12-03T05:15:30.005Z", |
| 137 | + of(instant).dateToString(of("%Y-%m-%dT%H:%M:%S.%LZ"), of("America/New_York")), |
| 138 | + "{'$dateToString': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, " |
| 139 | + + "'format': '%Y-%m-%dT%H:%M:%S.%LZ', " |
| 140 | + + "'timezone': 'America/New_York'}}"); |
| 141 | + assertExpression( |
| 142 | + "2007-12-03T14:45:30.005Z", |
| 143 | + of(instant).dateToString(of("%Y-%m-%dT%H:%M:%S.%LZ"), of("+04:30")), |
| 144 | + "{'$dateToString': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, " |
| 145 | + + "'format': '%Y-%m-%dT%H:%M:%S.%LZ', " |
| 146 | + + "'timezone': '+04:30'}}"); |
| 147 | + } |
| 148 | +} |
0 commit comments