def testEnablingChanges(self): cs = ConditionSet(name='condition_set', data=self.table, setop='AND') cc = CategoricalCondition(data=self.table, attr='fake_cat') rc = RangeCondition(data=self.table, attr='Information') rc.set_range(0.5, relative=True) cs.add_condition(cc) cc.add_category('C1') self.assertItemsEqual(cs.reference, self.fake_sets['C1']) cc.disable() print cs.query, cs.reference self.assertItemsEqual(cs.reference, []) cs.add_condition(rc) self.assertItemsEqual(cs.reference, ['CA', 'NY']) rc.enable(False) cc.enable() self.assertItemsEqual(cs.reference, self.fake_sets['C1']) cc.toggle_category(['C1', 'C3']) rc.enable() self.assertItemsEqual(cs.reference, ['NY']) rc.set_range(0, 1, relative=True) self.assertItemsEqual(cs.reference, self.fake_sets['C3'])
def testRemoveCondition(self): cs = ConditionSet(name='condition_set', data=self.table, setop='AND') cc = CategoricalCondition(data=self.table, attr='fake_cat') cs.add_condition(cc) cc.add_category('C1') self.assertItemsEqual(cs.reference, self.fake_sets['C1']) cs.remove_condition(cc) self.assertEqual(cs.reference, [])
def testAddCondition(self): cs = ConditionSet(name='condition_set', data=self.table, setop='AND') cc = CategoricalCondition(data=self.table, attr='fake_cat') cs.add_condition(cc) cc.add_category('C1') self.assertItemsEqual(cs.reference, self.fake_sets['C1']) cc.add_category(['C1', 'C3']) c1_and_c3 = self.fake_sets['C1'][:] c1_and_c3 += self.fake_sets['C3'] self.assertItemsEqual(cs.reference, c1_and_c3)
def testGrammar(self): cs = ConditionSet(name='condition_set', data=self.table, setop='AND') cc = CategoricalCondition(data=self.table, attr='fake_cat', name='catc') rc = RangeCondition(data=self.table, attr='Information', name='rangec') ac = AttributeCondition(data=self.table, name='attrc') cs.add_condition(cc) cs.add_condition(rc) cs.add_condition(ac) print cs.grammar self.maxDiff = None self.assertDictEqual(cs.grammar, {'setop': 'AND', 'conditions': ['rangec','catc','attrc'], 'data': 'census', 'name': 'condition_set'})
def testAddSeveralConditions(self): cs = ConditionSet(name='condition_set', data=self.table, setop='AND') cc = CategoricalCondition(data=self.table, attr='fake_cat') rc = RangeCondition(data=self.table, attr='Information') rc.set_range(0.5, relative=True) cs.add_condition(cc) cc.add_category('C1') self.assertItemsEqual(cs.reference, self.fake_sets['C1']) cs.add_condition(rc) self.assertItemsEqual(cs.reference, ['CA']) cc.toggle_category(['C1', 'C3']) self.assertItemsEqual(cs.reference, ['NY']) rc.set_range(0, 1, relative=True) self.assertItemsEqual(cs.reference, self.fake_sets['C3'])
def createConditions(self): self.categorical_condition = CategoricalCondition(data=self.table, attr='fake_cat') self.range_condition = RangeCondition(data=self.table, attr='Information', range=dict(min=250000, max=500000)) self.query_condition = QueryCondition(data=self.table, query={'State':'NY'}) self.attribute_condition = AttributeCondition(data=self.table, attributes=['Information']) self.consition_set = ConditionSet(name='condition_set', data=self.table, setop='AND') self.consition_set.add_condition(self.categorical_condition) self.consition_set.add_condition(self.range_condition) self.consition_set.add_condition(self.query_condition) self.consition_set.add_condition(self.attribute_condition) self.root.add_condition(self.categorical_condition) self.root.add_condition(self.range_condition) self.root.add_condition(self.query_condition) self.root.add_condition(self.attribute_condition)
def testCreation(self): cs = ConditionSet(name='condition_set', data=self.table, setop='AND') self.assertEqual(cs.is_empty(), True) self.assertEqual(cs.reference, []) self.assertEqual(cs.query, {'State': {'$in': []}}) self.assertEqual(cs.projection, {})
class RootTest(unittest.TestCase): def setUp(self): self.root = Root("root") self.root2 = Root("root2") self.createTable() self.createConditions() self.createDynamics() self.createSharedObject() def tearDown(self): names.clear() def createTable(self): self.df = pd.read_csv(RSC_DIR + '/census.csv') with open(RSC_DIR + '/schema_census') as f: schema = json.loads(f.read()) self.schema = OrderedDict(attributes=schema['attributes'], index=schema['index']) self.table = Table('census', self.schema).data(self.df) self.table.add_column('fake_cat', 'CATEGORICAL') items = self.table.index_items() fake_cat = ['C1','C2','C3','C4'] for i, item in enumerate(items): self.table.update({'State':item}, {'$set': {'fake_cat': fake_cat[i % 4]}}) self.root.add_dataset(self.table) def createConditions(self): self.categorical_condition = CategoricalCondition(data=self.table, attr='fake_cat') self.range_condition = RangeCondition(data=self.table, attr='Information', range=dict(min=250000, max=500000)) self.query_condition = QueryCondition(data=self.table, query={'State':'NY'}) self.attribute_condition = AttributeCondition(data=self.table, attributes=['Information']) self.consition_set = ConditionSet(name='condition_set', data=self.table, setop='AND') self.consition_set.add_condition(self.categorical_condition) self.consition_set.add_condition(self.range_condition) self.consition_set.add_condition(self.query_condition) self.consition_set.add_condition(self.attribute_condition) self.root.add_condition(self.categorical_condition) self.root.add_condition(self.range_condition) self.root.add_condition(self.query_condition) self.root.add_condition(self.attribute_condition) def createDynamics(self): self.filter1 = DynFilter("filer1", self.table) self.filter1.add_condition(self.range_condition) self.filter2 = DynFilter("filer2", self.table) self.filter2.add_condition(self.query_condition) self.filter2.add_condition(self.attribute_condition) self.selection = DynSelect("selection", self.table) self.selection.add_condition(self.categorical_condition) self.root.add_dynamic(self.filter1) self.root.add_dynamic(self.filter2) self.root.add_dynamic(self.selection) def createSharedObject(self): self.shared_object = SharedObject("shared_object", {'a':42, 'b':[1,2,3,4]}) self.root.add_dataset(self.shared_object) def testCreation(self): from pprint import pprint grammar = self.root.grammar pprint(grammar) names.clear() objects = Root.build(grammar) self.assertIn(self.table.name, objects) self.assertIn(self.attribute_condition.name, objects) self.assertIn(self.categorical_condition.name, objects) self.assertIn(self.range_condition.name, objects) self.assertIn(self.query_condition.name, objects) self.assertIn(self.filter1.name, objects) self.assertIn(self.filter2.name, objects) self.assertIn(self.selection.name, objects) self.assertIn(self.shared_object.name, objects) self.assertIsInstance(self.table, Table) self.assertIsInstance(self.attribute_condition, AttributeCondition) self.assertIsInstance(self.categorical_condition, CategoricalCondition) self.assertIsInstance(self.range_condition, RangeCondition) self.assertIsInstance(self.query_condition, QueryCondition) self.assertIsInstance(self.filter1, DynFilter) self.assertIsInstance(self.filter2, DynFilter) self.assertIsInstance(self.selection, DynSelect) self.assertIsInstance(self.shared_object, SharedObject)