<commit_before>import unittest2
from nose.tools import *

from gutter.client.operators.comparable import *
from gutter.client.operators.identity import *
from gutter.client.operators.misc import *
from gutter.client.models import Switch, Condition, Manager
from gutter.client import arguments
from gutter.client.arguments.variables import Value, Boolean, String
from gutter.client import signals

from exam.decorators import fixture, before, after
from exam.cases import Exam


class User(object):

    def __init__(self, name, age, location='San Francisco', married=False):
        self.name = name
        self.age = age
        self.location = location
        self.married = married


class UserArguments(arguments.Container):

    COMPATIBLE_TYPE = User

    name = arguments.String(lambda self: self.input.name)
    age = arguments.Value(lambda self: self.input.age)
    location = arguments.String(lambda self: self.input.location)
    married = arguments.Boolean(lambda self: self.input.married)


class TestIntegration(Exam, unittest2.TestCase):

    class Callback(object):

        def __init__(self):
            self.register_calls = []
            self.unregister_calls = []
            self.update_calls = []

        def switch_added(self, switch):
            self.register_calls.append(switch)

        def switch_removed(self, switch):
            self.unregister_calls.append(switch)

        def switch_updated(self, switch):
            self.update_calls.append((switch, switch.changes))

    class inputs(object):

        def __init__(self, manager, *inputs):
            self.manager = manager
            self.manager.input(*inputs)

        def __enter__(self):
            return self

        def active(self, *args, **kwargs):
            return self.manager.active(*args, **kwargs)

        def __exit__(self, type, value, traceback):
            self.manager.flush()

    callback = fixture(Callback)

    @fixture
    def manager(self):
        return Manager(storage=dict())

    @before
    def build_objects(self):
        self.setup_inputs()
        self.setup_conditions()
        self.setup_switches()

    @after
    def reset_objects(self):
        signals.switch_registered.reset()
        signals.switch_unregistered.reset()
        signals.switch_updated.reset()

    def setup_inputs(self):
        self.jeff = User('jeff', 21)
        self.frank = User('frank', 10, location="Seattle")
        self.larry = User('bill', 70, location="Yakima", married=True)
        self.timmy = User('timmy', 12)
        self.steve = User('timmy', 19)

    def setup_conditions(self):
        self.age_65_and_up = Condition(UserArguments, 'age', MoreThanOrEqualTo(lower_limit=65))
        self.age_under_18 = Condition(UserArguments, 'age', LessThan(upper_limit=18))
        self.age_not_under_18 = Condition(UserArguments, 'age', LessThan(upper_limit=18), negative=True)
        self.age_21_plus = Condition(UserArguments, 'age', MoreThanOrEqualTo(lower_limit=21))
        self.age_between_13_and_18 = Condition(UserArguments, 'age', Between(lower_limit=13, upper_limit=18))

        self.in_sf = Condition(UserArguments, 'location', Equals(value='San Francisco'))
        self.has_location = Condition(UserArguments, 'location', Truthy())

        self.three_quarters_married = Condition(UserArguments, 'married', Percent(percentage=75))
        self.ten_percent = Condition(UserArguments, 'name', Percent(percentage=10))
        self.upper_50_percent = Condition(UserArguments, 'name', PercentRange(lower_limit=50, upper_limit=100))

    def setup_switches(self):
        self.add_switch('can drink', condition=self.age_21_plus)
        self.add_switch('can drink in europe', condition=self.age_21_plus, state=Switch.states.GLOBAL)
        self.add_switch('can drink:wine', condition=self.in_sf, concent=True)
        self.add_switch('retired', condition=self.age_65_and_up)
        self.add_switch('can vote', condition=self.age_not_under_18)
        self.add_switch('teenager', condition=self.age_between_13_and_18)
        self.add_switch('SF resident', condition=self.in_sf)
        self.add_switch('teen or in SF', self.age_between_13_and_18, self.in_sf)
        self.add_switch('teen and in SF', self.age_between_13_and_18,
                        self.has_location, compounded=True)
        self.add_switch('10 percent', self.ten_percent)
        self.add_switch('Upper 50 percent', self.upper_50_percent)

    def add_switch(self, name, condition=None, *conditions, **kwargs):
        switch = Switch(name, compounded=kwargs.get('compounded', False))
        switch.state = kwargs.get('state', Switch.states.SELECTIVE)
        conditions = list(conditions)

        if condition:
            conditions.append(condition)

        [switch.conditions.append(c) for c in conditions]
        kwargs.get('manager', self.manager).register(switch)
        return switch

    def test_basic_switches_work_with_conditions(self):
        with self.inputs(self.manager, self.larry) as context:
            ok_(context.active('can drink') is True)
            ok_(context.active('can drink in europe') is True)
            ok_(context.active('can vote') is True)
            ok_(context.active('SF resident') is False)
            ok_(context.active('retired') is True)
            ok_(context.active('10 percent') is False)
            ok_(context.active('Upper 50 percent') is True)

        with self.inputs(self.manager, self.jeff) as context:
            ok_(context.active('can drink') is True)
            ok_(context.active('can drink in europe') is True)
            ok_(context.active('can vote') is True)
            ok_(context.active('SF resident') is True)
            ok_(context.active('teenager') is False)
            ok_(context.active('teen or in SF') is True)
            ok_(context.active('teen and in SF') is False)<commit_msg>Add print to debug travis issue<commit_after>import unittest2
from nose.tools import *

