Skip to content

Commit

Permalink
using __init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
tschm committed Jul 25, 2023
1 parent 43058c9 commit 08816b2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
2 changes: 2 additions & 0 deletions cvx/bson/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .file import read_bson, write_bson

1 change: 1 addition & 0 deletions cvx/json/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .file import read_json, write_json
20 changes: 7 additions & 13 deletions tests/test_bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,18 @@
import numpy as np
import pytest

from cvx.bson.file import read_bson, write_bson
from cvx.bson import read_bson, write_bson


@pytest.fixture()
def random_matrix():
np.random.seed(2)
return np.random.rand(5, 3)


def test_write(random_matrix, tmp_path):
dic = {"B": random_matrix}
write_bson(tmp_path / "maffay.bson", dic)
assert (tmp_path / "maffay.bson").exists()
@pytest.mark.parametrize("shape", [(50, 50), (1000, 50), (50, 1000), (1000,1000)])
def test_write(tmp_path, shape):
data = {"a": np.random.rand(*shape)}
write_bson(tmp_path / "maffay.bson", data)

x = dict(read_bson(tmp_path / "maffay.bson"))

assert set(x.keys()) == {"B"}
np.allclose(x["B"], random_matrix)
assert set(x.keys()) == {"a"}
np.allclose(x["a"], data["a"])


def test_wrong_type(tmp_path):
Expand Down
8 changes: 5 additions & 3 deletions tests/test_json.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# -*- coding: utf-8 -*-
import numpy as np
import pytest

from cvx.json.file import read_json, write_json
from cvx.json import read_json, write_json


def test_read_and_write_json(tmp_path):
data = {"a": np.array([2.0, 3.0]), "b": 3.0, "c": "test"}
@pytest.mark.parametrize("shape", [(50, 50), (1000, 50), (50, 1000), (1000,1000)])
def test_read_and_write_json(tmp_path, shape):
data = {"a": np.random.rand(*shape), "b": 3.0, "c": "test"}
write_json(tmp_path / "test.json", data)

recovered_data = dict(read_json(tmp_path / "test.json"))
Expand Down

0 comments on commit 08816b2

Please sign in to comment.