def test_filter__date(): acc_data1 = AccountData(data_path1, cat_path) first_date = acc_data1.get_column("date")[0] next_date = acc_data1.get_column("date")[1] acc_data1.filter_data("date", ">", first_date) assert (acc_data1.get_column("date")[0] == next_date)
def test_get_total(): acc_data = AccountData(data_path1, tag_path) assert (acc_data.get_total() == -44559.5) acc_data.filter_data("tags", "==", "tag2") assert (acc_data.get_total() == -24560.0) acc_data.filter_data("date", ">", datetime.date(2020, 12, 25)) assert (acc_data.get_total() == 200.0)
def test_reset_filter(): acc_data1 = AccountData(data_path1, cat_path) original_data = acc_data1.get_data() acc_data1.filter_data("amount", "==", 1000) filtered_data = acc_data1.get_data() acc_data1.reset_filter() reset_data = acc_data1.get_data() assert (original_data == reset_data) assert (reset_data != filtered_data)
def test_filter__empty(): #If filtering twice with an inverted filter the result should be empty acc_data = AccountData(data_path1, cat_path) acc_data.filter_data("amount", ">=", 300) acc_data.filter_data("amount", "<", 300) assert (len(acc_data.get_data()) == 0) #Tags should also be empty assert (len(acc_data.get_tags()) == 0) #It should be possible to still call get_timeseries() assert (type(acc_data.get_timeseries()) == TimeSeries)
def test_update__save_load(tmp_path): account_data1 = AccountData("./example_data/data1.csv", tag_file="./example_data/tags_nested.json") account_data2 = AccountData("./example_data/data2.csv", account_name="other_data") summed_account = account_data1 / 2 + account_data2 summed_account.update("./example_data/data_new.csv", "data1") #Save and load should not affect data summed_account.save(tmp_path / "account_data.csv") summed_account = AccountData(tmp_path / "account_data.csv") #Old data summed_account.filter_data("date", "==", datetime.date(2021, 1, 1)) assert (summed_account.get_column("text") == [ 'Lorem Ipsum', 'Lorem Ipsum' ]) summed_account.reset_filter() #New data summed_account.filter_data("date", "==", datetime.date(2021, 1, 5)) assert (summed_account.get_column("text") == ['A1', 'SWISH FRÅN Namn']) summed_account.reset_filter() #Overlapping data should be updated summed_account.filter_data("date", "==", datetime.date(2021, 1, 4)) assert (summed_account.get_column("text") == [ '201229 A1', '201230 A3 ', '210101 B1', '210102 a4', 'Updated data' ]) summed_account.filter_data("account", "==", "data1") assert (summed_account.get_column("amount") == [333.33 / 2]) summed_account.reset_filter()
def test_filter__amount(fun): acc_data1 = AccountData(data_path1, cat_path) acc_data1.filter_data("amount", fun, 200) for amount in acc_data1.get_column("amount"): if fun == ">": assert (amount > 200) elif fun == ">=": assert (amount >= 200) elif fun == "==": assert (amount == 200) elif fun == "!=": assert (amount != 200) elif fun == "<=": assert (amount <= 200) elif fun == "<": assert (amount < 200)
def test_filter__type_wrong(): acc_data1 = AccountData(data_path1, cat_path) with pytest.raises(ValueError): acc_data1.filter_data("amount", "==", "text") with pytest.raises(ValueError): acc_data1.filter_data("date", ">", 2) with pytest.raises(ValueError): acc_data1.filter_data("text", ">", "text") with pytest.raises(ValueError): acc_data1.filter_data("category", "==", datetime.date(2021, 1, 1))
def test_get_average__filtered(): acc_data = AccountData(data_path1, tag_path) #If we filter by something by other than date, we should still accumulate to the full extent of the data acc_data.filter_data("tags", "==", "tag2") assert (acc_data.get_average("Day") == sum(acc_data.get_column("amount")) / 26) #If filter by date, accumulate should only be within the filtered dates acc_data.reset_filter() acc_data.filter_data("date", ">", datetime.date(2020, 12, 13)) acc_data.filter_data("date", "<=", datetime.date(2020, 12, 20)) assert (acc_data.get_average("Week") == (1000 + 100 - 25000) / 1) #The filter function should detect if the date filter is greater than the data and it should affect the average acc_data.reset_filter() acc_data.filter_data("date", ">=", datetime.date(2020, 12, 7)) acc_data.filter_data("date", "<", datetime.date(2020, 12, 23)) assert (acc_data.get_average("Week") == (1000 + 100 - 25000) / 1)
def test_filter__multi_tags_list(): #When filtering with a list, only the tags with that litteral list should kept acc_data = AccountData(data_path1, tag_file=tag_path) acc_data.filter_data("tags", "==", ["överföring"]) assert (acc_data.get_tags() == ["överföring"])
def test_filter__multi_tags_any(): #When filtering "all" with a list, other tags in list should be kept acc_data = AccountData(data_path1, tag_file=tag_path) acc_data.filter_data("tags", "any", ["tag1", "tag3"]) assert ("tag1" in acc_data.get_tags()) assert ("tag3" in acc_data.get_tags())
def test_get_average__empty(): #Should return 0 if there is no data acc_data = AccountData(data_path1, tag_nested_path) acc_data.filter_data("tags", "==", "Non-existing") assert (acc_data.get_average("Day") == 0)
def test_filter__tag_list(): acc_data = AccountData(data_path1, tag_file=tag_path) acc_data.filter_data("tags", "==", ["tag1", "överföring"]) #Should only contain the once that contains all the tags. assert (sorted(acc_data.get_tags()) == sorted(["tag1", "överföring"]))
def test_filter__tag_list_order(): acc_data = AccountData(data_path1, tag_file=tag_path) #If the order of the tags is different, then it should still yield the same result acc_data.filter_data("tags", "==", ["överföring", "tag1"]) #Should only contain the once that contains all the tags. assert (sorted(acc_data.get_tags()) == sorted(["tag1", "överföring"]))
def test_filter__tags(): acc_data = AccountData(data_path1, tag_file=tag_path) acc_data.filter_data("tags", "!=", "tag1") assert ("tag1" not in acc_data.get_tags()) acc_data.filter_data("tags", "==", "tag3") assert (acc_data.get_tags() == ["tag3"])
def test_filter__multi_tags(): #If a data point has multiple tags, those tags should be preserved acc_data = AccountData(data_path1, tag_file=tag_path) acc_data.filter_data("tags", "==", "överföring") assert (len(acc_data.get_tags()) > 1)
def test_filter__tag_empty(): acc_data = AccountData(data_path1, tag_file=tag_path) #Should only return those that lack any tags. acc_data.filter_data("tags", "==", []) assert (acc_data.get_tags() == []) assert (len(acc_data.get_data()) > 0)
def test_filter__multi_tags_all(): #When filtering "all" with a list, other tags in list should be kept acc_data = AccountData(data_path1, tag_file=tag_path) acc_data.filter_data("tags", "all", ["överföring"]) assert ("överföring" in acc_data.get_tags()) assert (len(acc_data.get_tags()) > 1)
def test_filter__type_ok(): acc_data1 = AccountData(data_path1, cat_path) acc_data1.filter_data("amount", ">", 100) acc_data1.filter_data("amount", ">", 200.5) acc_data1.filter_data("text", "!=", "test") acc_data1.filter_data("date", ">", datetime.date(2021, 1, 4))
def test_filter__not_tag_list(): acc_data = AccountData(data_path1, tag_file=tag_path) acc_data.filter_data("tags", "!=", ["tag1", "överföring"]) #Should still contain överföring, just not any that also tag1 assert ("överföring" in acc_data.get_tags())
def test_filter__not_tag_empty(): acc_data = AccountData(data_path1, tag_file=tag_path) #Should only return those that has tags acc_data.filter_data("tags", "!=", []) assert (all(len(tags) > 0 for tags in acc_data.get_column("tags")))