Exemplo n.º 1
0
 def test__get_top_validated_events__time_constraints(self):
     soon = datetime.now() + timedelta(seconds=1)
     shortly_before = datetime.now() - timedelta(hours=1)
     fixture = """
         - User:
           - tg_id: 1
         - Event:
           - creator_id: 1
             name: Event 1
             description: Valid time constraints
             start_date: {0}
             end_date: {0}
             is_blocked: False
             completed_validations: 1
             quality_rating: 1
             interest_rating: 1
           - creator_id: 1
             name: Event 2
             description: Start date is today, but time is in past
             start_date: {1}
             end_date: {1}
             is_blocked: False
             completed_validations: 1
             quality_rating: 1
             interest_rating: 1
         """.format(soon, shortly_before)
     sqla_yaml_fixtures.load(Base, db.session, fixture)
     events = Event.get_top_validated_events(user=User.by(tg_id=1))
     self.assertTrue(
         len(events) == 1,
         "User should get validated events with valid time constraints")
     self.assertTrue(events[0].name == 'Event 1', "Wrong event is picked")
Exemplo n.º 2
0
 def test__get_not_finished_validation__false_if_there_is_one_for_yesterday(
         self):
     fixture = """
         - User:
           - tg_id: 1
           - tg_id: 2
         - Event:
           - creator_id: 2
             name: Event 1
             description: short 1
             start_date: 2020-10-25 14:00:00
             end_date: 2020-10-25 14:00:00
         - Validation:
           - validator_id: 1
             event_id: 1
             call_to_violence: False
             quality_rating: 5
             created: {}
         """.format(datetime.now() - timedelta(1))
     sqla_yaml_fixtures.load(Base, db.session, fixture)
     user = db.session.query(User).first()
     self.assertFalse(
         user.get_not_finished_validation(),
         "User should not have not finished validation if there is one from yesterday"
     )
Exemplo n.º 3
0
 def test__get_top_validated_events__returns_not_blocked_events(self):
     soon = datetime.now() + timedelta(seconds=1)
     fixture = """
         - User:
           - tg_id: 1
         - Event:
           - creator_id: 1
             name: Event 1
             description: Not blocked
             start_date: {0}
             end_date: {0}
             is_blocked: False
             completed_validations: 1
             quality_rating: 1
             interest_rating: 1
           - creator_id: 1
             name: Event 2
             description: Blocked
             start_date: {0}
             end_date: {0}
             is_blocked: True
             completed_validations: 1
             quality_rating: 1
             interest_rating: 1
         """.format(soon)
     sqla_yaml_fixtures.load(Base, db.session, fixture)
     events = Event.get_top_validated_events(user=User.by(tg_id=1))
     self.assertTrue(len(events) == 1, "User should get not blocked events")
     self.assertTrue(events[0].name == 'Event 1', "Wrong event is picked")
Exemplo n.º 4
0
 def test__can_validate__false_if_already_validated_today(self):
     fixture = """
         - User:
           - tg_id: 1
           - tg_id: 2
         - Event:
           - creator_id: 2
             name: Event 1
             description: short 1
             start_date: 2020-10-25 14:00:00
             end_date: 2020-10-25 14:00:00
           - creator_id: 2
             name: Event 2
             description: short 2
             start_date: 2020-10-25 14:00:00
             end_date: 2020-10-25 14:00:00
         - Validation:
           - validator_id: 1
             event_id: 1
             quality_rating: 5
             interest_rating: 5
             created: {}
           - validator_id: 1
             event_id: 2
             call_to_violence: True
         """.format(datetime.now() - timedelta(1))
     sqla_yaml_fixtures.load(Base, db.session, fixture)
     user = db.session.query(User).filter(User.tg_id == 1).first()
     self.assertFalse(
         user.can_validate_today,
         "User should not be able to validate if there were a validation done today"
     )
Exemplo n.º 5
0
def test_yaml_root_sequence(session):
    fixture = """
User:
  - username: deedee
    email: [email protected]
"""
    with pytest.raises(Exception) as exc_info:
        sqla_yaml_fixtures.load(BaseModel, session, fixture)
    assert 'Top level YAML' in str(exc_info)
    assert 'sequence' in str(exc_info)
Exemplo n.º 6
0
def test_mapper_must_contain_list(session):
    fixture = """
- User:
    username: deedee
    email: [email protected]
"""
    with pytest.raises(Exception) as exc_info:
        sqla_yaml_fixtures.load(BaseModel, session, fixture)
    assert 'must contain a sequence(list)' in str(exc_info)
    assert 'User' in str(exc_info)
