def test_add_reserveds():
    # Define values to be updated, and their new values
    names_to_update = {
        "variable": "v001",
        "variable_row": "v002",
        "scenario": "s001",
        "decision": "d001",
        "decision_row": "d002"
    }

    for ident_name, obfuscated_name in names_to_update.iteritems():
        identifier_row = get_identifier(None)
        save_identifier(identifier_row,
                        name=ident_name,
                        obfuscated_name=obfuscated_name)

    bnf_parser = ObfuscateKivyBNF(get_obfuscated_name)

    # Identifiers after leading reserved attributes should not be obfuscated
    # They should be added to reserved and removed from identifiers
    bnf_parser.attribs.parseString(
        "decision.scenario(variable) = reserved_one.variable "
        "+ view.reserved_one")

    assert bnf_parser.statement.transformString(
        "decision.scenario(variable) = reserved_one.variable") == \
        "d001.s001(variable)=reserved_one.variable"

    assert get_reserved_by_name('variable').primary_package == 'reserved_one'
    assert get_identifier_by_name('variable').obfuscated_name == 'variable'
    with pytest.raises(DoesNotExist):
        assert get_identifier_by_name('view').obfuscated_name != 'view'
def test_transform_conseq_idents_numbs():
    # Define values to be updated, and their new values
    names_to_update = {
        "variable": "v001",
        "variable_row": "v002",
        "scenario": "s001",
        "decision": "d001",
        "decision_row": "d002"
    }

    for ident_name, obfuscated_name in names_to_update.iteritems():
        identifier_row = get_identifier(None)
        save_identifier(identifier_row,
                        name=ident_name,
                        obfuscated_name=obfuscated_name)

    bnf_parser = ObfuscateKivyBNF(get_obfuscated_name)

    # Test that numbers separated from idents and they're not obfuscated
    assert bnf_parser.statement.transformString(
        "reserved_one 1 reserved_one -2.0") == \
        "reserved_one 1 reserved_one -2.0"
    assert bnf_parser.statement.transformString(
        "reserved_one <= 0 reserved_one >= 1") == \
        "reserved_one<=0 reserved_one>=1"
def test_add_reserveds():
    # Define values to be updated, and their new values
    names_to_update = {
        "variable": "v001",
        "variable_row": "v002",
        "scenario": "s001",
        "decision": "d001",
        "decision_row": "d002"
        }

    for ident_name, obfuscated_name in names_to_update.iteritems():
        identifier_row = get_identifier(None)
        save_identifier(
            identifier_row,
            name=ident_name,
            obfuscated_name=obfuscated_name
            )

    bnf_parser = ObfuscatePythonBNF(get_obfuscated_name)

    # Identifiers after leading reserved attributes should not be obfuscated
    # They should be added to reserved and removed from identifiers
    bnf_parser.attribs.parseString(
        "decision.scenario(variable) = reserved_one.variable "
        "+ view.reserved_one")

    assert bnf_parser.statement.transformString(
        "decision.scenario(variable) = reserved_one.variable") == \
        "d001.s001(variable)=reserved_one.variable"

    assert get_reserved_by_name('variable').primary_package == 'reserved_one'
    assert get_identifier_by_name('variable').obfuscated_name == 'variable'
    with pytest.raises(DoesNotExist):
        assert get_identifier_by_name('view').obfuscated_name != 'view'
Example #4
0
def test_save_identifier_duplicate():

    # Fail at duplicate add
    identifier_row = get_identifier(None)
    assert not identifier_row.id
    # Duplicate name
    with pytest.raises(IntegrityError):
        assert save_identifier(identifier_row,
                               name=u'identifier_one',
                               obfuscated_name='bbb')

    # Duplicate obfuscated name
    with pytest.raises(IntegrityError):
        assert save_identifier(identifier_row,
                               name=u'identifier_xxx',
                               obfuscated_name='aaa')

    # Duplicate randomly assigned obfuscated name
    identifier_row = get_identifier(None)
    set_random_seed(1)
    assert save_identifier(identifier_row, name=u'identifier_001')
    ident_001_obfuscated_name = get_identifier_by_name(
        u'identifier_001').obfuscated_name
    # Reset random seed to recreate last result
    identifier_row = get_identifier(None)
    set_random_seed(1)
    assert save_identifier(identifier_row, name=u'identifier_002')

    assert get_identifier_by_name(u'identifier_002').obfuscated_name != \
        ident_001_obfuscated_name
