def test_batch_membership__primary_key_constraint(create_batch_db, db_session,
                                                  test_batches):
    b = test_batches[0]
    db_session.add(batch.BatchMembership(batch_id=b.id, node_id="node_1"))
    db_session.commit()
    with pytest.raises(exc.IntegrityError, match=r"batch_membership_pk"):
        db_session.add(batch.BatchMembership(batch_id=b.id, node_id="node_1"))
        db_session.commit()
def test_batches_with_members(create_batch_db, db_session):
    b1 = batch.Batch(name="a", project_id="GDC-MISC")
    b2 = batch.Batch(name="b", project_id="GDC-INTERNAL")
    db_session.add(b1)
    db_session.add(b2)
    db_session.commit()

    b1.members.append(batch.BatchMembership(node_id="node_1"))
    b1.members.append(batch.BatchMembership(node_id="node_2"))
    b2.members.append(batch.BatchMembership(node_id="node_3"))
    b2.members.append(batch.BatchMembership(node_id="node_4"))
    db_session.commit()

    yield (b1, b2)
def test_batch_membership__node_id_required(create_batch_db, db_session,
                                            test_batches):
    b = test_batches[0]
    with pytest.raises(exc.IntegrityError,
                       match="violates not-null constraint"):
        db_session.add(batch.BatchMembership(batch_id=b.id))
        db_session.commit()
def test_batch_membership__repr():
    b = batch.BatchMembership(
        batch_id=1000,
        node_id="node_1",
        created_datetime=datetime.datetime(
            year=2021,
            month=1,
            day=18,
            hour=9,
            minute=30,
            second=10,
            microsecond=123,
            tzinfo=pytz.utc,
        ),
        updated_datetime=datetime.datetime(
            year=2021,
            month=1,
            day=18,
            hour=9,
            minute=30,
            second=10,
            microsecond=123,
            tzinfo=pytz.utc,
        ),
    )
    expected = "<BatchMembership(batch_id='1000', node_id='node_1', created_datetime='2021-01-18T09:30:10.000123+00:00', updated_datetime='2021-01-18T09:30:10.000123+00:00')>"
    assert repr(b) == expected
def test_batch_membership__node_in_multiple_batches(create_batch_db,
                                                    db_session, test_batches):
    b1 = test_batches[0]
    b2 = test_batches[1]

    b1.members.append(batch.BatchMembership(node_id="node_1"))
    b2.members.append(batch.BatchMembership(node_id="node_1"))
    db_session.commit()

    bm1 = (db_session.query(batch.BatchMembership).filter(
        batch.BatchMembership.batch_id == b1.id).one())

    bm2 = (db_session.query(batch.BatchMembership).filter(
        batch.BatchMembership.batch_id == b2.id).one())

    assert b1.members[0].node_id == bm1.node_id
    assert b2.members[0].node_id == bm2.node_id
def test_batch_membership__default_datetimes(create_batch_db, db_session,
                                             test_batches, attribute):
    b = test_batches[0]
    db_session.add(batch.BatchMembership(batch_id=b.id, node_id="node_1"))
    db_session.commit()
    b = db_session.query(batch.Batch).filter(batch.Batch.name == "a").one()
    date = getattr(b, attribute)
    assert date is not None
def test_batch_membership__indirect_create(create_batch_db, db_session,
                                           test_batches):
    b = test_batches[0]
    b.members.append(batch.BatchMembership(node_id="node_1"))
    db_session.commit()

    bm = (db_session.query(batch.BatchMembership).filter(
        batch.BatchMembership.batch_id == b.id).one())

    assert bm.batch_id == b.id
    assert bm.node_id == "node_1"
    assert bm.created_datetime is not None
    assert bm.updated_datetime is not None
    assert len(b.members) == 1
def test_batch__membership_to_json(contents, expected):
    b = batch.BatchMembership(**contents)

    assert b.to_json() == expected
def test_batch_membership__foreign_key_constraint(create_batch_db, db_session):
    with pytest.raises(exc.IntegrityError,
                       match=r"batch_membership_batch_id_fk"):
        db_session.add(batch.BatchMembership(batch_id=1, node_id="node_1"))
        db_session.commit()