def test_indexes(self): fields = FieldList("a", "b", "c", "d") indexes = fields.indexes(["a", "c", "d"]) self.assertEqual((0,2,3), indexes) indexes = fields.indexes( fields.fields() ) self.assertEqual((0,1,2,3), indexes)
def test_deletion(self): fields = FieldList("a", "b", "c", "d") del fields[0] self.assertEqual(["b", "c", "d"], fields.names()) del fields[2] self.assertEqual(["b", "c"], fields.names()) self.assertRaises(NoSuchFieldError, fields.field, "d") self.assertEqual(2, len(fields))
def test_list_creation(self): fields = FieldList("foo", "bar") for field in fields: self.assertEqual(type(field), Field) self.assertIsInstance(field.name, str) self.assertEqual("foo", fields[0].name, 'message') self.assertEqual(2, len(fields))
def setUp(self): self.context = OperationContext() self.context.add_operations_from(bubbles.backends.sql.ops) self.sql_data_store = SQLDataStore('sqlite:///') self.table = self.sql_data_store.create('test', FieldList(('a', 'integer'), ('b', 'integer'), ('c', 'integer')), replace=True) self.table.append_from_iterable([(1, 2, 3), (1, 2, 4), (1, 3, 5)])
def test_mask(self): fields = FieldList("a", "b", "c", "d") mask = fields.mask(["b", "d"]) self.assertEqual([False, True, False, True], mask)
def test_contains(self): fields = FieldList("a", "b", "c", "d") field = Field("a") self.assertEqual(True, "a" in fields) self.assertEqual(True, field in fields._fields)
def test_list_add(self): fields = FieldList("foo", "bar") fields.append("baz") self.assertEqual(3, len(fields))
def setUp(self): self.fields = FieldList("a", "b", "c", "d")
from bubbles import Pipeline, FieldList, data_object, open_store # Sample order data with fields: fields = FieldList(["id", "integer"], ["customer_id", "integer"], ["year", "integer"], ["amount", "integer"]) data = [[1, 1, 2009, 10], [2, 1, 2010, 20], [3, 1, 2011, 20], [4, 1, 2012, 50], [5, 2, 2010, 50], [6, 2, 2012, 40], [7, 3, 2011, 100], [8, 3, 2012, 150], [9, 3, 2013, 120]] # Stores for SQL alternative, if enabled (see below) stores = {"default": open_store("sql", "sqlite:///")} # # Create the pipeline # p = Pipeline(stores=stores) p.source_object("iterable", iterable=data, fields=fields) # Uncomment this to get SQL operations instead of python iterator p.create("default", "data") # Find last purchase date last_purchase = p.fork() last_purchase.aggregate(["customer_id"], [["year", "max"]], include_count=False) last_purchase.rename_fields({"year_max": "last_purchase_year"}) p.join_details(last_purchase, "customer_id", "customer_id") p.pretty_print()
def test_aggregated_fields(self): fields = FieldList("a", "b") agg_list = prepare_aggregation_list(['a', ('b', 'avg')]) agg_fields = fields.aggregated_fields(agg_list) self.assertListEqual( ['a_sum', 'b_avg', 'record_count'], agg_fields.names())
def test_contains(self): fields = FieldList("a", "b", "c", "d") field = Field("a") self.assertIn("a", fields) self.assertIn(field, fields._fields)