Skip to content

Commit b62bd55

Browse files
Elijah Deluziowesgarland
authored andcommitted
Add tests for 21st century, correct test's assumptions WRT dates in respective languages
1 parent 69813cb commit b62bd55

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

tests/js/js2py/datetime.simple.failing

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,32 @@
88
*/
99
'use strict'
1010

11-
const jsDate = new Date(1111, 2 , 3);
1211
const throughPython = python.eval('(lambda x: x)');
13-
const pyDate = throughPython(jsDate);
12+
var expectedJsTimestamp;
13+
var jsDate;
14+
var pyDate;
1415

15-
if (jsDate.getTime() !== pyDate.getTime())
16+
// Test 1: Date from timestamp of 0 (1970 - 01 - 01), timestamp = 0
17+
jsDate = new Date(Date.UTC(1970, 0, 1, 0, 0, 0));
18+
expectedJsTimestamp = jsDate.getTime();
19+
pyDate = throughPython(jsDate);
20+
21+
if (expectedJsTimestamp !== pyDate.getTime())
22+
{
23+
console.error('expected', expectedJsTimestamp, 'but got', pyDate.getTime());
24+
throw new Error('test failed');
25+
}
26+
27+
console.log('pass -', pyDate);
28+
29+
// Test 2: Date from 21st century (2222 - 02 - 03), timestamp = 7955193600000
30+
jsDate = new Date(Date.UTC(2222, 1, 3, 0, 0, 0));
31+
expectedJsTimestamp = jsDate.getTime();
32+
pyDate = throughPython(jsDate);
33+
34+
if (expectedJsTimestamp !== pyDate.getTime())
1635
{
17-
console.error('expected', jsDate.getTime(), 'but got', pyDate.getTime());
36+
console.error('expected', expectedJsTimestamp, 'but got', pyDate.getTime());
1837
throw new Error('test failed');
1938
}
2039

tests/js/py2js/datetime.simple.failing

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,36 @@
88
*/
99
'use strict'
1010

11-
python.exec(`import datetime`);
12-
const pyDate = python.eval(`datetime.datetime(1111, 2, 3)`);
13-
const jsDateExpected = new Date(1111, 2, 3);
1411
const throughJS = x => x;
15-
const jsDate = throughJS(pyDate);
12+
var expectedJsTimestamp;
13+
var jsDate;
14+
var pyDate;
15+
16+
// Test 1: Date from timestamp of 0 (1970 - 01 - 01), timestamp = 0
17+
expectedJsTimestamp = 0;
18+
python.exec(`from datetime import timezone`)
19+
python.exec(`import datetime`);
20+
pyDate = python.eval(`datetime.datetime.fromtimestamp(${expectedJsTimestamp}, tz=timezone.utc)`);
21+
jsDate = throughJS(pyDate);
22+
23+
if (jsDate.getTime() !== expectedJsTimestamp)
24+
{
25+
console.error('expected', expectedJsTimestamp, 'but got', jsDate.getTime());
26+
throw new Error('test failed');
27+
}
28+
29+
console.log('pass -', jsDate);
30+
31+
// Test 2: Date from 21st century (2222 - 02 - 03), timestamp = 7955193600
32+
expectedJsTimestamp = 7955193600;
33+
python.exec(`from datetime import timezone`)
34+
python.exec(`import datetime`);
35+
pyDate = python.eval(`datetime.datetime.fromtimestamp(${expectedJsTimestamp}, tz=timezone.utc)`);
36+
jsDate = throughJS(pyDate);
1637

17-
if (jsDate.getTime() !== jsDateExpected.getTime())
38+
if (jsDate.getTime() !== expectedJsTimestamp)
1839
{
19-
console.error('expected', jsDate.getTime(), 'but got', pyDate.getTime());
40+
console.error('expected', expectedJsTimestamp, 'but got',jsDate.getTime());
2041
throw new Error('test failed');
2142
}
2243

0 commit comments

Comments
 (0)