def test_build_question(self, runs): am = AttributeManager(self.description, ['first_name']) bm = AttributeManager('first_name', []) for i in range(4): member = Member(i, first_name='A', last_name='B') 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(250): member = Member( i + 10, first_name=random.choice(['1', '2', '3', '4', '5']), last_name=random.choice(['a', 'b', 'c', 'd', 'e']), ) am.register_member(member) bm.register_member(member) member, attr, opts, idx = am.build_question_with( bm, exclude_members=[Member(0)]) assert member != Member(0) assert attr in am.data assert member in am.available_members assert member in bm.available_members for first_name in set(opts).difference({opts[idx]}): assert not any( am.get_members_attribute(m) == attr and bm.get_members_attribute(m) == first_name for m in bm.available_members) assert attr == 'B' assert len(set(opts)) == 4
def get(self): db = getUtility(IRelationalDatabase) cr = db.cursor() number = self.member.number if number: cr.execute("""SELECT id, number, name FROM members WHERE number = ?""", (number,)) else: cr.execute("""SELECT id, number, name FROM members""") rst = cr.fetchall() cr.close() members = [] for record in rst: id = record['id'] number = record['number'] name = record['name'] member = Member() member.id = id member.number = number member.name = name members.append(member) return members
def test_available_members(self, member): am = AttributeManager(self.description, []) for i in range(10): am.register_member(Member(user_id=i, last_name=str(i))) assert am.available_members == frozenset(Member(i) for i in range(10))
def test_draw_hint_member_free_text(self): am = AttributeManager(self.description, ['first_name']) bm = AttributeManager('first_name', []) for i in range(4): member = Member(i, first_name=str(i), last_name=str(i)) am.register_member(member) bm.register_member(member) member = Member(i + 10, first_name=str(i)) am.register_member(member) bm.register_member(member) member = Member(i + 20, last_name=str(i)) am.register_member(member) bm.register_member(member) member = Member(100, first_name='A', last_name='B') am.register_member(member) bm.register_member(member) member = am.draw_hint_member(bm, multiple_choice=False) assert member.last_name and member.first_name assert member in am.available_members assert member in bm.available_members assert am.is_hintable_with_member(bm, member, multiple_choice=False) assert len(am.unique_attributes_of(member)) >= 1
def get(self): db = getUtility(IRelationalDatabase) cr = db.cursor() number = self.member.number if number: cr.execute( """SELECT id, number, name FROM members WHERE number = ?""", (number, )) else: cr.execute("""SELECT id, number, name FROM members""") rst = cr.fetchall() cr.close() members = [] for record in rst: id = record['id'] number = record['number'] name = record['name'] member = Member() member.id = id member.number = number member.name = name members.append(member) return members
def test_questionable(self, orchestra): assert orchestra.questionable() == [] orchestra.register_member( Member(1, first_name='John', gender=Gender.MALE)) orchestra.register_member( Member(2, first_name='John', gender=Gender.MALE)) orchestra.register_member( Member(3, first_name='John', gender=Gender.MALE)) orchestra.register_member( Member(4, first_name='John', gender=Gender.MALE)) assert orchestra.questionable() == [] orchestra.register_member( Member(5, first_name='Mike', gender=Gender.MALE)) orchestra.register_member( Member(6, first_name='Brad', gender=Gender.MALE)) orchestra.register_member( Member(7, first_name='Marc', gender=Gender.MALE)) orchestra.register_member( Member(8, first_name='Joe', gender=Gender.MALE)) assert orchestra.questionable() == [] orchestra.register_member( Member(9, first_name='Dong', last_name='Silver', gender=Gender.MALE)) assert orchestra.questionable() == [('last_name', 'first_name')] assert orchestra.questionable(exclude_members=[Member(9)]) == []
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 test_build_question_free_text(self): am = AttributeManager(self.description, ['first_name']) bm = AttributeManager('first_name', []) for i in range(4): member = Member(i, first_name='A', last_name='B') am.register_member(member) bm.register_member(member) with pytest.raises(RuntimeError, match='currently not hintable for first_name'): am.build_question_with(bm, multiple_choice=False) member = Member(100, first_name='100', last_name='200') am.register_member(member) bm.register_member(member) member, attr, correct = am.build_question_with(bm, multiple_choice=False) assert attr in am.data assert member in am.available_members assert member in bm.available_members assert len(am.data[attr]) == 1 assert attr == '200' assert correct == bm.get_members_attribute(member)
def test_copy_backwards_compat(self): member = Member(1) del member._functions del member.joined new_member = member.copy() assert new_member.functions == [] assert new_member.joined is None
def on_add_clicked(self, *args): number = self.number.get_text() name = self.name.get_text() member = Member() member.number = number member.name = name self.add(member) self.list_store.append((member, number, name,))
def test_init(self): score = Score() assert score.answers == 0 assert score.correct == 0 score = Score(answers=2, correct=1, member=Member(123)) assert score.answers == 2 assert score.correct == 1 assert score.member == Member(123)
def test_address_and_coordinates(self): with pytest.raises(ValueError, match='Only address'): Member( self.user_id, address=self.address, latitude=self.latitude, longitude=self.longitude, ) with pytest.raises(ValueError, match='Either none'): Member(self.user_id, latitude=self.latitude)
def test_is_hintable_with_member(self): am = AttributeManager(self.description, []) bm = AttributeManager('first_name', []) for i in range(10): am.register_member(Member(i, first_name='a', last_name='b')) assert not bm.is_hintable_with_member( am, Member(100, first_name='a', last_name='b')) assert not bm.is_hintable_with_member( am, Member(100, first_name='c', last_name='b')) for i in range(5): am.register_member(Member(i + 10, first_name=str(i), last_name='b')) assert not bm.is_hintable_with_member( am, Member(100, first_name='a', last_name='b')) assert not bm.is_hintable_with_member( am, Member(100, first_name='c', last_name='b')) for i in range(5): am.register_member(Member(i + 20, first_name='a', last_name=str(i))) assert not bm.is_hintable_with_member( am, Member(100, first_name='a', last_name='b')) assert bm.is_hintable_with_member( am, Member(100, first_name='c', last_name='b'))
def test_distinct_values_for_member(self, member): am = AttributeManager(self.description, []) bm = AttributeManager('first_name', []) for i in range(10): am.register_member(Member(i, first_name='a', last_name='b')) assert (am.distinct_values_for_member( bm, Member(100, first_name='a', last_name='b')) == set()) assert (am.distinct_values_for_member( bm, Member(100, first_name='c', last_name='b')) == set()) for i in range(5): am.register_member(Member(i + 10, first_name=str(i), last_name='b')) assert (am.distinct_values_for_member( bm, Member(100, first_name='a', last_name='b')) == set()) assert (am.distinct_values_for_member( bm, Member(100, first_name='c', last_name='b')) == set()) for i in range(5): am.register_member(Member(i + 20, first_name='a', last_name=str(i))) assert (am.distinct_values_for_member( bm, Member(100, first_name='a', last_name='b')) == set()) assert am.distinct_values_for_member( bm, Member(100, first_name='c', last_name='b')) == { '0', '1', '2', '3', '4', }
def test_set_address_bad_response_1(self, monkeypatch): member = Member(1) def geocode_1(*args, **kwargs): return None monkeypatch.setattr(Photon, 'geocode', geocode_1) assert member.set_address(address=self.address) is None assert member.latitude is None assert member.longitude is None assert member._raw_address is None
def on_add_clicked(self, *args): number = self.number.get_text() name = self.name.get_text() member = Member() member.number = number member.name = name self.add(member) self.list_store.append(( member, number, name, ))
def test_set_address_bad_response_2(self, monkeypatch): member = Member(1) def geocode_2(*args, **kwargs): location = get_address_from_cache(args[1]) location.raw.clear() return location monkeypatch.setattr(Photon, 'geocode', geocode_2) assert 'Universitätsplatz' in member.set_address(address=self.address) assert member.latitude is not None assert member.longitude is not None assert member._raw_address is None
def test_equality(self, member): a = member b = Member(member.user_id, allow_contact_sharing=True) c = Member(123) d = Photon() assert a == b assert hash(a) == hash(b) assert a != c assert hash(a) != hash(c) assert a != d assert hash(a) != hash(d)
def test_scores(self, today): todays_score = score_orchestra(today).todays_score weeks_score = score_orchestra(today - dt.timedelta( days=today.weekday())).weeks_score months_score = score_orchestra(today - dt.timedelta(days=today.day - 1)).months_score years_score = score_orchestra(dt.date(today.year, 1, 1)).years_score overall_score = score_orchestra(today).overall_score for score in [todays_score, weeks_score, months_score, years_score]: assert score[0].member == Member(1) assert score[0] == Score(8, 4) assert score[1].member == Member(2) assert score[1] == Score(4, 2) assert score[2].member == Member(3) assert score[2] == Score(3, 1) assert score[3].member == Member(4) assert score[3] == Score(4, 1) assert overall_score[0].member == Member(2) assert overall_score[0] == Score(14, 12) assert overall_score[1].member == Member(3) assert overall_score[1] == Score(13, 11) assert overall_score[2].member == Member(4) assert overall_score[2] == Score(14, 11) assert overall_score[3].member == Member(1) assert overall_score[3] == Score(18, 14)
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_questionable_multiple_choice_first_name(self, orchestra): assert orchestra.questionable() == [] orchestra.register_member( Member(1, first_name='John', last_name='test')) orchestra.register_member( Member(5, first_name='Mike', last_name='test')) orchestra.register_member( Member(6, first_name='Brad', last_name='test')) orchestra.register_member( Member(7, first_name='Marc', last_name='test')) orchestra.register_member(Member(8, first_name='Joe', last_name='test')) assert orchestra.questionable() == [] assert orchestra.questionable(multiple_choice=False) == [('first_name', 'last_name')]
def test_truncate_long_answers(self, bot, chat_id, empty_orchestra, monkeypatch, empty_member): poll_message = None def send_poll(*args, **kwargs): nonlocal poll_message poll_message = fake_poll() poll_message.poll.options = [PollOption(o, 0) for o in kwargs['options']] return poll_message monkeypatch.setattr(bot, 'send_poll', send_poll) for i in range(10): empty_orchestra.register_member(Member(i, nickname=str(i), last_name=150 * str(i))) empty_orchestra.register_member(empty_member) questioner = Questioner( user_id=int(chat_id), orchestra=empty_orchestra, hint_attributes=['nickname'], question_attributes=['last_name'], number_of_questions=42, bot=bot, multiple_choice=True, ) questioner.ask_question() assert questioner.number_of_questions_asked == 1 for option in poll_message.poll.options: assert option.text[-4:] == ' ...'
def test_repr(self, member): member.first_name = 'first' member.nickname = 'nick' member.last_name = 'last' assert repr(member) == 'Member(first "nick" last)' member = Member(1) assert repr(member) == 'Member(1)'
def test_init_errors(self): member = Member(1) invalid_poll = Poll(123, 'question', ['Opt1', 'Opt2'], 0, False, False, Poll.REGULAR, True) with pytest.raises(ValueError, match='must be a quiz poll'): Question(member, Question.AGE, poll=invalid_poll) with pytest.raises(ValueError, match='poll if and only if'): Question(member, Question.AGE, multiple_choice=False, poll=self.poll) with pytest.raises(ValueError, match='poll if and only if'): Question(member, Question.AGE) with pytest.raises(ValueError, match='Attribute not supported'): Question(member, 'attribute') with pytest.raises(ValueError, match='Photos are'): Question(member, Question.PHOTO, multiple_choice=False) for attr in Question.SUPPORTED_ATTRIBUTES: if attr != Question.PHOTO: with pytest.raises(ValueError, match='the required attribute'): Question(member, attr, multiple_choice=False)
def book_issue(self, member_number, book_barcode): member = Member() member.number = member_number memberdboperation = getAdapter(member, IDbOperation) member = memberdboperation.get()[0] book = Book() book.number = book_barcode bookdboperation = getAdapter(book, IDbOperation) book = bookdboperation.get()[0] circulation = Circulation() circulation.member = member circulation.book = book circulationdboperation = getAdapter(circulation, IDbOperation) circulationdboperation.add()
def test_draw_question_attributes_error(self): am = AttributeManager(self.description, []) bm = AttributeManager(self.description, []) with pytest.raises( RuntimeError, match=f'Given member has no attribute {self.description}'): am.draw_question_attributes(bm, Member(1))
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 orchestra(members, bot, chat_id, skip=None): if skip is None: skip = [] orchestra_ = Orchestra() for i in range(1, members): gender = random.choice([Gender.MALE, Gender.FEMALE]) phone_number_ = phone_number() if 'phone_number' not in skip else None first_name_ = first_name(gender) if 'first_name' not in skip else None last_name_ = last_name() if 'last_name' not in skip else None nickname_ = nickname() if 'nickname' not in skip else None date_of_birth_ = date_of_birth( ) if 'date_of_birth' not in skip else None instruments_ = instrument() if 'instruments' not in skip else None address_ = address() if 'address' not in skip else None photo_file_id_ = (PhotoFileID.photo_file_id(bot, chat_id) if 'photo_file_id' not in skip else None) orchestra_.register_member( Member( user_id=i, phone_number=phone_number_, first_name=first_name_, last_name=last_name_, nickname=nickname_, gender=gender, date_of_birth=date_of_birth_, instruments=instruments_, address=address_, photo_file_id=photo_file_id_, )) return orchestra_
def test_comparison(self): score_1 = Score(4, 2) score_2 = Score(8, 4) member = Member(123) assert score_1 <= score_2 assert score_2 >= score_1 assert score_1 < score_2 assert score_2 > score_1 assert score_1 != score_2 score_1 = Score(3, 1) score_2 = Score(4, 1) assert score_1 >= score_2 assert score_2 <= score_1 assert score_1 > score_2 assert score_2 < score_1 assert score_1 != score_2 assert score_1 >= score_1 assert score_1 <= score_1 assert not score_1 > score_1 assert not score_1 < score_1 assert score_1 == score_1 assert not (score_1 < member) assert not (score_1 <= member) assert not (score_1 > member) assert not (score_1 >= member)
def test_is_hintable_with_member_free_text(self): am = AttributeManager(self.description, []) bm = AttributeManager('first_name', []) for i in range(10): am.register_member(Member(i, first_name='a', last_name='b')) bm.register_member(Member(i, first_name='a', last_name='b')) assert not bm.is_hintable_with_member( am, Member(100, first_name='a', last_name='b'), multiple_choice=False) member = Member(100, first_name='c', last_name='b') am.register_member(member) bm.register_member(member) assert bm.is_hintable_with_member(am, member, multiple_choice=False)
def test_double_register(self, member): member.last_name = 'test' am = AttributeManager(self.description, []) am.register_member(member) with pytest.raises(RuntimeError, match='Member is already registered.'): am.register_member(Member(user_id=123456, last_name='test1'))
def score_orchestra_anonymous(date): o = Orchestra() offset = dt.timedelta(weeks=100) m_1 = Member(1) m_1.user_score.add_to_score(8, 4, date) m_1.user_score.add_to_score(10, 10, date - offset) o.register_member(m_1) return o