def test_his_read_with_boolean(mock_s3, mock_get_url): """ Args: mock_s3: mock_get_url: """ mock_s3.return_value = _get_mock_s3() his = Grid(version=VER_3_0, columns=["ts", "val"]) his.extend([ { "ts": datetime(2020, 1, 1, tzinfo=pytz.utc), "val": MARKER }, { "ts": datetime(2020, 1, 1, tzinfo=pytz.utc), "val": False }, { "ts": datetime(2020, 1, 1, tzinfo=pytz.utc), "val": 1 }, { "ts": datetime(2020, 1, 1, tzinfo=pytz.utc), "val": 1.0 }, { "ts": datetime(2020, 1, 1, tzinfo=pytz.utc), "val": "" }, ]) mock_s3.return_value.history = his mock_get_url.return_value = "s3://bucket/grid.zinc" envs = {'HAYSTACK_PROVIDER': "shaystack.providers.url"} with get_provider("shaystack.providers.url", envs) as _: client = Client(schema) executed = client.execute(''' { haystack { histories(ids:"@id1") { ts val bool } } } ''') assert executed == \ {'data': {'haystack': {'histories': [[{'ts': '2020-01-01T00:00:00+00:00 UTC', 'val': 'm:', 'bool': True}, {'ts': '2020-01-01T00:00:00+00:00 UTC', 'val': False, 'bool': False}, {'ts': '2020-01-01T00:00:00+00:00 UTC', 'val': 'n:1.000000', 'bool': True}, {'ts': '2020-01-01T00:00:00+00:00 UTC', 'val': 'n:1.000000', 'bool': True}, {'ts': '2020-01-01T00:00:00+00:00 UTC', 'val': 's:', 'bool': False}]]}}}
def test_entities_with_id(mock_s3, mock_get_url): """ Args: mock_s3: mock_get_url: """ mock_s3.return_value = _get_mock_s3() mock_get_url.return_value = "s3://bucket/grid.zinc" envs = {'HAYSTACK_PROVIDER': "shaystack.providers.url"} with get_provider("shaystack.providers.url", envs) as _: client = Client(schema) executed = client.execute(''' { haystack { entities(ids:["@id1","@id2"]) } } ''') assert executed == \ { 'data': { 'haystack': { 'entities': [ {'id': 'r:id1', 'col': 'n:1.000000', 'hisURI': 's:his0.zinc'}, {'id': 'r:id2', 'col': 'n:2.000000', 'hisURI': 's:his1.zinc'} ] } } }
def test_versions(mock_s3, mock_get_url): """ Args: mock_s3: mock_get_url: """ mock_s3.return_value = _get_mock_s3() mock_get_url.return_value = "s3://bucket/grid.zinc" envs = {'HAYSTACK_PROVIDER': "shaystack.providers.url"} with get_provider("shaystack.providers.url", envs) as _: client = Client(schema) executed = client.execute(''' { haystack { versions } } ''') assert executed == \ {'data': {'haystack': {'versions': ['2020-10-01T00:00:03+00:00 UTC', '2020-10-01T00:00:02+00:00 UTC', '2020-10-01T00:00:01+00:00 UTC']}}}
def test_version(mock, mock_get_url): """ Args: mock: mock_get_url: """ mock.return_value = _get_mock_s3() mock_get_url.return_value = "s3://bucket/grid.zinc" with get_provider("shaystack.providers.url", {}) as provider: versions = provider.versions() assert len(versions) == 3
def test_read_version_without_filter(mock_s3, mock_get_url): """ Args: mock_s3: mock_get_url: """ mock_s3.return_value = _get_mock_s3() mock_get_url.return_value = "s3://bucket/grid.zinc" with get_provider("shaystack.providers.url", {}) as provider: version_2 = datetime(2020, 10, 1, 0, 0, 2, 0, tzinfo=pytz.UTC) result = provider.read(0, None, None, None, date_version=version_2) assert result.metadata["v"] == "2"
def test_read_last_without_filter(mock_s3, mock_get_url): """ Args: mock_s3: mock_get_url: """ mock_s3.return_value = _get_mock_s3() mock_get_url.return_value = "s3://bucket/grid.zinc" with cast(URLProvider, get_provider("shaystack.providers.url", {})) as provider: provider.cache_clear() result = provider.read(0, None, None, None, None) assert result.metadata["v"] == "3"
def test_values_for_tag(mock_s3, mock_get_url): """ Args: mock_s3: mock_get_url: """ mock_s3.return_value = _get_mock_s3() mock_get_url.return_value = "s3://bucket/grid.zinc" with get_provider("shaystack.providers.url", {}) as provider: result = provider.values_for_tag("col") assert result == [1.0, 2.0] result = provider.values_for_tag("id") assert result == [Ref("id1"), Ref("id2")]
def test_read_version_with_ids(mock_s3, mock_get_url): """ Args: mock_s3: mock_get_url: """ mock_s3.return_value = _get_mock_s3() mock_get_url.return_value = "s3://bucket/grid.zinc" with get_provider("shaystack.providers.url", {}) as provider: version_2 = datetime(2020, 10, 1, 0, 0, 2, 0, tzinfo=pytz.UTC) result = provider.read(0, None, [Ref("id1")], None, version_2) assert result.metadata["v"] == "2" assert len(result) == 1 assert result[0]['id'] == Ref("id1")
def test_read_version_with_filter2(mock_s3, mock_get_url): """ Args: mock_s3: mock_get_url: """ mock_s3.return_value = _get_mock_s3() mock_get_url.return_value = "s3://bucket/grid.zinc" with get_provider("shaystack.providers.url", {}) as provider: version_2 = datetime(2020, 10, 1, 0, 0, 2, 0, tzinfo=pytz.UTC) result = provider.read(0, "id,other", None, "id==@id1", version_2) assert result.column == { "id": MetadataObject(), "other": MetadataObject() }
def test_his_read_with_datetime(mock_s3, mock_get_url): """ Args: mock_s3: mock_get_url: """ mock_s3.return_value = _get_mock_s3() his = Grid(version=VER_3_0, columns=["ts", "val"]) his.append({ "ts": datetime(2020, 1, 1, tzinfo=pytz.utc), "val": datetime(2020, 1, 1, tzinfo=pytz.utc) }) mock_s3.return_value.history = his mock_s3.return_value.his_count = 300 mock_get_url.return_value = "s3://bucket/grid.zinc" envs = {'HAYSTACK_PROVIDER': "shaystack.providers.url"} with cast(URLProvider, get_provider("shaystack.providers.url", envs)) as Provider: Provider.cache_clear() client = Client(schema) executed = client.execute(''' { haystack { histories(ids:"@id1") { ts val date time #datetime } } } ''') assert executed == \ {'data': {'haystack': {'histories': [[{'ts': '2020-01-01T00:00:00+00:00 UTC', 'val': 't:2020-01-01T00:00:00+00:00 UTC', 'date': '2020-01-01T00:00:00+00:00', 'time': '00:00:00'}]]}}}
def test_entities_with_select(mock_s3, mock_get_url): """ Args: mock_s3: mock_get_url: """ mock_s3.return_value = _get_mock_s3() mock_get_url.return_value = "s3://bucket/grid.zinc" envs = {'HAYSTACK_PROVIDER': "shaystack.providers.url"} with get_provider("shaystack.providers.url", envs) as _: client = Client(schema) executed = client.execute(''' { haystack { entities(filter:"id==@id1" select:"id") } } ''') assert executed == \ {'data': {'haystack': {'entities': [{'id': 'r:id1'}]}}}
def test_tag_values(mock_s3, mock_get_url): """ Args: mock_s3: mock_get_url: """ mock_s3.return_value = _get_mock_s3() mock_get_url.return_value = "s3://bucket/grid.zinc" envs = {'HAYSTACK_PROVIDER': "shaystack.providers.url"} with get_provider("shaystack.providers.url", envs) as _: client = Client(schema) executed = client.execute(''' { haystack { tagValues(tag:"id") } } ''') assert executed == \ {'data': {'haystack': {'tagValues': ['@id1', '@id2']}}}
def test_lru_version(mock): """ Args: mock: """ mock.return_value = _get_mock_s3() with cast(URLProvider, get_provider("shaystack.providers.url", {})) as provider: provider.cache_clear() version_3 = datetime(2020, 10, 1, 0, 0, 3, 0, tzinfo=pytz.UTC) version_2 = datetime(2020, 10, 1, 0, 0, 2, 0, tzinfo=pytz.UTC) version_1 = datetime(2020, 10, 1, 0, 0, 1, 0, tzinfo=pytz.UTC) version_0 = datetime(2020, 10, 1, 0, 0, 0, 0, tzinfo=pytz.UTC) last = None url = "s3://bucket/grid.zinc" assert provider._download_grid(url, last).metadata["v"] == "3" assert provider._download_grid(url, version_3).metadata["v"] == "3" assert provider._download_grid(url, version_2).metadata["v"] == "2" assert provider._download_grid(url, version_1).metadata["v"] == "1" result = provider._download_grid(url, version_0) assert len(result) == 0