from gutter.client.operators.comparable import *
from gutter.client.operators.identity import *
from gutter.client.operators.misc import *
from gutter.client.models import Switch, Condition, Manager
from gutter.client import arguments
from gutter.client.arguments.variables import Value, Boolean, String
from gutter.client import signals

from exam.decorators import fixture, before, after
from exam.cases import Exam


class User(object):

    def __init__(self, name, age, location='San Francisco', married=False):
        self.name = name
        self.age = age
        self.location = location
        self.married = married


class UserArguments(arguments.Container):

    COMPATIBLE_TYPE = User

    name = arguments.String(lambda self: self.input.name)
    age = arguments.Value(lambda self: self.input.age)
    location = arguments.String(lambda self: self.input.location)
    married = arguments.Boolean(lambda self: self.input.married)


class TestIntegration(Exam, unittest2.TestCase):

    class Callback(object):

        def __init__(self):
            self.register_calls = []
            self.unregister_calls = []
            self.update_calls = []

        def switch_added(self, switch):
            self.register_calls.append(switch)

        def switch_removed(self, switch):
            self.unregister_calls.append(switch)

        def switch_updated(self, switch):
            self.update_calls.append((switch, switch.changes))

    class inputs(object):

        def __init__(self, manager, *inputs):
            self.manager = manager
            self.manager.input(*inputs)

        def __enter__(self):
            return self

        def active(self, *args, **kwargs):
            return self.manager.active(*args, **kwargs)

        def __exit__(self, type, value, traceback):
            self.manager.flush()

    callback = fixture(Callback)

    @fixture
    def manager(self):
        return Manager(storage=dict())

    @before
    def build_objects(self):
        self.setup_inputs()
        self.setup_conditions()
        self.setup_switches()

    @after
    def reset_objects(self):
        signals.switch_registered.reset()
        signals.switch_unregistered.reset()
        signals.switch_updated.reset()

    def setup_inputs(self):
        self.jeff = User('jeff', 21)
        self.frank = User('frank', 10, location="Seattle")
        self.larry = User('bill', 70, location="Yakima", married=True)
        self.timmy = User('timmy', 12)
        self.steve = User('timmy', 19)

    def setup_conditions(self):
        self.age_65_and_up = Condition(UserArguments, 'age', MoreThanOrEqualTo(lower_limit=65))
        self.age_under_18 = Condition(UserArguments, 'age', LessThan(upper_limit=18))
        self.age_not_under_18 = Condition(UserArguments, 'age', LessThan(upper_limit=18), negative=True)
        self.age_21_plus = Condition(UserArguments, 'age', MoreThanOrEqualTo(lower_limit=21))
        self.age_between_13_and_18 = Condition(UserArguments, 'age', Between(lower_limit=13, upper_limit=18))

        self.in_sf = Condition(UserArguments, 'location', Equals(value='San Francisco'))
        self.has_location = Condition(UserArguments, 'location', Truthy())

        self.three_quarters_married = Condition(UserArguments, 'married', Percent(percentage=75))
        self.ten_percent = Condition(UserArguments, 'name', Percent(percentage=10))
        self.upper_50_percent = Condition(UserArguments, 'name', PercentRange(lower_limit=50, upper_limit=100))

    def setup_switches(self):
        self.add_switch('can drink', condition=self.age_21_plus)
        self.add_switch('can drink in europe', condition=self.age_21_plus, state=Switch.states.GLOBAL)
        self.add_switch('can drink:wine', condition=self.in_sf, concent=True)
        self.add_switch('retired', condition=self.age_65_and_up)
        self.add_switch('can vote', condition=self.age_not_under_18)
        self.add_switch('teenager', condition=self.age_between_13_and_18)
        self.add_switch('SF resident', condition=self.in_sf)
        self.add_switch('teen or in SF', self.age_between_13_and_18, self.in_sf)
        self.add_switch('teen and in SF', self.age_between_13_and_18,
                        self.has_location, compounded=True)
        self.add_switch('10 percent', self.ten_percent)
        self.add_switch('Upper 50 percent', self.upper_50_percent)

    def add_switch(self, name, condition=None, *conditions, **kwargs):
        switch = Switch(name, compounded=kwargs.get('compounded', False))
        switch.state = kwargs.get('state', Switch.states.SELECTIVE)
        conditions = list(conditions)

        if condition:
            conditions.append(condition)

        [switch.conditions.append(c) for c in conditions]
        kwargs.get('manager', self.manager).register(switch)
        return switch

    def test_basic_switches_work_with_conditions(self):
        with self.inputs(self.manager, self.larry) as context:
            ok_(context.active('can drink') is True)
            ok_(context.active('can drink in europe') is True)
            ok_(context.active('can vote') is True)
            ok_(context.active('SF resident') is False)
            ok_(context.active('retired') is True)
            ok_(context.active('10 percent') is False)
            print context.active('Upper 50 percent')
            ok_(context.active('Upper 50 percent') is True)

        with self.inputs(self.manager, self.jeff) as context:
            ok_(context.active('can drink') is True)
            ok_(context.active('can drink in europe') is True)
            ok_(context.active('can vote') is True)
            ok_(context.active('SF resident') is True)
            ok_(context.active('teenager') is False)
            ok_(context.active('teen or in SF') is True)
            ok_(context.active('teen and in SF') is False)
{'fraction_non_alphanumeric': 0.112351001775298, 'fraction_numerical': 0.013187927973624143, 'mean_word_length': 3.137810423224904, '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': '5185414', 'n_tokens_mistral': 3508, 'n_tokens_neox': 3360, 'n_words': 732}