def test_recorddao_data_query_scalar_list_has_only(self): """Test that the RecordDAO is retrieving on a has_only list of scalars.""" just_5 = list( self.record_dao.data_query(val_data_list_1=has_only( DataRange(-1, 1), DataRange(7.5, 10)))) # 5 only self.assertEqual(len(just_5), 1) self.assertIn("spam5", just_5)
def test_find(self): store, kosh_db = self.connect() # Create many datasets ds = store.create(metadata={"key1": 1, "key2": "A"}) ds2 = store.create(metadata={"key2": "B", "key3": 3}) ds3 = store.create() store.create(metadata={"key2": "C", "key3": 4}) ds.associate("tests/baselines/node_extracts2", "something") ds2.associate("tests/baselines/node_extracts2", "something") ds3.associate("tests/baselines/node_extracts2", "something") s = list(store.find(key2=DataRange("A"))) self.assertEqual(len(s), 3) s = list( store.find(key2=DataRange("A"), file=os.path.abspath("tests/baselines/node_extracts2"))) self.assertEqual(len(s), 2) self.assertEqual(len(ds._associated_data_), 1) ds2.dissociate("tests/baselines/node_extracts2") self.assertEqual(len(ds2._associated_data_), 0) s = list( store.find(key2=DataRange("A"), file=os.path.abspath("tests/baselines/node_extracts2"))) self.assertEqual(len(s), 1) os.remove(kosh_db)
def test_any_string_dr(self): """ Given a list of string DataRanges and a datum name, we correctly get any Records. A record will be included in the return if the corresponding datum name in that record has a value of a list containing any of the given list of string DataRanges. """ list_to_check = [ DataRange(min="a", max="c", max_inclusive=True), DataRange(min="d", max="g", max_inclusive=True) ] records = list( self.record_dao.get_list(datum_name="spam", list_of_contents=list_to_check, ids_only=True, operation=ListQueryOperation.ANY)) self.assertEqual(len(records), 2) self.assertTrue(self.record_4.id in records) self.assertTrue(self.record_5.id in records) records = list( self.record_dao.get_list(datum_name="spam", list_of_contents=list_to_check, operation=ListQueryOperation.ANY)) records_to_check = { records[0].id: records[0], records[1].id: records[1] } self.assertEqual(records_to_check[self.record_4.id].raw, self.record_4.raw) self.assertEqual(records_to_check[self.record_5.id].raw, self.record_5.raw)
def test_sort_and_standardizing(self): """Test the function for processing query criteria.""" criteria = { "numra": DataRange(1, 2), "lexra": DataRange("bar", "foo"), "num": 12, "num2": 2, "listnum": ListCriteria(entries=(1, 2), operation="ALL"), "lex": "cat" } scalar, string, scalar_list, string_list = sort_and_standardize_criteria( criteria) num_equiv = DataRange(12, 12, max_inclusive=True) num2_equiv = DataRange(2, 2, max_inclusive=True) lex_equiv = DataRange("cat", "cat", max_inclusive=True) # assertCountEqual WOULD make sense, except it seems to test on object # identity and not using the == operator. Instead, we sort scalar.sort() string.sort() self.assertEqual(scalar, [("num", num_equiv), ("num2", num2_equiv), ("numra", criteria["numra"])]) self.assertEqual(string, [("lex", lex_equiv), ("lexra", criteria["lexra"])]) self.assertEqual(scalar_list[0], ("listnum", criteria["listnum"])) self.assertFalse(string_list) with self.assertRaises(ValueError) as context: sina.utils.sort_and_standardize_criteria({"bork": ["meow"]}) self.assertIn('criteria must be a number, string', str(context.exception))
def test_all_scal_dr(self): """ Given a list of scalar DataRanges and a datum name, we correctly get all Records. A record will be included in the return if the corresponding datum name in that record has a value of a list containing all of the given list of scalars DataRanges. """ list_to_check = [DataRange(0, 2), DataRange(3, 6)] records = list( self.record_dao.get_list(datum_name="eggs", list_of_contents=list_to_check, ids_only=True, operation=ListQueryOperation.ALL)) self.assertEqual(len(records), 2) self.assertTrue(self.record_2.id in records) self.assertTrue(self.record_1.id in records) records = list( self.record_dao.get_list(datum_name="eggs", list_of_contents=list_to_check, operation=ListQueryOperation.ALL)) records_to_check = { records[0].id: records[0], records[1].id: records[1] } self.assertEqual(records_to_check[self.record_1.id].raw, self.record_1.raw) self.assertEqual(records_to_check[self.record_2.id].raw, self.record_2.raw)
def test_invert_ranges_many_ranges(self): """Test that we correctly invert multiple DataRanges.""" ranges = [ DataRange(max=2, max_inclusive=False), DataRange(min=4, min_inclusive=True) ] opposite_range = DataRange(min=2, max=4) self.assertEqual(sina.utils.invert_ranges(ranges), [opposite_range])
def test_data_range_identity(self): """Test functions that return what kind of range the DataRange is.""" numerical = DataRange(min=1) lexographic = DataRange(max="foo_c") self.assertTrue(numerical.is_numeric_range()) self.assertFalse(numerical.is_lexographic_range()) self.assertTrue(lexographic.is_lexographic_range()) self.assertFalse(lexographic.is_numeric_range())
def test_data_range_is_single(self): """Test the DataRange is_single_value method.""" is_single = DataRange(2, 2, max_inclusive=True) is_also_single = DataRange("cat", "cat", max_inclusive=True) is_not_single = DataRange(2, 10, max_inclusive=True) self.assertTrue(is_single.is_single_value()) self.assertTrue(is_also_single.is_single_value()) self.assertFalse(is_not_single.is_single_value())
def test_recorddao_scalar_datum_query_no_match(self): """Test that the RecordDAO data query is retrieving no Records when there's no matches.""" too_small_range = DataRange(min=10.99999, min_inclusive=False) too_small = self.record_dao.data_query(spam_scal=too_small_range) self.assertFalse(list(too_small)) nonexistant_scalar = self.record_dao.data_query( nonexistant_scalar=DataRange(-999, 0)) self.assertFalse(list(nonexistant_scalar))
def test_recorddao_data_query_scalar_list_has_any(self): """Test that the RecordDAO is retrieving on a has_any list of scalars.""" just_5_and_6 = list( self.record_dao.data_query(val_data_list_1=has_any( DataRange(-1, 1), DataRange(7.5, 9)))) # 5 & 6 self.assertEqual(len(just_5_and_6), 2) self.assertIn("spam5", just_5_and_6) self.assertIn("spam6", just_5_and_6)
def test_invert_ranges_one_range(self): """Test that we correctly invert a single DataRange.""" data_range = DataRange(min=2, max=4) opposite_ranges = [ DataRange(max=2, max_inclusive=False), DataRange(min=4, min_inclusive=True) ] self.assertEqual(sina.utils.invert_ranges([data_range]), opposite_ranges)
def test_data_range_overlap_bad_types(self): """Test that we refuse to check type-mismatched DataRanges for overlap.""" strings = DataRange("cat", "horse") scalars = DataRange(42, 45) with self.assertRaises(TypeError) as context: self.assertFalse(strings.overlaps(scalars)) self.assertIn( 'Only DataRanges of the same type (numeric or lexicographic)', str(context.exception))
def test_merge_overlapping_ranges(self): """Test that we merge overlapping DataRanges.""" ranges = [ DataRange(max=0), DataRange(min=0, max=5), DataRange(min=3, max=4) ] merged_range = DataRange(max=5) self.assertEqual(sina.utils.merge_ranges(ranges), [merged_range])
def test_invert_ranges_multitype_error(self): """Test that a TypeError is raised if mixed types of ranges are inverted.""" ranges = [ DataRange(max="cat", max_inclusive=True), DataRange(min=2, min_inclusive=False) ] with self.assertRaises(TypeError) as context: sina.utils.invert_ranges(ranges) self.assertIn('must be only numeric DataRanges or', str(context.exception))
def test_merge_string_ranges(self): """Test that we merge string ranges correctly.""" ranges = [ DataRange(min="cat", max="giraffe", min_inclusive=False), DataRange(min="dog", max="zebra") ] merged_ranges = [ DataRange(min="cat", max="zebra", min_inclusive=False) ] self.assertEqual(sina.utils.merge_ranges(ranges), merged_ranges)
def test_invert_ranges_strings(self): """Test that range inversion works with strings.""" ranges = [ DataRange(max="cat", max_inclusive=True), DataRange(min="dog", min_inclusive=False) ] opposite_range = DataRange(min="cat", max="dog", min_inclusive=False, max_inclusive=True) self.assertEqual(sina.utils.invert_ranges(ranges), [opposite_range])
def test_data_range_equality(self): """Test the DataRange equality operator.""" basic_case = DataRange(1, 2) flipped_inclusivity = DataRange(1, 2e0, min_inclusive=False, max_inclusive=True) with_strings = DataRange("foo_a", "foo_c") basic_case_again = DataRange(1, 2) self.assertEqual(basic_case, basic_case_again) self.assertNotEqual(basic_case, flipped_inclusivity) self.assertNotEqual(basic_case, with_strings)
def test_merge_disordered_ranges(self): """Test that we correctly order our ranges and their mergings.""" ranges = [ DataRange(min=5.5), DataRange(min=3, max=4, min_inclusive=False), DataRange(min=5, max=6) ] merged_ranges = [ DataRange(min=3, max=4, min_inclusive=False), DataRange(min=5) ] self.assertEqual(sina.utils.merge_ranges(ranges), merged_ranges)
def test_recorddao_data_query_has_only_mixed(self): """ Test that the RecordDAO is retrieving on mixed data types for has_only. Test that we can mix searching on scalars and lists of scalars. """ just_5 = list( self.record_dao.data_query( val_data_list_1=has_only(DataRange(-1, 1), DataRange(7.5, 21)), # 5 & 6 spam_scal_3=DataRange(0, 50))) # 5 only self.assertEqual(len(just_5), 1) self.assertEqual(just_5[0], "spam5")
def test_invert_ranges_one_range_zeroes(self): """ Test that we correctly invert a single DataRange. Checks for correct behavior on range bounds that are "Falsey" (ex: zero). """ data_range = DataRange(min=0, max=0, max_inclusive=True) opposite_ranges = [ DataRange(max=0), DataRange(min=0, min_inclusive=False) ] self.assertEqual(sina.utils.invert_ranges([data_range]), opposite_ranges)
def test_data_range_bad_string_setters_max(self): """Test invalid string setters for DataRanges. For max only.""" flipped_inclusivity = DataRange(1, 2, min_inclusive=False, max_inclusive=True) with_strings = DataRange("foo_a", "foo_c") with self.assertRaises(TypeError) as context: flipped_inclusivity.parse_max("cat)") self.assertIn('Bad type for portion of range', str(context.exception)) with self.assertRaises(ValueError) as context: with_strings.parse_max("4") self.assertIn('Bad inclusiveness specifier', str(context.exception))
def test_recorddao_data_query_all_list_criteria(self): """ Test that the RecordDAO is retrieving on mixed data types. Test that we can mix searching on strings, scalars, lists of strings, and lists of scalars, using has_all, has_any, and has_only """ no_match = list( self.record_dao.data_query( val_data_list_1=has_any(DataRange(-10, 8.5), DataRange(8.9, 25)), # 5 & 6 spam_scal_3=DataRange(0, 50), # 5 only val_data_list_2=has_all('eggs', DataRange('o', 'z')), # 5 & 6 val_data_3=has_only(0, 9.3))) # 6 only self.assertFalse(no_match) just_5 = list( self.record_dao.data_query( val_data_list_1=has_all(DataRange(-10, 8.5), DataRange(8.9, 25)), # 5 & 6 spam_scal_3=DataRange(0, 50), # 5 only val_data_list_2=has_all('eggs', DataRange('o', 'z')), # 5 & 6 val_data_3='sugar')) # 5 only self.assertEqual(len(just_5), 1) self.assertEqual(just_5[0], "spam5")
def test_data_range_bad_assignment(self): """Tests invalid assignments for DataRanges.""" with self.assertRaises(TypeError) as context: DataRange(30, "fast") self.assertIn('Bad type for portion of range', str(context.exception)) with self.assertRaises(TypeError) as context: DataRange(["foo", "bar"], ["spam", "eggs"]) self.assertIn('Bad type for portion of range', str(context.exception)) with self.assertRaises(ValueError) as context: DataRange(30, 1) self.assertIn('min must be <= max', str(context.exception)) with self.assertRaises(ValueError) as context: DataRange() self.assertIn('Null DataRange', str(context.exception))
def test_data_range_contains(self): """Test the DataRange contains operator.""" flipped_inclusivity = DataRange(1, 2, min_inclusive=False, max_inclusive=True) inf_max = DataRange(max=100) with_strings = DataRange(min="foo_a", min_inclusive=False) self.assertTrue(2 in flipped_inclusivity) self.assertFalse(1 in flipped_inclusivity) self.assertTrue(-999 in inf_max) self.assertFalse(100 in inf_max) self.assertTrue("foo_c" in with_strings) self.assertFalse("foo_a" in with_strings)
def test_basic_data_range_scalar(self): """Test basic DataRange creation using scalars.""" basic_case = DataRange(1, 2) self.assertEqual(basic_case.min, 1) self.assertEqual(basic_case.max, 2) self.assertTrue(basic_case.min_inclusive) self.assertFalse(basic_case.max_inclusive)
def test_parse_data_string(self): """Test the function for parsing a string to names and DataRanges.""" just_one = "speed=[1,2)" basic_case = DataRange(1, 2) a_few = "speed=(1,2] quadrant=nw" flipped_inclusivity = DataRange(1, 2, min_inclusive=False, max_inclusive=True) both_sides_equal = DataRange("nw", "nw", max_inclusive=True) none = "" self.assertEqual(basic_case, sina.utils.parse_data_string(just_one)["speed"]) few_dict = sina.utils.parse_data_string(a_few) self.assertEqual(flipped_inclusivity, few_dict["speed"]) self.assertEqual(both_sides_equal, few_dict["quadrant"]) self.assertFalse(sina.utils.parse_data_string(none))
def test_data_range_inclusivity_assignment(self): """Test DataRange creation including inclusivity.""" flipped_inclusivity = DataRange(1, 2, min_inclusive=False, max_inclusive=True) self.assertFalse(flipped_inclusivity.min_inclusive) self.assertTrue(flipped_inclusivity.max_inclusive)
def test_recorddao_data_query_string_list_has_all(self): """Test that the RecordDAO is retrieving on a has_all list of strings.""" just_5_and_6 = list( self.record_dao.data_query( val_data_list_2=has_all('eggs', DataRange('o', 'z')))) # 5 & 6 self.assertEqual(len(just_5_and_6), 2) self.assertIn("spam5", just_5_and_6) self.assertIn("spam6", just_5_and_6)
def test_recorddao_string_datum_query(self): """Test that the RecordDAO data query is retrieving based on one string correctly.""" just_right_range = DataRange(min="astounding", max="runny", max_inclusive=True) just_right = self.record_dao.data_query(val_data=just_right_range) just_right_list = list(just_right) self.assertEqual(len(just_right_list), 2) six.assertCountEqual(self, just_right_list, ["spam", "spam3"])
def test_recorddao_many_scalar_data_query(self): """Test that RecordDAO's data query is retrieving on multiple scalars correctly.""" spam_and_spam_3 = DataRange(min=10) one = self.record_dao.data_query( spam_scal=spam_and_spam_3, spam_scal_2=10.5) # Matches spam_3 only self.assertEqual(len(list(one)), 1) none = self.record_dao.data_query(spam_scal=spam_and_spam_3, nonexistant=10101010) self.assertFalse(list(none))