|
| 1 | +import 'package:firebase_data_connect/firebase_data_connect.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + group('Timestamp', () { |
| 6 | + test('constructor initializes with correct nanoseconds and seconds', () { |
| 7 | + final timestamp = Timestamp(500, 864000); // Example timestamp values |
| 8 | + expect(timestamp.nanoseconds, 500); |
| 9 | + expect(timestamp.seconds, 864000); |
| 10 | + }); |
| 11 | + |
| 12 | + test('fromJson throws exception for invalid date format', () { |
| 13 | + expect(() => Timestamp.fromJson('invalid-date'), throwsException); |
| 14 | + }); |
| 15 | + |
| 16 | + test('fromJson correctly parses date with nanoseconds and UTC (Z) format', |
| 17 | + () { |
| 18 | + final timestamp = Timestamp.fromJson('1970-01-11T00:00:00.123456789Z'); |
| 19 | + expect(timestamp.seconds, 864000); |
| 20 | + expect(timestamp.nanoseconds, 123456789); |
| 21 | + }); |
| 22 | + |
| 23 | + test('fromJson correctly parses date without nanoseconds', () { |
| 24 | + final timestamp = Timestamp.fromJson('1970-01-11T00:00:00Z'); |
| 25 | + expect(timestamp.seconds, 864000); |
| 26 | + expect(timestamp.nanoseconds, 0); |
| 27 | + }); |
| 28 | + |
| 29 | + test('fromJson correctly handles timezones with positive offset', () { |
| 30 | + final timestamp = Timestamp.fromJson('1970-01-11T00:00:00+02:00'); |
| 31 | + expect(timestamp.seconds, |
| 32 | + 864000 - (2 * 3600)); // Adjusts by the positive timezone offset |
| 33 | + }); |
| 34 | + |
| 35 | + test('fromJson correctly handles timezones with negative offset', () { |
| 36 | + final timestamp = Timestamp.fromJson('1970-01-11T00:00:00-05:00'); |
| 37 | + expect(timestamp.seconds, |
| 38 | + 864000 + (5 * 3600)); // Adjusts by the negative timezone offset |
| 39 | + }); |
| 40 | + |
| 41 | + test('toJson correctly serializes to ISO8601 string with nanoseconds', () { |
| 42 | + final timestamp = Timestamp(123456789, 864000); // Example timestamp |
| 43 | + final json = timestamp.toJson(); |
| 44 | + expect(json, '1970-01-11T00:00:00.123456789Z'); |
| 45 | + }); |
| 46 | + |
| 47 | + test('toJson correctly serializes to ISO8601 string without nanoseconds', |
| 48 | + () { |
| 49 | + final timestamp = Timestamp(0, 864000); // No nanoseconds |
| 50 | + final json = timestamp.toJson(); |
| 51 | + expect(json, '1970-01-11T00:00:00.000Z'); |
| 52 | + }); |
| 53 | + |
| 54 | + test('toDateTime correctly converts to DateTime object', () { |
| 55 | + final timestamp = Timestamp(0, 864000); // Example timestamp |
| 56 | + final dateTime = timestamp.toDateTime(); |
| 57 | + expect(dateTime, DateTime.utc(1970, 1, 11, 0, 0, 0, 0)); |
| 58 | + }); |
| 59 | + }); |
| 60 | +} |
0 commit comments