def test_to_son(self): dt = datetime(2010, 11, 12, 13, 14, 15) field = JsonField(db_field="test") expect(field.to_son(1)).to_equal(serialize(1)) expect(field.to_son([1, 2, 3])).to_equal(serialize([1, 2, 3])) expect(field.to_son({"a": 1, "b": 3})).to_equal(serialize({"a": 1, "b": 3})) expect(field.to_son(dt)).to_equal(serialize(dt))
def test_validate_enforces_jsonifiable_objects(self): field = JsonField(db_field="test") expect(field.validate(1)).to_be_true() expect(field.validate([1, 2, 3])).to_be_true() expect(field.validate({"a": 1, "b": 3})).to_be_true() expect(field.validate(None)).to_be_true()
def test_from_son(self): dt = datetime.utcnow() field = JsonField(db_field="test") expect(field.from_son("1")).to_equal(1) expect(field.from_son("[1, 2, 3]")).to_be_like([1, 2, 3]) expect(field.from_son('{"a": 1, "b": 3}')).to_be_like({"a": 1, "b": 3}) expect(field.from_son(None)).to_be_null() try: import ujson # NOQA dt_value = calendar.timegm(dt.utctimetuple()) expect(field.from_son(serialize(dt))).to_be_like(dt_value) except ImportError: value = field.from_son(serialize(dt)).isoformat() expect(value[:20]).to_equal(dt.isoformat()[:20])
def test_to_son(self): dt = datetime(2010, 11, 12, 13, 14, 15) field = JsonField(db_field="test") expect(field.to_son(1)).to_equal(serialize(1)) expect(field.to_son([1, 2, 3])).to_equal(serialize([1, 2, 3])) expect(field.to_son({ "a": 1, "b": 3 })).to_equal(serialize({ "a": 1, "b": 3 })) expect(field.to_son(dt)).to_equal(serialize(dt)) expect(field.to_son(None)).to_be_null()
def test_from_son(self): dt = datetime.utcnow() field = JsonField(db_field="test") expect(field.from_son("1")).to_equal(1) expect(field.from_son("[1, 2, 3]")).to_be_like([1, 2, 3]) expect(field.from_son('{"a": 1, "b": 3}')).to_be_like({"a": 1, "b": 3}) try: import ujson # NOQA dt_value = calendar.timegm(dt.utctimetuple()) expect(field.from_son(serialize(dt))).to_be_like(dt_value) except ImportError: value = field.from_son(serialize(dt)).isoformat() expect(value[:20]).to_equal(dt.isoformat()[:20])
class JSONFieldDocument(Document): field = JsonField()
def test_validate_enforces_jsonifiable_objects(self): field = JsonField(db_field="test") expect(field.validate(1)).to_be_true() expect(field.validate([1, 2, 3])).to_be_true() expect(field.validate({"a": 1, "b": 3})).to_be_true()
def test_create_json_field(self): field = JsonField(db_field="test") expect(field.db_field).to_equal("test")