import pytest
from stix2patterns.pattern import ParseException

from stix2matcher.matcher import MatcherException, match

_observations = [
    {
        "type": "observed-data",
        "first_observed": "1983-04-21T15:31:06Z",
        "last_observed": "1983-04-21T15:31:06Z",
        "number_observed": 1,
        "objects": {
            "0": {
                "type": "event",
                "good_ts": u"2010-05-21T13:21:43Z",
                "good_ts_frac": u"2010-05-21T13:21:43.1234Z",
                "bad_ts": [
                    u"2010-05-21T13:21:43",
                    u"2010-05-21T13:21:43z",
                    u"2010-05-21t13:21:43Z",
                    u"2010/05/21T13:21:43Z",
                    u"2010-05-21T13:21:99Z",
                    u"2010-05-21T13:21Z"
                ]
            }
        }
    }
]


@pytest.mark.parametrize("pattern", [
    "[event:good_ts = t'2010-05-21T13:21:43Z']",
    "[event:good_ts != t'1974-11-05T05:31:11Z']",
    "[event:good_ts > t'1974-11-05T05:31:11Z']",
    "[event:good_ts < t'3012-08-17T17:43:55Z']",
    "[event:good_ts_frac = t'2010-05-21T13:21:43.1234Z']",
    "[event:good_ts IN (t'1953-11-26T14:25:33Z', t'2010-05-21T13:21:43Z', t'2000-06-17T17:25:51.44Z')]",
    "[event:good_ts NOT IN (t'1953-11-26T14:25:33Z', t'1985-07-25T20:27:52Z', t'2000-06-17T17:25:51.44Z')]"
])
def test_ts_match(pattern):
    assert match(pattern, _observations)


@pytest.mark.parametrize("pattern", [
    # Same as above with operators reversed.
    "[event:good_ts != t'2010-05-21T13:21:43Z']",
    "[event:good_ts = t'1974-11-05T05:31:11Z']",
    "[event:good_ts <= t'1974-11-05T05:31:11Z']",
    "[event:good_ts >= t'3012-08-17T17:43:55Z']",
    "[event:good_ts_frac != t'2010-05-21T13:21:43.1234Z']",
    "[event:good_ts NOT IN (t'1953-11-26T14:25:33Z', t'2010-05-21T13:21:43Z', t'2000-06-17T17:25:51.44Z')]",
    "[event:good_ts IN (t'1953-11-26T14:25:33Z', t'1985-07-25T20:27:52Z', t'2000-06-17T17:25:51.44Z')]"
])
def test_ts_nomatch(pattern):
    assert not match(pattern, _observations)


@pytest.mark.parametrize("pattern", [
    "[event:good_ts = t'2010-05-21T13:21:43']",
    "[event:good_ts = t'2010-05-21T13:21:43z']",
    "[event:good_ts = t'2010-05-21t13:21:43Z']",
    "[event:good_ts = t'2010/05/21T13:21:43Z']",
    "[event:good_ts = t'2010-05-21T13:21Z']",
    "[event:good_ts = t'2010-05-21T13:21:99Z']"
])
def test_ts_pattern_error_parse(pattern):
    with pytest.raises(ParseException):
        match(pattern, _observations)


@pytest.mark.parametrize("pattern", [
    # auto-generate simple tests for all the bad timestamps
    "[event:bad_ts[{}] = t'1996-07-11T09:17:10Z']".format(i)
    for i in range(len(_observations[0]["objects"]["0"]["bad_ts"]))
])
def test_ts_json_error(pattern):
    with pytest.raises(MatcherException):
        match(pattern, _observations)

@@ -484,24 +484,96 @@
 :43.1234Z%22,%0A
+                %22good_ts_frac_nano%22: u%222010-05-21T13:21:43.123456789Z%22,%0A
             
@@ -588,16 +588,16 @@
 _ts%22: %5B%0A
-
         
@@ -1191,32 +1191,101 @@
 21:43.1234Z'%5D%22,%0A
+    %22%5Bevent:good_ts_frac_nano = t'2010-05-21T13:21:43.123456789Z'%5D%22,%0A
     %22%5Bevent:good
@@ -1829,32 +1829,32 @@
 7T17:43:55Z'%5D%22,%0A
-
     %22%5Bevent:good
@@ -1889,32 +1889,102 @@
 21:43.1234Z'%5D%22,%0A
+    %22%5Bevent:good_ts_frac_nano != t'2010-05-21T13:21:43.123456789Z'%5D%22,%0A
     %22%5Bevent:good

add tests for nanoseconds