def test_explicit_exclusion_on_list_of_models(self, ctx): export_data = ExportData() lst = export_data(Order.query.all(), exclude=["customer_id"]) assert type(lst) == list assert len(lst) == 2 assert all(["customer_id" not in model for model in lst])
def test_exclude_property(self, ctx): export_data = ExportData(exclude=["id"]) product = Product.query.get(1) data = export_data(product) assert "id" not in data
def test_exclude_property_and_include_parameter(self, ctx): export_data = ExportData(exclude=["id"]) product = Product.query.get(1) data = export_data(product, include=["id"]) assert "id" not in data
def test_list_of_models_without_export_data(self, ctx): export_data = ExportData() lst = export_data(Order.query.all()) assert type(lst) == list assert len(lst) == 2 assert {"id": 1, "customer_id": 1} in lst
def test_explicit_inclusion_on_list_of_model_with_export_data_method( self, ctx): export_data = ExportData() lst = Cartoon.query.all() output = export_data(Cartoon.query.all(), include=["id"]) assert len(output) == len(lst) assert all(["name" not in model for model in output])
def test_explicit_exclusion_on_list_of_models_with_export_data_method( self, ctx): export_data = ExportData() lst = OrderLine.query.all() output = export_data(lst, exclude=["product_id"]) assert len(output) == len(lst) assert all(["product_id" not in model for model in output])
def test_exclude_property_on_model_with_export_data_method(self, ctx): assert hasattr(Cartoon, "export_data") assert hasattr(Cartoon, "id") export_data = ExportData(exclude=["id"]) product = Cartoon.query.first() data = export_data(product) assert "id" not in data
def test_explicit_column_exclusion(self, ctx): export_data = ExportData() product = Product.query.get(1) data = export_data(product, exclude=["id", "name"]) assert len(data) == 5 assert "id" not in data assert "name" not in data
def test_list_of_models_with_export_method(self, ctx): export_data = ExportData() lst = export_data(OrderLine.query.all()) data = lst[0] assert len(data) == 3 assert "product_id" in data assert "unit_price" in data assert "quantity" in data
def test_explicit_column_inclusion(self, ctx): export_data = ExportData() product = Product.query.get(1) data = export_data(product, include=["id", "name"]) assert len(data) == 2 assert data["id"] == 1 assert data["name"] == "Explosive Tennis Balls"
def test_explicit_loaded_relationship_exclusion(self, ctx): export_data = ExportData() order = (Order.query.options(joinedload(Order.lines)).options( joinedload(Order.customer)).get(1)) data = export_data(order, exclude=["lines"]) assert "lines" not in data assert "customer" in data
def test_include_and_exclude_argument(self, ctx): export_data = ExportData() product = Product.query.get(1) data = export_data(product, include=["id", "name", "price"], exclude=["id"]) assert len(data) == 2 assert "id" not in data
def test_exclude_property_on_list_of_models(self, ctx): export_data = ExportData(exclude=["customer_id"]) assert not hasattr(Order, "export_data") lst = Order.query.all() assert len(lst) > 0 output = export_data(lst) assert all(["customer_id" not in model for model in output])
def test_persisted_object(self, ctx): export_data = ExportData() product = Product.query.get(1) data = export_data(product) assert len(data) == 7 assert data["id"] == 1 assert data["name"] == "Explosive Tennis Balls" assert data["color"] == "white" assert data["price"] == 9
def test_non_persisted_untouched_object(self): export_data = ExportData() data = export_data(Product()) assert len(data) == 7 for prop in ["id", "name", "created_at", "updated_at"]: assert data[prop] is None assert data["color"] == "white" assert data["price"] == 0 assert data["enabled"] is False
def test_with_invalid_arguments(self): message = "Pass a valid SQLAlchemy mapped class instance" export_data = ExportData() class NonMappedModel(object): pass with pytest.raises(ValueError, match=message): export_data(NonMappedModel()) with pytest.raises(ValueError, match=message): export_data(1)
def test_loaded_one_to_one_relationship_with_export_data_method(self, ctx): export_data = ExportData() order_line = Order.query.options(joinedload(Order.customer)).get(1) data = export_data(order_line, include=["id"]) assert len(data) == 2 customer = data["customer"] assert len(customer) == 2 assert "id" in customer assert "firstname" in customer
def test_non_persisted_touched_object(self): export_data = ExportData() data = export_data(Product(id=9, name="TNT", price=799, color="red")) assert len(data) == 7 assert data["id"] == 9 assert data["name"] == "TNT" assert data["color"] == "red" assert data["price"] == 799 assert data["enabled"] is False assert data["created_at"] is None assert data["updated_at"] is None
def test_loaded_one_to_one_relationship(self, ctx): export_data = ExportData() order_line = OrderLine.query.options(joinedload( OrderLine.product)).get((1, 2)) data = export_data(order_line) assert len(data) == 5 assert data["order_id"] == 1 assert data["product_id"] == 2 product_data = data["product"] assert len(product_data) == 7 assert product_data["id"] == 2 assert product_data["name"] == "Binocular" assert product_data["color"] == "black" assert product_data["price"] == 99
def test_loaded_one_to_many_relationship(self, ctx): export_data = ExportData() order = Order.query.options(joinedload(Order.lines)).get(1) data = export_data(order) assert len(data) == 3 assert len(data["lines"]) == 3 assert { "product_id": 1, "quantity": 3, "unit_price": 14 } in data["lines"] assert { "product_id": 2, "quantity": 4, "unit_price": 142 } in data["lines"] assert { "product_id": 3, "quantity": 7, "unit_price": 73 } in data["lines"]