1#!/usr/bin/env python3
 2
 3"""Test the timestamp-based cursor OR merge function."""
 4
 5from datetime import datetime
 6
 7from chart.db.ts import merge_ts_or
 8
 9cursors = [
10iter(
11    [
12        (datetime(2012, 6, 7, 7, 23, 10), 1, 2, 100),
13        (datetime(2012, 6, 7, 7, 23, 20), 1, 3, 100),
14        (datetime(2012, 6, 7, 7, 23, 30), 1, 4, 100),
15        (datetime(2012, 6, 7, 7, 23, 40), 1, 5, 100),
16        (datetime(2012, 6, 7, 7, 23, 50), 1, 6, 100),
17        (datetime(2012, 6, 7, 7, 24, 6), 1, 7, 100),
18        (datetime(2012, 6, 7, 7, 24, 8), 1, 8, 100),
19        (datetime(2012, 6, 7, 7, 24, 10), 1, 9, 100),
20        (datetime(2012, 6, 7, 7, 24, 12), 1, 10, 100),
21        (datetime(2012, 6, 7, 7, 24, 14), 1, 11, 100),
22        (datetime(2012, 6, 7, 7, 24, 16), 1, 12, 100)
23    ]),
24iter(
25    [
26        (datetime(2012, 6, 7, 7, 23, 11), 10, 22),
27        (datetime(2012, 6, 7, 7, 23, 21), 10, 32),
28        (datetime(2012, 6, 7, 7, 23, 30), 10, 42),
29        (datetime(2012, 6, 7, 7, 23, 40), 10, 52),
30        (datetime(2012, 6, 7, 7, 24, 1), 10, 62),
31        (datetime(2012, 6, 7, 7, 24, 6), 10, 72),
32        (datetime(2012, 6, 7, 7, 24, 9), 10, 82)
33    ])
34]
35
36result = [
37    [datetime(2012, 6, 7, 7, 23, 10), 1, 2, 100, None, None],
38    [datetime(2012, 6, 7, 7, 23, 11), None, None, None, 10, 22],
39    [datetime(2012, 6, 7, 7, 23, 20), 1, 3, 100, None, None],
40    [datetime(2012, 6, 7, 7, 23, 21), None, None, None, 10, 32],
41    [datetime(2012, 6, 7, 7, 23, 30), 1, 4, 100, 10, 42],
42    [datetime(2012, 6, 7, 7, 23, 40), 1, 5, 100, 10, 52],
43    [datetime(2012, 6, 7, 7, 23, 50), 1, 6, 100, None, None],
44    [datetime(2012, 6, 7, 7, 24, 1), None, None, None, 10, 62],
45    [datetime(2012, 6, 7, 7, 24, 6), 1, 7, 100, 10, 72],
46    [datetime(2012, 6, 7, 7, 24, 8), 1, 8, 100, None, None],
47    [datetime(2012, 6, 7, 7, 24, 9), None, None, None, 10, 82],
48    [datetime(2012, 6, 7, 7, 24, 10), 1, 9, 100, None, None],
49    [datetime(2012, 6, 7, 7, 24, 12), 1, 10, 100, None, None],
50    [datetime(2012, 6, 7, 7, 24, 14), 1, 11, 100, None, None],
51    [datetime(2012, 6, 7, 7, 24, 16), 1, 12, 100, None, None]
52]
53
54
55def test_merge():
56    """Test that running the OR merge against `cursors` produces `result`."""
57    i = 0
58    for row in merge_ts_or(cursors):
59        assert row == result[i]
60        i += 1
61
62if __name__ == '__main__':
63    test_merge()