def test_init(self, member, dummy_am): am = NameManager(self.description, [dummy_am]) member.first_name = 'test' assert am.description == self.description assert am.questionable_attributes == [dummy_am] assert am.male_data == {} assert am.female_data == {} assert am.get_members_attribute(member) == 'test' member.gender = Gender.MALE assert am.get_members_attribute(member) == 'test'
def test_build_question_free_text(self, gender): bm = NameManager(self.description, ['last_name']) am = AttributeManager('last_name', ['first_name']) for i in range(4): member = Member(i, last_name='A', first_name='B', gender=gender) am.register_member(member) bm.register_member(member) with pytest.raises(RuntimeError, match='currently not hintable for last_name'): bm.build_question_with(am, multiple_choice=False) member = Member(100, first_name='100', last_name='200', gender=gender) am.register_member(member) bm.register_member(member) member, attr, correct = bm.build_question_with(am, multiple_choice=False) data = bm.female_data if member.gender == Gender.FEMALE else bm.male_data assert attr in data assert member in am.available_members assert member in bm.available_members assert attr == '100' assert correct == am.get_members_attribute(member)
def __init__(self) -> None: self._members: Dict[int, Member] = dict() self._members_lock = Lock() self.attribute_managers: Dict[str, AttributeManager] = { 'address': AttributeManager( 'address', list(self.ATTRIBUTE_MANAGERS.difference(['address'])) ), 'age': ChangingAttributeManager( 'age', list(self.ATTRIBUTE_MANAGERS.difference(['age', 'birthday'])) ), 'birthday': AttributeManager( 'birthday', list(self.ATTRIBUTE_MANAGERS.difference(['birthday', 'age'])) ), 'first_name': NameManager( 'first_name', list(self.ATTRIBUTE_MANAGERS.difference(['first_name', 'full_name'])) ), 'full_name': NameManager( 'full_name', list( self.ATTRIBUTE_MANAGERS.difference( ['full_name', 'first_name', 'last_name', 'nickname'] ) ), ), 'instruments': AttributeManager( 'instruments', list(self.ATTRIBUTE_MANAGERS.difference(['instruments'])) ), 'last_name': AttributeManager( 'last_name', list(self.ATTRIBUTE_MANAGERS.difference(['last_name', 'full_name'])) ), 'nickname': AttributeManager( 'nickname', list(self.ATTRIBUTE_MANAGERS.difference(['nickname', 'full_name'])) ), 'photo_file_id': PhotoManager( 'photo_file_id', list(self.ATTRIBUTE_MANAGERS.difference(['photo_file_id'])) ), 'joined': AttributeManager( 'joined', list(self.ATTRIBUTE_MANAGERS.difference(['joined', 'age', 'birthday'])) ), 'functions': AttributeManager( 'functions', list( self.ATTRIBUTE_MANAGERS.difference(['functions', 'age', 'birthday', 'joined']) ), ), }
def test_build_question(self, gender): bm = NameManager(self.description, ['last_name']) am = AttributeManager('last_name', ['first_name'], gendered_questions=True) for i in range(4): member = Member(i, last_name='A', first_name='B', gender=gender) am.register_member(member) bm.register_member(member) with pytest.raises(RuntimeError, match='currently not hintable for first_name'): am.build_question_with(bm) for i in range(100): member = Member( i + 10, first_name=random.choice(['1', '2', '3', '4', '5']), last_name=random.choice(['a', 'b', 'c', 'd', 'e']), gender=Gender.MALE, ) am.register_member(member) bm.register_member(member) for i in range(100): member = Member( i + 120, first_name=random.choice(['6', '7', '8', '9', '10']), last_name=random.choice(['f', 'g', 'h', 'i', 'j']), gender=Gender.FEMALE, ) am.register_member(member) bm.register_member(member) member, attr, opts, idx = am.build_question_with(bm) assert attr in am.data assert member in am.available_members assert member in bm.available_members for last_name in set(opts).difference({opts[idx]}): assert not any( am.get_members_attribute(m) == attr and bm.get_members_attribute(m) == last_name for m in bm.available_members) assert attr == 'A' assert len(set(opts)) == 4 for o in opts: if gender == Gender.MALE: assert o in ['1', '2', '3', '4', '5', 'B'] else: assert o in ['6', '7', '8', '9', '10', 'B']
def test_kick_member(self, member, gender): member.first_name = 'test' member.gender = gender am = NameManager(self.description, []) am.register_member(member) data = am.male_data if gender == Gender.MALE else am.female_data assert data == {'test': {member}} am.kick_member(member) assert data == {} am.kick_member(member) assert data == {}
def test_distinct_values_for_member_full_name(self, member, gender, other_gender): am = NameManager('full_name', []) bm = AttributeManager('instruments', [], gendered_questions=True) for i in range(10): am.register_member(Member(i, last_name='a', instruments=[Tuba()])) for g in [None, gender, other_gender]: assert (am.distinct_values_for_member( bm, Member(100, last_name='a', instruments=[Tuba()], gender=g)) == set()) assert (am.distinct_values_for_member( bm, Member(100, last_name='c', instruments=[Tuba()], gender=g)) == set()) am.register_member(Member(200, last_name='A', instruments=[Trumpet()])) assert (am.distinct_values_for_member( bm, Member(200, last_name='A', instruments=[Trumpet()], gender=gender)) == set()) assert am.distinct_values_for_member( bm, Member(200, last_name='A', instruments=[Trumpet()])) == {'a'}
def test_double_register(self, member): member.first_name = 'test' member.gender = Gender.MALE am = NameManager(self.description, []) am.register_member(member) with pytest.raises(RuntimeError, match='Member is already registered.'): am.register_member(Member(user_id=123456, first_name='test1'))
def test_register_male_member(self, member): member.first_name = 'test1' member.gender = Gender.MALE am = NameManager(self.description, []) am.register_member(member) assert am.female_data == {} assert am.male_data == {'test1': {member}} assert all(member is not m for m in am.male_data['test1']) member2 = Member(2, first_name='test2', gender=Gender.MALE) am.register_member(member2) assert am.female_data == {} assert am.male_data == {'test1': {member}, 'test2': {member2}} assert (member2 is not m for m in am.male_data['test2']) member3 = Member(4, first_name='test1', gender=Gender.MALE) am.register_member(member3) assert am.female_data == {} assert am.male_data == {'test1': {member, member3}, 'test2': {member2}} assert all(member3 is not m for m in am.male_data['test1'])
def test_unique_attributes_of(self, member): member.first_name = 'test' member.gender = Gender.MALE am = NameManager(self.description, []) am.register_member(member) am.register_member(Member(1, first_name='test', gender=Gender.MALE)) assert am.unique_attributes_of(member) == [] member = Member(2, first_name='test', gender=Gender.FEMALE) am.register_member(member) assert am.unique_attributes_of(member) == [] member = Member(3, first_name='test1', gender=Gender.FEMALE) am.register_member(member) assert am.unique_attributes_of(member) == ['test1']
def test_distinct_values_for_member(self, member, gender, other_gender): am = NameManager(self.description, []) bm = AttributeManager('last_name', [], gendered_questions=True) for i in range(10): am.register_member( Member(i, last_name='a', first_name='b', gender=gender)) for g in [None, gender, other_gender]: assert (am.distinct_values_for_member( bm, Member(100, last_name='a', first_name='b', gender=g)) == set()) assert (am.distinct_values_for_member( bm, Member(100, last_name='c', first_name='b', gender=g)) == set()) for i in range(5): am.register_member( Member(i + 10, last_name=str(i), first_name='b', gender=gender)) for g in [None, gender, other_gender]: assert (am.distinct_values_for_member( bm, Member(100, last_name='a', first_name='b', gender=g)) == set()) assert (am.distinct_values_for_member( bm, Member(100, last_name='c', first_name='b', gender=g)) == set()) for i in range(5): am.register_member( Member(i + 20, last_name='a', first_name=str(i), gender=gender)) assert (am.distinct_values_for_member( bm, Member(100, last_name='a', first_name='b', gender=gender)) == set()) assert am.distinct_values_for_member( bm, Member(100, last_name='c', first_name='b', gender=gender)) == {'0', '1', '2', '3', '4'} assert (am.distinct_values_for_member( bm, Member(100, last_name='a', first_name='b', gender=other_gender)) == set()) assert (am.distinct_values_for_member( bm, Member(100, last_name='c', first_name='b', gender=other_gender)) == set())
def test_distinct_values_for_member_no_attr(self, member): am = NameManager(self.description, []) bm = AttributeManager('last_name', []) assert am.distinct_values_for_member(bm, Member(1)) == set()
def test_register_member_without_gender(self, member): member.first_name = 'test' am = NameManager(self.description, []) am.register_member(member) assert am.male_data == {} assert am.female_data == {}
def test_register_member_without_attribute(self, member): am = NameManager(self.description, []) am.register_member(member) assert am.male_data == {} assert am.female_data == {}