<commit_before>from nose.tools import raises

from .util import TestCase, load_json_fixture, load_csv

from babbage.cube import Cube
from babbage.exc import BindingException, QueryException


class CubeTestCase(TestCase):

    def setUp(self):
        super(CubeTestCase, self).setUp()
        self.cra_model = load_json_fixture('models/cra.json')
        self.cra_table = load_csv('cra.csv')
        self.cra_table = load_csv('cap_or_cur.csv')
        self.cube = Cube(self.engine, 'cra', self.cra_model)

    def test_table_exists(self):
        assert self.engine.has_table(self.cra_table.name)

    def test_table_load(self):
        table = self.cube._load_table(self.cra_table.name)
        assert table is not None
        assert 'cra' in repr(self.cube)

    def test_table_pk(self):
        assert self.cube.fact_pk is not None

    @raises(BindingException)
    def test_table_load_nonexist(self):
        self.cube._load_table('lalala')

    @raises(BindingException)
    def test_dimension_column_nonexist(self):
        model = self.cra_model.copy()
        model['dimensions']['cofog1']['attributes']['name']['column'] = 'lala'
        self.cube = Cube(self.engine, 'cra', model)
        self.cube.model['cofog1.name'].bind(self.cube)

    @raises(BindingException)
    def test_star_column_nonexist(self):
        model = self.cra_model.copy()
        model['dimensions']['cap_or_cur']['join_column'] = 'lala'
        self.cube = Cube(self.engine, 'cra', model)
        self.cube.facts()

    @raises(BindingException)
    def test_attr_table_different_from_dimension_key(self):
        model = self.cra_model.copy()
        model['dimensions']['cap_or_cur']['attributes']['code']['column'] = 'cap_or_cur'
        self.cube = Cube(self.engine, 'cra', model)
        self.cube.facts()

    def test_dimension_column_qualified(self):
        model = self.cra_model.copy()
        name = 'cra.cofog1_name'
        model['dimensions']['cofog1']['attributes']['name']['column'] = name
        self.cube = Cube(self.engine, 'cra', model)
        self.cube.model['cofog1.name'].bind(self.cube)

    def test_facts_basic(self):
        facts = self.cube.facts()
        assert facts['total_fact_count'] == 36
        assert len(facts['data']) == 36, len(facts['data'])
        row0 = facts['data'][0]
        assert 'cofog1.name' in row0, row0
        assert 'amount' in row0, row0
        assert 'amount.sum' not in row0, row0
        assert '_count' not in row0, row0

    def test_facts_basic_filter(self):
        facts = self.cube.facts(cuts='cofog1:"4"')
        assert facts['total_fact_count'] == 12, facts
        assert len(facts['data']) == 12, len(facts['data'])

    def test_facts_set_filter(self):
        facts = self.cube.facts(cuts='cofog1:"4";"10"')
        assert facts['total_fact_count'] == 23, facts
        assert len(facts['data']) == 23, len(facts['data'])

    def test_facts_star_filter(self):
        facts = self.cube.facts(cuts='cap_or_cur.label:"Current Expenditure"')
        assert facts['total_fact_count'] == 21, facts
        assert len(facts['data']) == 21, len(facts['data'])

    def test_facts_star_filter_and_facts_field(self):
        facts = self.cube.facts(cuts='cap_or_cur.label:"Current Expenditure"',
                                fields='cofog1')
        assert facts['total_fact_count'] == 21, facts<commit_msg>[#18] Add support for star schemas (JD Bothma @ Code4SA)<commit_after>from nose.tools import raises

from .util import TestCase, load_json_fixture, load_csv

from babbage.cube import Cube
from babbage.exc import BindingException, QueryException


class CubeTestCase(TestCase):

    def setUp(self):
        super(CubeTestCase, self).setUp()
        self.cra_model = load_json_fixture('models/cra.json')
        self.cra_table = load_csv('cra.csv')
        self.cra_table = load_csv('cap_or_cur.csv')
        self.cube = Cube(self.engine, 'cra', self.cra_model)

    def test_table_exists(self):
        assert self.engine.has_table(self.cra_table.name)

    def test_table_load(self):
        table = self.cube._load_table(self.cra_table.name)
        assert table is not None
        assert 'cra' in repr(self.cube)

    def test_table_pk(self):
        assert self.cube.fact_pk is not None

    @raises(BindingException)
    def test_table_load_nonexist(self):
        self.cube._load_table('lalala')

    @raises(BindingException)
    def test_dimension_column_nonexist(self):
        model = self.cra_model.copy()
        model['dimensions']['cofog1']['attributes']['name']['column'] = 'lala'
        self.cube = Cube(self.engine, 'cra', model)
        self.cube.model['cofog1.name'].bind(self.cube)

    @raises(BindingException)
    def test_star_column_nonexist(self):
        model = self.cra_model.copy()
        model['dimensions']['cap_or_cur']['join_column'] = 'lala'
        self.cube = Cube(self.engine, 'cra', model)
        self.cube.facts()

    @raises(BindingException)
    def test_attr_table_different_from_dimension_key(self):
        model = self.cra_model.copy()
        model['dimensions']['cap_or_cur']['attributes']['code']['column'] = 'cap_or_cur'
        self.cube = Cube(self.engine, 'cra', model)
        self.cube.facts()

    def test_dimension_column_qualified(self):
        model = self.cra_model.copy()
        name = 'cra.cofog1_name'
        model['dimensions']['cofog1']['attributes']['name']['column'] = name
        self.cube = Cube(self.engine, 'cra', model)
        self.cube.model['cofog1.name'].bind(self.cube)

    def test_facts_basic(self):
        facts = self.cube.facts()
        assert facts['total_fact_count'] == 36
        assert len(facts['data']) == 36, len(facts['data'])
        row0 = facts['data'][0]
        assert 'cofog1.name' in row0, row0
        assert 'amount' in row0, row0
        assert 'amount.sum' not in row0, row0
        assert '_count' not in row0, row0

    def test_facts_basic_filter(self):
        facts = self.cube.facts(cuts='cofog1:"4"')
        assert facts['total_fact_count'] == 12, facts
        assert len(facts['data']) == 12, len(facts['data'])

    def test_facts_set_filter(self):
        facts = self.cube.facts(cuts='cofog1:"4";"10"')
        assert facts['total_fact_count'] == 23, facts
        assert len(facts['data']) == 23, len(facts['data'])

    def test_facts_star_filter(self):
        facts = self.cube.facts(cuts='cap_or_cur.label:"Current Expenditure"')
        assert facts['total_fact_count'] == 21, facts
        assert len(facts['data']) == 21, len(facts['data'])

    def test_facts_star_filter_and_facts_field(self):
        facts = self.cube.facts(cuts='cap_or_cur.label:"Current Expenditure"',
                                fields='cofog1')
        assert facts['total_fact_count'] == 21, facts
        assert len(facts['data']) == 21, len(facts['data'])

    def test_facts_star_filter_and_field(self):
        facts = self.cube.facts(cuts='cap_or_cur.label:"Current Expenditure"',
                                fields='cap_or_cur.code')
        assert facts['total_fact_count'] == 21, facts
        assert len(facts['data']) == 21, len(facts['data'])

    def test_facts_facts_filter_and_star_field(self):
        facts = self.cube.facts(cuts='cofog1:"4"',
                                fields='cap_or_cur.code')
        assert facts['total_fact_count'] == 12, facts
        assert len(facts['data']) == 12, len(facts['data'])

    def test_facts_star_filter(self):
        facts = self.cube.facts(cuts='cap_or_cur.label:"Current Expenditure"')
        assert facts['total_fact_count'] == 21, facts
        assert len(facts['data']) == 21, len(facts['data'])

    def test_facts_star_filter_and_facts_field(self):
        facts = self.cube.facts(cuts='cap_or_cur.label:"Current Expenditure"',
                                fields='cofog1')
        assert facts['total_fact_count'] == 21, facts
{'fraction_non_alphanumeric': 0.14182509505703422, 'fraction_numerical': 0.013181242078580482, 'mean_word_length': 3.1641160949868072, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3469462', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2552, 'n_words': 449}