def test_transform_conseq_idents_numbs():
    # Define values to be updated, and their new values
    names_to_update = {
        "variable": "v001",
        "variable_row": "v002",
        "scenario": "s001",
        "decision": "d001",
        "decision_row": "d002"
        }

    for ident_name, obfuscated_name in names_to_update.iteritems():
        identifier_row = get_identifier(None)
        save_identifier(
            identifier_row,
            name=ident_name,
            obfuscated_name=obfuscated_name
            )

    bnf_parser = ObfuscatePythonBNF(get_obfuscated_name)

    # Test that numbers separated from idents and they're not obfuscated
    assert bnf_parser.statement.transformString(
        "reserved_one 1 reserved_one -2.0") == \
        "reserved_one 1 reserved_one -2.0"
    assert bnf_parser.statement.transformString(
        "reserved_one <= 0 reserved_one >= 1") == \
        "reserved_one<=0 reserved_one>=1"
def test_transform_statement():
    # Define values to be updated, and their new values
    names_to_update = {
        "variable": "v001",
        "variable_row": "v002",
        "search_variables": "v003",
        "variable_list": "v004",
        "variable_id": "v005",
        "select_variable": "v006",
        "variable_name": "v007",
        "VarValues": "v008",
        "scenario": "s001",
        "scenario_row": "s002",
        "search_scenarios": "s003",
        "scenario_list": "s004",
        "scenario_id": "s005",
        "select_scenario": "s006",
        "is_for_scenario_result": "s007",
        "is_for_variable_name": "s008",
        "decision": "d001",
        "decision_row": "d002",
        "search_decision": "d003",
        "decision_list": "d004",
        "decision_id": "d005",
        "is_edit_mode": "x001",
        }

    for ident_name, obfuscated_name in names_to_update.iteritems():
        identifier_row = get_identifier(None)
        save_identifier(
            identifier_row,
            name=ident_name,
            obfuscated_name=obfuscated_name
            )

    ##################
    # Test kivy source
    ##################
    bnf_parser = ObfuscateKivyBNF(get_obfuscated_name)

    # Test simple substitution
    assert bnf_parser.statement.transformString(
        "<VarValues>") == \
        "<v008>"

    # Test internationalization function
    assert bnf_parser.statement.transformString(
        "text: _('Variable type: Values')") == \
        "text:_('Variable type: Values')"

    # Test function
    assert bnf_parser.statement.transformString(
        "height: dp(self.variable) * font_multiplier + "
        "scenario.height") == \
        "height:dp(self.v001)*font_multiplier+s001.height"

    # Test kivy directive
    assert bnf_parser.statement.transformString(
        "#: import ButtonBar view.scenario.ButtonBar") == \
        "#: import ButtonBar view.s001.ButtonBar"
def test_transform_statement():
    # Define values to be updated, and their new values
    names_to_update = {
        "variable": "v001",
        "variable_row": "v002",
        "search_variables": "v003",
        "variable_list": "v004",
        "variable_id": "v005",
        "select_variable": "v006",
        "variable_name": "v007",
        "VarValues": "v008",
        "scenario": "s001",
        "scenario_row": "s002",
        "search_scenarios": "s003",
        "scenario_list": "s004",
        "scenario_id": "s005",
        "select_scenario": "s006",
        "is_for_scenario_result": "s007",
        "is_for_variable_name": "s008",
        "decision": "d001",
        "decision_row": "d002",
        "search_decision": "d003",
        "decision_list": "d004",
        "decision_id": "d005",
        "is_edit_mode": "x001",
    }

    for ident_name, obfuscated_name in names_to_update.iteritems():
        identifier_row = get_identifier(None)
        save_identifier(identifier_row,
                        name=ident_name,
                        obfuscated_name=obfuscated_name)

    ##################
    # Test kivy source
    ##################
    bnf_parser = ObfuscateKivyBNF(get_obfuscated_name)

    # Test simple substitution
    assert bnf_parser.statement.transformString(
        "<VarValues>") == \
        "<v008>"

    # Test internationalization function
    assert bnf_parser.statement.transformString(
        "text: _('Variable type: Values')") == \
        "text:_('Variable type: Values')"

    # Test function
    assert bnf_parser.statement.transformString(
        "height: dp(self.variable) * font_multiplier + "
        "scenario.height") == \
        "height:dp(self.v001)*font_multiplier+s001.height"

    # Test kivy directive
    assert bnf_parser.statement.transformString(
        "#: import ButtonBar view.scenario.ButtonBar") == \
        "#: import ButtonBar view.s001.ButtonBar"
