def test_notice_collection_count_by_organization(session): collection = GazetteNoticeCollection(session) assert collection.count_by_organization() == [] organizations = OrganizationCollection(session) organizations.add_root(name='1', title='A') organizations.add_root(name='2', title='B') organizations.add_root(name='3', title='C') for organization, count in (('1', 2), ('2', 4), ('3', 10)): for x in range(count): collection.add(title='', text='', organization_id=organization, category_id='', issues=['2017-{}'.format(y) for y in range(x)], user=None) assert collection.count_by_organization() == [ ('A', 1), ('B', 6), ('C', 45), ] assert collection.count_by_organization() == \ collection.for_state('drafted').count_by_organization() collection.issues = ['2017-1', '2017-4'] assert collection.count_by_organization() == [('B', 2), ('C', 13)]
def test_notice_collection_used_issues(session): collection = GazetteNoticeCollection(session) issues = IssueCollection(session) a = issues.add(name='2017-1', number=1, date=date(2017, 1, 2), deadline=standardize_date(datetime(2017, 1, 1, 12, 0), 'UTC')) b = issues.add(name='2017-2', number=2, date=date(2017, 2, 2), deadline=standardize_date(datetime(2017, 2, 1, 12, 0), 'UTC')) c = issues.add(name='2017-3', number=3, date=date(2017, 3, 2), deadline=standardize_date(datetime(2017, 3, 1, 12, 0), 'UTC')) for issues in (('3', '2'), ('1', ), ('1', '3')): collection.add(title='', text='', organization_id='', category_id='', issues=[f'2017-{issue}' for issue in issues], user=None) assert collection.used_issues == [a, b, c]
def test_notice_collection_own_user_id(session): users = UserCollection(session) user_a = users.add(username='******', password='******', role='admin') user_b = users.add(username='******', password='******', role='admin') user_c = users.add(username='******', password='******', role='admin') collection = GazetteNoticeCollection(session) for title, user, annotators in ( ('A', user_a, []), ('B', user_b, [user_a]), ('C', user_c, [user_a, user_b]), ): notice = collection.add(title=title, text='Text', organization_id='100', category_id='11', issues=['2017-46'], user=user) for annotator in annotators: notice.changes.append( GazetteNoticeChange( channel_id=str(notice.id), owner=str(annotator.id), )) assert collection.query().count() == 3 collection.own_user_id = str(user_a.id) assert collection.query().count() == 3 collection.own_user_id = str(user_b.id) assert collection.query().count() == 2 collection.own_user_id = str(user_c.id) assert collection.query().count() == 1
def test_notice_collection_used_categories(session): collection = GazetteNoticeCollection(session) categories = CategoryCollection(session) a = categories.add_root(name='1', title='A') b = categories.add_root(name='2', title='B') c = categories.add_root(name='3', title='C') for category, count in (('1', 2), ('2', 4), ('3', 1)): for x in range(count): collection.add(title='', text='', organization_id=None, category_id=category, issues=['2017-{}'.format(y) for y in range(x)], user=None) assert collection.used_categories == [a, b, c]
def test_notice_collection(session, organizations, categories, issues): user = UserCollection(session).add(username='******', password='******', role='admin') collection = GazetteNoticeCollection(session) collection.add(title='Notice A', text='An <strong>important</strong> Notice!', organization_id='100', category_id='11', issues=['2017-46', '2017-47'], user=user) collection.add(title='Notice B', text='Another Notice', organization_id='200', category_id='13', issues={'2017-47', '2017-48'}, user=user) notice = collection.for_term('Notice A').query().one() assert notice.title == 'Notice A' assert notice.text == 'An <strong>important</strong> Notice!' assert notice.organization_id == '100' assert notice.organization == 'State Chancellery' assert notice.category_id == '11' assert notice.category == 'Education' assert notice.issues == {'2017-46': None, '2017-47': None} assert notice.first_issue == standardize_date(datetime(2017, 11, 17), 'UTC') assert notice.user == user assert notice.changes.one().event == 'created' assert notice.changes.one().user == user notice = collection.for_term('Notice B').query().one() assert notice.title == 'Notice B' assert notice.text == 'Another Notice' assert notice.organization_id == '200' assert notice.organization == 'Civic Community' assert notice.category_id == '13' assert notice.category == 'Commercial Register' assert notice.issues == {'2017-47': None, '2017-48': None} assert notice.first_issue == standardize_date(datetime(2017, 11, 24), 'UTC') assert notice.user == user assert notice.changes.one().event == 'created' assert notice.changes.one().user == user
def test_notice_collection_count_by_group(session): collection = GazetteNoticeCollection(session) assert collection.count_by_group() == [] groups = UserGroupCollection(session) group_a = groups.add(name='A') group_b = groups.add(name='B') groups.add(name='C') users = UserCollection(session) user_a = users.add('*****@*****.**', 'pw', 'editor', group=group_a) user_b = users.add('*****@*****.**', 'pw', 'editor', group=group_a) user_c = users.add('*****@*****.**', 'pw', 'admin') user_d = users.add('*****@*****.**', 'pw', 'publisher') user_e = users.add('*****@*****.**', 'pw', 'publisher', group=group_b) user_f = users.add('*****@*****.**', 'pw', 'publisher') user_g = users.add('*****@*****.**', 'pw', 'publisher') user_h = users.add('*****@*****.**', 'pw', 'publisher') for user, count in ( (user_a, 2), (user_b, 4), (user_c, 1), (user_d, 7), (user_e, 2), (user_f, 3), (user_g, 6), (user_h, 2), ): for x in range(count): collection.add(title='', text='', organization_id='', category_id='', issues=['2017-{}'.format(y) for y in range(x)], user=user) assert collection.count_by_group() == [('A', 7), ('B', 1)] assert collection.count_by_group() == \ collection.for_state('drafted').count_by_group() collection.issues = ['2017-2', '2017-4'] assert collection.count_by_group() == [('A', 1)]
def test_notice_collection_for_categories(session): collection = GazetteNoticeCollection(session) categories = CategoryCollection(session) categories.add_root(name='1', title='A') categories.add_root(name='2', title='B') categories.add_root(name='3', title='C') for category, count in (('1', 2), ('2', 4), ('3', 1)): for x in range(count): collection.add(title='', text='', organization_id=None, category_id=category, issues=['2017-{}'.format(y) for y in range(x)], user=None) assert collection.for_categories(['1']).query().count() == 2 assert collection.for_categories(['2']).query().count() == 4 assert collection.for_categories(['3']).query().count() == 1 assert collection.for_categories(['1', '3']).query().count() == 3
def test_notice_collection_count_by_category(session): collection = GazetteNoticeCollection(session) assert collection.count_by_category() == [] categories = CategoryCollection(session) categories.add_root(name='1', title='A') categories.add_root(name='2', title='B') categories.add_root(name='3', title='C') for category, count in (('1', 2), ('2', 4), ('3', 1)): for x in range(count): collection.add(title='', text='', organization_id=None, category_id=category, issues=['2017-{}'.format(y) for y in range(x)], user=None) assert collection.count_by_category() == [('A', 1), ('B', 6)] assert collection.count_by_category() == \ collection.for_state('drafted').count_by_category() collection.issues = ['2017-0', '2017-2'] assert collection.count_by_category() == [('A', 1), ('B', 4)]
def test_notice_collection_query_deleted_user(session): groups = UserGroupCollection(session) group_a = groups.add(name="Group A") group_b = groups.add(name="Group B") users = UserCollection(session) user_a = users.add(realname="User A", username='******', password='******', role='admin', group=group_a) user_b = users.add(realname="User B", username='******', password='******', role='admin', group=group_b) notices = GazetteNoticeCollection(session) notice_a = notices.add(title='A', text='Text', organization_id='100', category_id='11', issues=['2017-46'], user=user_a) notice_b = notices.add(title='B', text='Text', organization_id='100', category_id='11', issues=['2017-46'], user=user_b) assert notices.query().count() == 2 assert notice_a.user is not None assert notice_b.user is not None assert notice_a.group is not None assert notice_b.group is not None assert notices.for_term("User A").query().one() == notice_a assert notices.for_term("User B").query().one() == notice_b assert notices.for_term("Group A").query().one() == notice_a assert notices.for_term("Group B").query().one() == notice_b users.delete(user_a.username) users.delete(user_b.username) groups.delete(group_a) groups.delete(group_b) commit() assert users.query().count() == 0 assert groups.query().count() == 0 notice_a = notices.query().filter(GazetteNotice.title == 'A').one() notice_b = notices.query().filter(GazetteNotice.title == 'B').one() assert notice_a.user is None assert notice_b.user is None assert notice_a.group is None assert notice_b.group is None assert notices.query().count() == 2 assert notices.for_term("User A").query().one() == notice_a assert notices.for_term("User B").query().one() == notice_b assert notices.for_term("Group A").query().one() == notice_a assert notices.for_term("Group B").query().one() == notice_b
def test_issue_form(session): request = DummyRequest(session, DummyPrincipal()) # Test apply / update # ... unused issues = IssueCollection(session) issue = issues.add(name='2018-1', number=1, date=date(2018, 1, 5), deadline=standardize_date(datetime(2018, 1, 4, 12, 0), 'UTC')) form = IssueForm() form.request = request form.on_request() form.apply_model(issue) assert form.number.data == 1 assert form.name.data == '2018-1' assert form.date_.data == date(2018, 1, 5) assert form.deadline.data == datetime(2018, 1, 4, 13, 0) form.number.data = 2 form.date_.data = date(2019, 1, 5) form.deadline.data = datetime(2019, 1, 4, 13, 0) form.update_model(issue) assert issue.number == 2 assert issue.name == '2019-2' assert issue.date == date(2019, 1, 5) assert issue.deadline == standardize_date(datetime(2019, 1, 4, 12, 0), 'UTC') # used issue = issues.add(name='2019-3', number=3, date=date(2019, 2, 5), deadline=standardize_date(datetime(2019, 2, 4, 12, 0), 'UTC')) notices = GazetteNoticeCollection(session) notices.add('title', 'text', '', '', None, ['2019-3']) form.apply_model(issue) assert form.number.render_kw == {'readonly': True} # Test validation # ... empty values form = IssueForm() form.request = request assert not form.validate() # ... new model form = IssueForm( DummyPostData({ 'number': '3', 'date_': '2019-03-05', 'deadline': '2019-03-04T12:00' })) form.request = request assert not form.validate() assert 'This value already exists.' in form.errors['name'] form = IssueForm( DummyPostData({ 'number': '5', 'date_': '2019-03-05', 'deadline': '2019-03-04T12:00' })) form.request = request assert form.validate() form = IssueForm( DummyPostData({ 'number': '3', 'date_': '2018-03-05', 'deadline': '2019-03-04T12:00' })) form.request = request assert form.validate() # ... existing model form = IssueForm( DummyPostData({ 'number': '2', 'date_': '2019-03-05', 'deadline': '2019-03-04T12:00' })) form.model = issues.query().filter_by(number='3').one() form.request = request assert not form.validate() assert 'This value already exists.' in form.errors['name'] assert 'This value is in use.' in form.errors['name'] form = IssueForm( DummyPostData({ 'number': '5', 'date_': '2019-03-05', 'deadline': '2019-03-04T12:00' })) form.model = issues.query().filter_by(number='2').one() form.request = request assert form.validate() # Datetimepicker forma form = IssueForm( DummyPostData({ 'number': '1', 'date_': '2020-01-01', 'deadline': '2011-01-01 12:00' })) form.request = request assert form.validate()