Exemplo n.º 7
0
def test_insert_invalid(session):
    fixture = """
- User:
  - username: deedee
    email: [email protected]
    color_no: blue
"""
    with pytest.raises(Exception) as exc_info:
        sqla_yaml_fixtures.load(BaseModel, session, fixture)
    assert 'User' in str(exc_info)
    assert 'color_no' in str(exc_info)
Exemplo n.º 8
0
def test_insert_nested_back_populate(session):
    fixture = """
- User:
  - username: joey
    email: [email protected]
    profile2:
      name: Jeffrey
"""
    sqla_yaml_fixtures.load(BaseModel, session, fixture)
    users = session.query(User).all()
    assert len(users) == 1
    assert users[0].profile2.name == 'Jeffrey'
Exemplo n.º 9
0
def test_insert_nested_NO_back_populate(session):
    fixture = """
- Profile3:
  - name: Jeffrey
    user:
      username: joey
      email: [email protected]
"""
    sqla_yaml_fixtures.load(BaseModel, session, fixture)
    profiles = session.query(Profile3).all()
    assert len(profiles) == 1
    assert profiles[0].name == 'Jeffrey'
    assert profiles[0].user.username == 'joey'
Exemplo n.º 10
0
def test_init_param(session):
    fixture = """
- User:
  - __key__: joey
    username: joey
    email: [email protected]
    profile:
      nickname: Joey
"""
    sqla_yaml_fixtures.load(BaseModel, session, fixture)
    users = session.query(User).all()
    assert len(users) == 1
    assert users[0].profile.name == 'Joey'
Exemplo n.º 11
0
def test_yaml_root_item_single_element(session):
    fixture = """
- User:
  - username: deedee
    email: [email protected]
  Group:
  - name: Ramones
"""
    with pytest.raises(Exception) as exc_info:
        sqla_yaml_fixtures.load(BaseModel, session, fixture)
    assert 'Sequence item must contain only one mapper' in str(exc_info)
    assert 'Group' in str(exc_info)
    assert 'User' in str(exc_info)
Exemplo n.º 12
0
def test_insert_simple(session):
    fixture = """
- User:
  - username: deedee
    email: [email protected]
  - username: joey
    email: [email protected]
"""
    sqla_yaml_fixtures.load(BaseModel, session, fixture)
    users = session.query(User).all()
    assert len(users) == 2
    assert users[0].username == 'deedee'
    assert users[1].username == 'joey'
Exemplo n.º 13
0
def test_custom_loader(session):
    fixture = """
- 'Person:create':
  - username: joey
  - username: deedee
"""
    sqla_yaml_fixtures.load(BaseModel, session, fixture)
    users = session.query(Person).order_by(Person.username).all()
    assert len(users) == 2
    assert users[0].username == 'deedee'
    assert users[0].email == '*****@*****.**'
    assert users[1].username == 'joey'
    assert users[1].email == '*****@*****.**'
Exemplo n.º 14
0
def test_insert_relation(session):
    fixture = """
- User:
  - __key__: joey
    username: joey
    email: [email protected]
- Profile:
  - user: joey
    name: Jeffrey
"""
    sqla_yaml_fixtures.load(BaseModel, session, fixture)
    users = session.query(User).all()
    assert len(users) == 1
    assert users[0].profile.name == 'Jeffrey'
Exemplo n.º 15
0
def main(argv=None):
    args = make_parser().parse_args(argv)

    if not args.yes:
        print('DB: \x1b[0;34;40m{}\x1b[0m'.format(args.db_url))
        if (args.reset_db):
            print('RESET DB: \x1b[0;37;41m{}\x1b[0m'.format(
                'DB data will be deleted!'))
        print('Load fixtures, OK? (Ctrl-C to cancel)')
        try:
            input()  # input doest matter
        except KeyboardInterrupt:
            print('\nCancelled.')
            sys.exit(100)

    # get Base mapper class and create engine
    engine = create_engine(args.db_url)
    module_name, class_name = args.db_base.split(':')
    module = importlib.import_module(module_name)
    BaseClass = getattr(module, class_name)

    # reset DB
    if args.reset_db:
        BaseClass.metadata.drop_all(engine)
        BaseClass.metadata.create_all(engine)

    # load fixtures
    connection = engine.connect()
    session = Session(bind=connection)
    try:
        fixture_yaml = []
        for fixture_name in args.files:
            print('Loading file: {} ...'.format(fixture_name))
            with open(fixture_name) as fp:
                if args.jinja2:
                    from jinja2 import Template
                    file_yaml = Template(fp.read()).render()
                else:
                    file_yaml = fp.read()
            fixture_yaml.append(file_yaml)
        data_yaml = '\n'.join(fixture_yaml)
        sqla_yaml_fixtures.load(BaseClass, session, data_yaml)
        session.commit()
    except:
        session.close()
        raise

    if args.alembic_stamp:
        subprocess.check_call('alembic stamp head', shell=True)