Example #8
0
def session(database, request):
    from logic.identifier import Identifier, get_identifier, save_identifier
    from logic.reserved import Reserved, get_reserved, save_reserved
    from data.base import obfuscatedb

    obfuscatedb.create_tables([Identifier,
                               Reserved], safe=True)

    # Add a decision, decision_id = 1
    identifier_row = get_identifier(None)
    assert 1 == save_identifier(
        identifier_row,
        name=u'identifier_one',
        obfuscated_name=u'aaa')

    reserved_row = get_reserved(None)
    assert 1 == save_reserved(
        reserved_row,
        name=u'reserved_one',
        primary_package=u'module')

    assert get_identifier(1).name == 'identifier_one'
    assert get_identifier(1).obfuscated_name == 'aaa'
    assert get_identifier(2).name == 'reserved_one'
    assert get_identifier(2).obfuscated_name == 'reserved_one'

    def teardown():
        obfuscatedb.drop_tables([Identifier,
                                 Reserved], safe=True)

    request.addfinalizer(teardown)
    return session
Example #9
0
def test_search_identifiers():
    from logic.identifier import get_identifier, save_identifier, \
        search_identifiers

    identifier_row = get_identifier(1)
    assert identifier_row.name == u'identifier_one'

    # Find One
    identifier_list = search_identifiers(u'identifier')
    count = 0
    for d in identifier_list:
        count += 1
        assert 'one' in d['name']
    assert count == 1

    # Add a identifier, id = 3
    identifier_row = get_identifier(None)
    assert 3 == save_identifier(identifier_row,
        name=u'identifier_two')

    # Find One and Two
    identifier_list = search_identifiers(u'iden%tifie &_')
    count = 0
    for d in identifier_list:
        count += 1
        assert 'identifier' in d['name']
    assert count == 2

    # Find only Two
    identifier_list = search_identifiers(u'two')
    count = 0
    for d in identifier_list:
        count += 1
        assert 'two' in d['name']
    assert count == 1

    # Find none
    identifier_list = search_identifiers(u'id xxx')
    count = 0
    for d in identifier_list:
        count += 1
    assert count == 0

    # Find all with blank search
    identifier_list = search_identifiers(u'')
    count = 0
    for d in identifier_list:
        count += 1
        assert 'identifier' in d['name'] or 'reserved_one' in d['name']
    assert count == 3

    # Find all with None search
    identifier_list = search_identifiers(None)
    count = 0
    for d in identifier_list:
        count += 1
        assert 'identifier' in d['name'] or 'reserved_one' in d['name']
    assert count == 3
Example #10
0
def test_save_identifier_reserved():
    from logic.identifier import save_identifier, get_identifier

    identifier_row = get_identifier(None)
    assert save_identifier(identifier_row,
                           name=u'Identifier_Two',
                           obfuscated_name='reserved_one') == 3

    assert get_identifier(3).obfuscated_name != 'reserved_one'
Example #11
0
def test_save_identifier_bad_column_name():
    from logic.identifier import save_identifier, get_identifier

    identifier_row = get_identifier(None)
    with pytest.raises(AttributeError):
        assert save_identifier(identifier_row,
                               name=u'Identifier One BAD FIELD NAME',
                               obfuscted_name='xxx',
                               xxx='Does not exist')
