from __future__ import absolute_import, division, print_function

import pytest
pytest.importorskip('dynd')

from odo.backends.dynd import convert, append, create, discover
from odo import into, append, convert, resource, discover
from dynd import nd
import numpy as np
import datashape

def test_create():
    ds = datashape.dshape('5 * int32')
    d = create(nd.array, dshape=ds)
    assert discover(d) == ds

def test_convert():
    ds = datashape.dshape('3 * int32')
    x = convert(nd.array, [1, 2, 3], dshape=ds)
    assert isinstance(x, nd.array)
    assert convert(list, x) == [1, 2, 3]

    for cls in [list, tuple, np.ndarray]:
        y = convert(cls, x)
        assert isinstance(y, cls)
        assert isinstance(convert(nd.array, y), nd.array)

    x = nd.array([('a', 1)], type='1 * {a: string, b: int32}')
    assert convert(list, x) == [{'a': 'a', 'b': 1}]

from __future__ import absolute_import, division, print_function

import pytest
import sys
pytest.importorskip('dynd')

from odo import create, convert, discover
from dynd import nd
import numpy as np
import datashape


@pytest.fixture
def x():
    ds = datashape.dshape('3 * int32')
    return convert(nd.array, [1, 2, 3], dshape=ds)


def test_create():
    ds = datashape.dshape('5 * int32')
    d = create(nd.array, dshape=ds)
    assert discover(d) == ds


def test_simple_convert(x):
    assert isinstance(x, nd.array)
    assert convert(list, x) == [1, 2, 3]


@pytest.mark.parametrize(['typ', 'expected'],
                         [(list, [1, 2, 3]),
                          (tuple, (1, 2, 3)),
                          (np.ndarray, np.array([1, 2, 3]))])
def test_convert_different_types(x, typ, expected):
    y = convert(typ, x)
    assert isinstance(y, typ)
    assert isinstance(convert(nd.array, y), nd.array)
    assert all(lhs == rhs for lhs, rhs in zip(y, expected))


@pytest.mark.xfail(
    sys.version_info[:2] == (3, 3),
    raises=AssertionError,
    reason='Python 3.3 is not well supported'
)
def test_convert_struct():
    x = nd.array([('a', 1)], type='1 * {a: string, b: int32}')
    assert convert(list, x) == [{'a': 'a', 'b': 1}]

Refactor dynd tests