Exemplo n.º 16
0
def test_insert_nested_list(session):
    fixture = """
- Group:
  - name: Ramones
    genres:
      - name: rock
      - name: punk
"""
    sqla_yaml_fixtures.load(BaseModel, session, fixture)
    genres = session.query(Genre).all()
    assert len(genres) == 2
    assert genres[0].name == 'rock'
    assert genres[0].group.name == 'Ramones'
    assert genres[1].name == 'punk'
    assert genres[1].group.name == 'Ramones'
Exemplo n.º 17
0
def test_2many_works(session):
    fixture = """
- Profile:
  - __key__: jeffrey
    name: Jeffrey
    user_id: 1

- Group:
  - name: Ramones
    members: [jeffrey]
"""
    sqla_yaml_fixtures.load(BaseModel, session, fixture)
    group = session.query(Group).filter_by(name="Ramones").one()
    assert len(group.members) == 1
    assert group.members[0].name == "Jeffrey"
Exemplo n.º 18
0
    def test__available_events__excludes_events_posted_by_user(self):
        fixture = """
            - User:
              - tg_id: 1
            - Event:
              - creator_id: 1
                name: Event 1
                description: short 1
                start_date: 2020-10-25 14:00:00
                end_date: 2020-10-25 14:00:00
            """
        sqla_yaml_fixtures.load(Base, db.session, fixture)

        events = self.available_events()
        self.assertEqual(len(events), 0,
                         "We should exclude events posted by the user")
Exemplo n.º 19
0
def test_2many_empty_is_ok(session):
    fixture = """
- User:
  - __key__: joey
    username: joey
    email: [email protected]
    profile:
      name: Jeffrey

- Group:
  - name: Ramones
    members: []
"""
    sqla_yaml_fixtures.load(BaseModel, session, fixture)
    groups = session.query(Group).all()
    assert len(groups) == 1
    assert len(groups[0].members) == 0
Exemplo n.º 20
0
def test_2many_invalid_ref(session):
    fixture = """
- User:
  - __key__: joey
    username: joey
    email: [email protected]
    profile:
      name: Jeffrey

- Group:
  - name: Ramones
    members: [joey] # should be joey.profile
"""
    with pytest.raises(Exception) as exc_info:
        sqla_yaml_fixtures.load(BaseModel, session, fixture)
    assert 'KeyError' in str(exc_info)
    assert 'groups' in str(exc_info)
Exemplo n.º 21
0
 def test__get_top_validated_events__returns_properly_sorted_events(self):
     conf.polling_candidates_by_quality_rating = 2
     conf.polling_candidates = 1
     soon = datetime.now() + timedelta(seconds=1)
     fixture = """
         - User:
           - tg_id: 1
         - Event:
           - creator_id: 1
             name: Event 1
             description: Top quality rating, lowest interest rating.
             start_date: {0}
             end_date: {0}
             is_blocked: False
             completed_validations: 1
             quality_rating: 3
             interest_rating: 1
           - creator_id: 1
             name: Event 2
             description: Lowest quality rating, top interest rating.
             start_date: {0}
             end_date: {0}
             is_blocked: False
             completed_validations: 1
             quality_rating: 1
             interest_rating: 3
           - creator_id: 1
             name: Event 3
             description: Mid quality and interest rating.
             start_date: {0}
             end_date: {0}
             is_blocked: False
             completed_validations: 1
             quality_rating: 2
             interest_rating: 2
         """.format(soon)
     sqla_yaml_fixtures.load(Base, db.session, fixture)
     events = Event.get_top_validated_events(user=User.by(tg_id=1))
     self.assertTrue(
         len(events) == 1, "User should get only properly sorted events")
     self.assertTrue(events[0].name == 'Event 3', "Wrong event is picked")
def test_2many_secondary(session):
    fixture = """
- Instrument:
  - __key__: drums
    name: drums
  - __key__: guitar
    name: guitar

- User:
  - __key__: joey
    username: joey
    email: [email protected]
    instruments:
      - drums
      - guitar
"""
    sqla_yaml_fixtures.load(BaseModel, session, fixture)
    users = session.query(User).order_by(User.username).all()
    assert users[0].username == 'joey'
    assert users[0].instruments[0].name == 'drums'
    assert users[0].instruments[1].name == 'guitar'
Exemplo n.º 23
0
def test_store_returned(session):
    fixture = """
- User:
  - __key__: dee
    username: deedee
    email: [email protected]
"""
    store = sqla_yaml_fixtures.load(BaseModel, session, fixture)
    users = session.query(User).all()
    assert len(users) == 1
    assert users[0].username == 'deedee'
    assert users[0].id is not None
    assert users[0].id == store.get('dee').id