Example #12
0
def test_save_reserved_reassign():
    from logic.reserved import save_reserved, get_reserved
    from logic.identifier import get_identifier_by_name, save_identifier, \
        get_identifier

    identifier_row = get_identifier(None)
    save_identifier(
        identifier_row,
        name='some_ident',
        obfuscated_name='Reserved'
        )
    assert get_identifier_by_name('some_ident').obfuscated_name == 'Reserved'

    reserved_row = get_reserved(None)
    save_reserved(
        reserved_row,
        name=u'Reserved')

    assert get_identifier_by_name('some_ident').obfuscated_name != 'Reserved'
Example #13
0
def test_save_identifier_bad_column_name():
    from logic.identifier import save_identifier, get_identifier

    identifier_row = get_identifier(None)
    with pytest.raises(AttributeError):
        assert save_identifier(
            identifier_row,
            name=u'Identifier One BAD FIELD NAME',
            obfuscted_name='xxx',
            xxx='Does not exist')
Example #14
0
def test_save_identifier_reserved():
    from logic.identifier import save_identifier, get_identifier

    identifier_row = get_identifier(None)
    assert save_identifier(
        identifier_row,
        name=u'Identifier_Two',
        obfuscated_name='reserved_one') == 3

    assert get_identifier(3).obfuscated_name != 'reserved_one'
Example #15
0
def add_identifiers(identifier_list=None, do_obfuscate=True):
    """Add identifier and obfuscated names to Identifiers table.

    Parameters
    ----------
    do_obfuscate : bool
    identifier_list : list
    """
    for identifier_name in identifier_list:
        # Skip identifiers in reserved
        try:
            get_reserved_by_name(identifier_name)
        except DoesNotExist:
            pass
        else:
            continue

        if not do_obfuscate \
                or identifier_name[0:2] == '__' \
                or identifier_name == '__init__.py' \
                or identifier_name.startswith('test_') \
                or identifier_name.endswith('_test.py'):
            obfuscated_name = identifier_name
        else:
            obfuscated_name = ''

        identifier_row = get_identifier(None)
        try:
            save_identifier(identifier_row,
                            name=identifier_name,
                            obfuscated_name=obfuscated_name)
        except IntegrityError as e:
            if 'unique' not in e.message.lower():
                raise
            # If should not be obfuscated, replace obfuscated, o/w pass
            if not do_obfuscate:
                identifier_row = get_identifier_by_name(identifier_name)
                if identifier_row.obfuscated_name != identifier_name:
                    save_identifier(identifier_row,
                                    obfuscated_name=identifier_name)
Example #16
0
def save_reserved(reserved_row, **kwargs):
    """Save a Reserved row."""
    from logic.identifier import get_identifier_by_name, \
        get_identifier_by_obfuscated, save_identifier, get_identifier
    try:
        for name, value in kwargs.iteritems():
            getattr(reserved_row, name)  # Make sure column exists
            setattr(reserved_row, name, value)

    except AttributeError:
        raise

    with obfuscatedb.atomic():
        try:
            reserved_id = save_row(reserved_row, **kwargs)
        except IntegrityError:
            raise

        ####################
        # Update identifiers
        ####################
        if reserved_row.name[0] in [reserved_prefixes.reserved_dir,
                                    reserved_prefixes.non_obfuscated_dir]:
            identifier_name = reserved_row.name[1:]
        elif reserved_row.name[0] in [reserved_prefixes.reserved_file,
                                      reserved_prefixes.non_obfuscated_file]:
            identifier_name = reserved_row.name[1:-3]
        else:
            identifier_name = reserved_row.name

        # Reassign identifier obfuscated name if it exists for another name
        try:
            identifier_row = get_identifier_by_obfuscated(identifier_name)
        except DoesNotExist:
            pass
        else:
            if identifier_row.name != identifier_name:
                identifier_row.obfuscated_name = None
                save_identifier(identifier_row)

        # Unobfuscate name in identifiers
        try:
            identifier_row = get_identifier_by_name(identifier_name)
        except DoesNotExist:
            identifier_row = get_identifier(None)
            save_identifier(
                identifier_row,
                name=identifier_name,
                obfuscated_name=identifier_name)
        else:
            if identifier_row.obfuscated_name != identifier_name:
                save_identifier(
                    identifier_row,
                    name=identifier_name,
                    obfuscated_name=identifier_name)

        return reserved_id
Example #17
0
def test_save_identifier_duplicate():

    # Fail at duplicate add
    identifier_row = get_identifier(None)
    assert not identifier_row.id
    # Duplicate name
    with pytest.raises(IntegrityError):
        assert save_identifier(
            identifier_row,
            name=u'identifier_one',
            obfuscated_name='bbb')

    # Duplicate obfuscated name
    with pytest.raises(IntegrityError):
        assert save_identifier(
            identifier_row,
            name=u'identifier_xxx',
            obfuscated_name='aaa')


    # Duplicate randomly assigned obfuscated name
    identifier_row = get_identifier(None)
    set_random_seed(1)
    assert save_identifier(
        identifier_row,
        name=u'identifier_001')
    ident_001_obfuscated_name = get_identifier_by_name(
        u'identifier_001').obfuscated_name
    # Reset random seed to recreate last result
    identifier_row = get_identifier(None)
    set_random_seed(1)
    assert save_identifier(
        identifier_row,
        name=u'identifier_002')

    assert get_identifier_by_name(u'identifier_002').obfuscated_name != \
        ident_001_obfuscated_name
def test_transform_builder():
    # Define values to be updated, and their new values
    names_to_update = {
        "variable": "v001",
        "variable_row": "v002",
        "scenario": "s001",
        "decision": "d001",
        "decision_row": "d002"
        }

    for ident_name, obfuscated_name in names_to_update.iteritems():
        identifier_row = get_identifier(None)
        save_identifier(
            identifier_row,
            name=ident_name,
            obfuscated_name=obfuscated_name
            )

    bnf_parser = ObfuscatePythonBNF(get_obfuscated_name)

    # Test kivy Builder
    assert bnf_parser.builder.transformString(
        "Builder.load_file('view/decision/scenario.kv')") == \
        "Builder.load_file('view/d001/s001.kv')"
Example #19
0
def test_logic_identifier_delete():
    from logic.identifier import delete_identifier, get_identifier, \
        save_identifier

    identifier_row = get_identifier(None)
    assert save_identifier(identifier_row, name=u'Identifier Two')

    assert get_identifier(2).id == 2, 'Record not present or wrong id'

    # Delete an existing identifier
    row = get_identifier(2)
    assert delete_identifier(row) == 1

    with pytest.raises(DoesNotExist):
        assert get_identifier(2).id == 2, 'Record not found'

    # Fail to delete a non-existing identifier
    with pytest.raises(DoesNotExist):
        assert delete_identifier(row)
Example #20
0
def test_logic_identifier_delete():
    from logic.identifier import delete_identifier, get_identifier, \
        save_identifier

    identifier_row = get_identifier(None)
    assert save_identifier(
        identifier_row,
        name=u'Identifier Two')

    assert get_identifier(2).id == 2, 'Record not present or wrong id'

    # Delete an existing identifier
    row = get_identifier(2)
    assert delete_identifier(row) == 1

    with pytest.raises(DoesNotExist):
        assert get_identifier(2).id == 2, 'Record not found'

    # Fail to delete a non-existing identifier
    with pytest.raises(DoesNotExist):
        assert delete_identifier(row)
Example #21
0
def test_save_identifier_add():
    from logic.identifier import save_identifier, get_identifier

    identifier_row = get_identifier(None)
    save_identifier(identifier_row,
                    name=u'Identifier_Two',
                    obfuscated_name='bbb')

    assert get_identifier(3).id == 3

    # Change identifier
    save_identifier(identifier_row, name=u'Test_Change_Identifier')

    assert u'Test_Change_Identifier' == get_identifier(3).name
    # Change obfuscated name
    save_identifier(identifier_row, obfuscated_name=u'zzz')

    assert u'zzz' == get_identifier(3).obfuscated_name