Exemplo n.º 24
0
 def test__get_not_finished_validation__true_if_there_is_one_for_today(
         self):
     fixture = """
         - User:
           - tg_id: 1
           - tg_id: 2
         - Event:
           - creator_id: 2
             name: Event 1
             description: short 1
             start_date: 2020-10-25 14:00:00
             end_date: 2020-10-25 14:00:00
         - Validation:
           - validator_id: 1
             event_id: 1
         """
     sqla_yaml_fixtures.load(Base, db.session, fixture)
     user = db.session.query(User).first()
     self.assertTrue(
         user.get_not_finished_validation(),
         "User should have not finished validation if there is one from today"
     )
def test_self_referencing_2many_secondary(session):
    fixture = """
- User:
  - __key__: joey
    username: joey
  - __key__: johnny
    username: johnny
  - __key__: tommy
    username: tommy
    email: [email protected]
    friends:
      - joey
      - johnny
"""
    sqla_yaml_fixtures.load(BaseModel, session, fixture)
    users = session.query(User).order_by(User.username).all()
    assert len(users[0].friends) == 0
    assert len(users[1].friends) == 0
    assert len(users[2].friends) == 2
    assert users[2].username == 'tommy'
    assert users[2].friends[0].username == 'joey'
    assert users[2].friends[1].username == 'johnny'
Exemplo n.º 26
0
 def test__can_validate__false_if_has_not_finished_validation_today(self):
     fixture = """
         - User:
           - tg_id: 1
           - tg_id: 2
         - Event:
           - creator_id: 2
             name: Event 1
             description: short 1
             start_date: 2020-10-25 14:00:00
             end_date: 2020-10-25 14:00:00
         - Validation:
           - validator_id: 1
             event_id: 1
             call_to_violence: False
             quality_rating: 5
         """.format(datetime.now() + timedelta(1))
     sqla_yaml_fixtures.load(Base, db.session, fixture)
     user = db.session.query(User).filter(User.tg_id == 1).first()
     self.assertFalse(
         user.can_validate_today,
         "User should be able to validate if today's validation is not finished yet"
     )
Exemplo n.º 27
0
    def test__available_events__selects_event_with_active_start_date(self):
        fixture = """
            - User:
              - tg_id: 1
              - tg_id: 2
            - Event:
              - creator_id: 2
                name: Event 1
                description: short 1
                start_date: 2030-10-25 14:00:00
                end_date: 2030-10-25 14:00:00
              - creator_id: 2
                name: Event 2
                description: short 2
                start_date: {}
                end_date: 2046-10-25 14:00:00
            """.format(datetime.now() - timedelta(1))
        sqla_yaml_fixtures.load(Base, db.session, fixture)

        events = self.available_events()
        self.assertEqual(len(events), 1,
                         "We have only one event with start date in future")
        self.assertEqual(events[0].name, 'Event 1')
Exemplo n.º 28
0
 def test__can_validate__true_if_did_not_validate_at_all(self):
     fixture = """
         - User:
           - tg_id: 1
           - tg_id: 2
         - Event:
           - creator_id: 1
             name: Event 1
             description: short 1
             start_date: 2020-10-25 14:00:00
             end_date: 2020-10-25 14:00:00
         - Validation:
           - validator_id: 2
             event_id: 1
             quality_rating: 5
             interest_rating: 5
         """
     sqla_yaml_fixtures.load(Base, db.session, fixture)
     user = db.session.query(User).first()
     self.assertTrue(
         user.can_validate_today,
         "User should be able to validate if there were no validations done before"
     )
Exemplo n.º 29
0
def test_2many_no_backref(session):
    fixture = """
- User:
  - __key__: joey
    username: joey
    email: [email protected]
    roles:
      - name: owner
      - name: editor
      - name: viewer
"""
    data = sqla_yaml_fixtures.load(BaseModel, session, fixture)
    roles = session.query(Role).all()
    assert len(roles) == 3
    assert roles[0].user_id == data.get('joey').id
Exemplo n.º 30
0
 def test__can_validate__true_if_did_not_validate_yet_today(self):
     fixture = """
         - User:
           - tg_id: 1
           - tg_id: 2
         - Event:
           - creator_id: 2
             name: Event 1
             description: short 1
             start_date: 2020-10-25 14:00:00
             end_date: 2020-10-25 14:00:00
         - Validation:
           - validator_id: 1
             event_id: 1
             quality_rating: 5
             interest_rating: 5
             created: {}
         """.format(datetime.now() - timedelta(1))
     sqla_yaml_fixtures.load(Base, db.session, fixture)
     user = db.session.query(User).filter(User.tg_id == 1).first()
     self.assertTrue(
         user.can_validate_today,
         "User should be able to validate if there were no validations done today"
     )