Example #22
0
def test_save_identifier_add():
    from logic.identifier import save_identifier, get_identifier

    identifier_row = get_identifier(None)
    save_identifier(
        identifier_row,
        name=u'Identifier_Two',
        obfuscated_name='bbb')

    assert get_identifier(3).id == 3

    # Change identifier
    save_identifier(
        identifier_row,
        name=u'Test_Change_Identifier')

    assert u'Test_Change_Identifier' == get_identifier(3).name
    # Change obfuscated name
    save_identifier(
        identifier_row,
        obfuscated_name=u'zzz')

    assert u'zzz' == get_identifier(3).obfuscated_name
def test_transform_statement():
    # Define values to be updated, and their new values
    names_to_update = {
        "variable": "v001",
        "variable_row": "v002",
        "search_variables": "v003",
        "variable_list": "v004",
        "variable_id": "v005",
        "select_variable": "v006",
        "variable_name": "v007",
        "VarValues": "v008",
        "scenario": "s001",
        "scenario_row": "s002",
        "search_scenarios": "s003",
        "scenario_list": "s004",
        "scenario_id": "s005",
        "select_scenario": "s006",
        "is_for_scenario_result": "s007",
        "is_for_variable_name": "s008",
        "decision": "d001",
        "decision_row": "d002",
        "search_decision": "d003",
        "decision_list": "d004",
        "decision_id": "d005",
        "is_edit_mode": "x001",
        }

    for ident_name, obfuscated_name in names_to_update.iteritems():
        identifier_row = get_identifier(None)
        save_identifier(
            identifier_row,
            name=ident_name,
            obfuscated_name=obfuscated_name
            )

    ####################
    # Test python source
    ####################
    bnf_parser = ObfuscatePythonBNF(get_obfuscated_name)

    # Test simple substitution
    assert bnf_parser.statement.transformString(
        "    update(x.scenario_row, decision_id.y, z)") == \
        "    update(x.s002,d005.y,z)"

    # Test substitutions with other non-name symbols
    assert bnf_parser.statement.transformString(
        "    update(x.scenario_row, z) + 5*27 /3 == 'some junk'") == \
        "    update(x.s002,z)+5*27/3=='some junk'"

    # Test string parameters
    assert bnf_parser.statement.transformString(
        "    update(x.scenario_row, 'decision_id', z)") == \
        "    update(x.s002,'decision_id',z)"

    assert bnf_parser.statement.transformString(
        "variable.scenario(is_edit_mode) = scenario_list  # Do something") == \
        "v001.s001(x001)=s004"

    # Test init def
    assert bnf_parser.statement.transformString(
        "    def __init__(self,*args,**kwargs):") == \
        "    def __init__(self,*args,**kwargs):"

    # Test with None
    assert bnf_parser.statement.transformString(
        "self.get_variable_values = kwargs.get('get_values', None)") == \
        "self.get_variable_values=kwargs.get('get_values',None)"

    # Test with keyword from and import; multiple consecutive idents
    assert bnf_parser.statement.transformString(
        "from scenario import ValidationError, variable.xyz") == \
        "from s001 import ValidationError,v001.xyz"

    # Test with keyword except; multiple consecutive idents
    assert bnf_parser.statement.transformString(
        "    except FloatingPointError:") == \
        "    except FloatingPointError:"

    # Test with separator characters
    assert bnf_parser.statement.transformString(
        "print scenario, import % 2") == \
        "print s001,import%2"

    # Test dictionary
    assert bnf_parser.statement.transformString("[]") == "[]"
    assert bnf_parser.statement.transformString(
        "{'scenario': decision, scenario: 'decision'}") == \
        "{'scenario':d001,s001:'decision'}"

    # Test list
    assert bnf_parser.statement.transformString("a={}") == "a={}"
    assert bnf_parser.statement.transformString("**a=={}") == "**a=={}"
    assert bnf_parser.statement.transformString(
        "['scenario', {xyz: decision},scenario,[a,b],  'decision']") == \
        "['scenario',{xyz:d001},s001,[a,b],'decision']"

    # Test that sci-notation numbers are not obfuscated
    assert bnf_parser.fnumber.transformString(
        "(-3.969683028665376e+01, 2.209460984245205e+02)") == \
        "(-3.969683028665376e+01, 2.209460984245205e+02)"