def test_create_message(): addstuff() session = Session() message = PullMessage() version = session.query(models.Version).first() message.add_version(version) assert message.to_json() == PullMessage(message.to_json()).to_json()
def test_encode_message(): addstuff() session = Session() message = PullMessage() version = session.query(models.Version).first() message.add_version(version) assert message.to_json() == json.loads(json.dumps(message.to_json()))
def test_encode_message(): addstuff() changestuff() session = Session() message = PushMessage() message.add_unversioned_operations() message.set_node(session.query(models.Node).first()) assert message.to_json() == json.loads(json.dumps(message.to_json()))
def test_compression_consistency(): addstuff() changestuff() session = Session() ops = session.query(models.Operation).all() compress() news = session.query(models.Operation).order_by(models.Operation.order).all() assert news == compressed_operations(ops)
def test_detail_by_custom_pk(self): self.author() view = views.AuthorDetail.as_view(pk_url_kwarg='foo') res = view(DummyRequest(), foo=1) self.assertEqual(res.status_code, 200) self.assertEqual(res.context['object'], Session.query(Author).filter(Author.id==1).one()) self.assertEqual(res.context['author'], Session.query(Author).filter(Author.id==1).one()) self.assertTemplateUsed(res, 'tests:templates/author_detail.html')
def test_create_message(): addstuff() changestuff() session = Session() message = PushMessage() message.add_unversioned_operations() message.set_node(session.query(models.Node).first()) assert message.to_json() == PushMessage(message.to_json()).to_json()
def test_encode_message(): addstuff() changestuff() session = Session() message = PushMessage() message.add_unversioned_operations() message.set_node(session.query(models.Node).first()) assert message.to_json() == json.loads(json.dumps(message.to_json()))
def test_encode_message(): addstuff() session = Session() message = PullMessage() version = session.query(models.Version).first() message.add_version(version) msg_json = json.dumps(message.to_json(), cls=SyncdbJSONEncoder) assert message.to_json() == json.loads(msg_json)
def test_template_name_field(self): self.page() view = views.PageDetail.as_view() res = view(DummyRequest(), pk=1) self.assertEqual(res.status_code, 200) self.assertEqual(res.context['object'], Session.query(Page).filter(Page.id==1).one()) self.assertEqual(res.context['page'], Session.query(Page).filter(Page.id==1).one()) self.assertTemplateUsed(res, 'tests:templates/page_template.html')
def test_create_message(): addstuff() changestuff() session = Session() message = PushMessage() message.add_unversioned_operations() message.set_node(session.query(models.Node).first()) assert message.to_json() == PushMessage(message.to_json()).to_json()
def test_template_name_suffix(self): self.author() view = views.AuthorDetail.as_view(template_name_suffix='_view') res = view(DummyRequest(), pk=1) self.assertEqual(res.status_code, 200) self.assertEqual(res.context['object'], Session.query(Author).filter(Author.id==1).one()) self.assertEqual(res.context['author'], Session.query(Author).filter(Author.id==1).one()) self.assertTemplateUsed(res, 'tests:templates/author_view.html')
def addstuff(): a1 = A(name="first a") a2 = A(name="second a") b1 = B(name="first b", a=a1) b2 = B(name="second b", a=a1) b3 = B(name="third b", a=a2) session = Session() session.add_all([a1, a2, b1, b2, b3]) session.commit()
def test_context_object_name(self): self.author() view = views.AuthorDetail.as_view(context_object_name='thingy') res = view(DummyRequest(), pk=1) self.assertEqual(res.status_code, 200) self.assertEqual(res.context['object'], Session.query(Author).filter(Author.id==1).one()) self.assertEqual(res.context['thingy'], Session.query(Author).filter(Author.id==1).one()) self.assertFalse('author' in res.context) self.assertTemplateUsed(res, 'tests:templates/author_detail.html')
def test_detail_by_custom_slug(self): self.author() self.author(name='Scott Rosenberg', slug='scott-rosenberg') view = views.AuthorDetail.as_view(slug_url_kwarg='foo') res = view(DummyRequest(), foo='scott-rosenberg') self.assertEqual(res.status_code, 200) self.assertEqual(res.context['object'], Session.query(Author).filter(Author.slug=='scott-rosenberg').one()) self.assertEqual(res.context['author'], Session.query(Author).filter(Author.slug=='scott-rosenberg').one()) self.assertTemplateUsed(res, 'tests:templates/author_detail.html')
def test_message_does_not_contaminate_database(): addstuff() session = Session() message = PullMessage() version = session.query(models.Version).first() message.add_version(version) # test that the are no unversioned operations assert not session.query(models.Operation).\ filter(models.Operation.version_id == None).all()
def test_compression_consistency(): addstuff() changestuff() session = Session() ops = session.query(models.Operation).all() compress() news = session.query(models.Operation).order_by( models.Operation.order).all() assert news == compressed_operations(ops)
def test_sign_message(): addstuff() changestuff() session = Session() message = PushMessage() message.set_node(session.query(models.Node).first()) message.add_unversioned_operations() assert message.islegit(session) message.key += "broken" assert not message.islegit(session)
def test_sign_message(): addstuff() changestuff() session = Session() message = PushMessage() message.set_node(session.query(models.Node).first()) message.add_unversioned_operations() assert message.islegit(session) message.key += "broken" assert not message.islegit(session)
def test_compression_correctness(): addstuff() changestuff() session = Session() ops = compressed_operations(session.query(models.Operation).all()) groups = group_by(lambda op: (op.content_type_id, op.row_id), ops) for g in groups.itervalues(): logging.info(g) assert len(g) == 1 # assert correctness when compressing operations from a pull # message pull_ops = [ models.Operation(command='i', content_type_id=1, row_id=1, order=1), models.Operation(command='d', content_type_id=1, row_id=1, order=2), models.Operation(command='i', content_type_id=1, row_id=1, order=3), models.Operation(command='u', content_type_id=1, row_id=1, order=4), # result of above should be a single 'i' models.Operation(command='u', content_type_id=2, row_id=1, order=5), models.Operation(command='d', content_type_id=2, row_id=1, order=6), models.Operation(command='i', content_type_id=2, row_id=1, order=7), models.Operation(command='d', content_type_id=2, row_id=1, order=8), # result of above should be a single 'd' models.Operation(command='d', content_type_id=3, row_id=1, order=9), models.Operation(command='i', content_type_id=3, row_id=1, order=10), # result of above should be an 'u' models.Operation(command='i', content_type_id=4, row_id=1, order=11), models.Operation(command='u', content_type_id=4, row_id=1, order=12), models.Operation(command='d', content_type_id=4, row_id=1, order=13), # result of above should be no operations models.Operation(command='d', content_type_id=5, row_id=1, order=14), models.Operation(command='i', content_type_id=5, row_id=1, order=15), models.Operation(command='d', content_type_id=5, row_id=1, order=16), # result of above should be a single 'd' models.Operation(command='u', content_type_id=6, row_id=1, order=17), models.Operation(command='d', content_type_id=6, row_id=1, order=18), models.Operation(command='i', content_type_id=6, row_id=1, order=19), # result of above should be an 'u' models.Operation(command='d', content_type_id=7, row_id=1, order=20), models.Operation(command='i', content_type_id=7, row_id=1, order=21), models.Operation(command='u', content_type_id=7, row_id=1, order=22) # result of above should be an 'u' ] compressed = compressed_operations(pull_ops) logging.info("len(compressed) == {0}".format(len(compressed))) logging.info("\n".join(repr(op) for op in compressed)) assert len(compressed) == 6 assert compressed[0].command == 'i' assert compressed[1].command == 'd' assert compressed[2].command == 'u' assert compressed[3].command == 'd' assert compressed[4].command == 'u' assert compressed[5].command == 'u'
def test_compression_correctness(): addstuff() changestuff() session = Session() ops = compressed_operations(session.query(models.Operation).all()) groups = group_by(lambda op: (op.content_type_id, op.row_id), ops) for g in groups.values(): logging.info(g) assert len(g) == 1 # assert correctness when compressing operations from a pull # message pull_ops = [ models.Operation(command='i', content_type_id=1, row_id=1, order=1), models.Operation(command='d', content_type_id=1, row_id=1, order=2), models.Operation(command='i', content_type_id=1, row_id=1, order=3), models.Operation(command='u', content_type_id=1, row_id=1, order=4), # result of above should be a single 'i' models.Operation(command='u', content_type_id=2, row_id=1, order=5), models.Operation(command='d', content_type_id=2, row_id=1, order=6), models.Operation(command='i', content_type_id=2, row_id=1, order=7), models.Operation(command='d', content_type_id=2, row_id=1, order=8), # result of above should be a single 'd' models.Operation(command='d', content_type_id=3, row_id=1, order=9), models.Operation(command='i', content_type_id=3, row_id=1, order=10), # result of above should be an 'u' models.Operation(command='i', content_type_id=4, row_id=1, order=11), models.Operation(command='u', content_type_id=4, row_id=1, order=12), models.Operation(command='d', content_type_id=4, row_id=1, order=13), # result of above should be no operations models.Operation(command='d', content_type_id=5, row_id=1, order=14), models.Operation(command='i', content_type_id=5, row_id=1, order=15), models.Operation(command='d', content_type_id=5, row_id=1, order=16), # result of above should be a single 'd' models.Operation(command='u', content_type_id=6, row_id=1, order=17), models.Operation(command='d', content_type_id=6, row_id=1, order=18), models.Operation(command='i', content_type_id=6, row_id=1, order=19), # result of above should be an 'u' models.Operation(command='d', content_type_id=7, row_id=1, order=20), models.Operation(command='i', content_type_id=7, row_id=1, order=21), models.Operation(command='u', content_type_id=7, row_id=1, order=22) # result of above should be an 'u' ] compressed = compressed_operations(pull_ops) logging.info("len(compressed) == {0}".format(len(compressed))) logging.info("\n".join(repr(op) for op in compressed)) assert len(compressed) == 6 assert compressed[0].command == 'i' assert compressed[1].command == 'd' assert compressed[2].command == 'u' assert compressed[3].command == 'd' assert compressed[4].command == 'u' assert compressed[5].command == 'u'
def test_tracking(): addstuff() changestuff() session = Session() assert session.query(models.Operation).\ filter(models.Operation.command == 'i').\ count() == 5, "insert operations don't match" assert session.query(models.Operation).\ filter(models.Operation.command == 'u').\ count() == 2, "update operations don't match" assert session.query(models.Operation).\ filter(models.Operation.command == 'd').\ count() == 1, "delete operations don't match"
def test_tracking(): addstuff() changestuff() session = Session() assert session.query(models.Operation).\ filter(models.Operation.command == 'i').\ count() == 5, "insert operations don't match" assert session.query(models.Operation).\ filter(models.Operation.command == 'u').\ count() == 2, "update operations don't match" assert session.query(models.Operation).\ filter(models.Operation.command == 'd').\ count() == 1, "delete operations don't match"
def test_compression(): addstuff() changestuff() compress() # remove unnecesary operations session = Session() assert session.query(models.Operation).\ filter(models.Operation.command == 'i').\ count() == 4, "insert operations don't match" assert session.query(models.Operation).\ filter(models.Operation.command == 'u').\ count() == 0, "update operations don't match" assert session.query(models.Operation).\ filter(models.Operation.command == 'd').\ count() == 0, "delete operations don't match"
def test_compression(): addstuff() changestuff() compress() # remove unnecesary operations session = Session() assert session.query(models.Operation).\ filter(models.Operation.command == 'i').\ count() == 4, "insert operations don't match" assert session.query(models.Operation).\ filter(models.Operation.command == 'u').\ count() == 0, "update operations don't match" assert session.query(models.Operation).\ filter(models.Operation.command == 'd').\ count() == 0, "delete operations don't match"
def changestuff(): session = Session() a1, a2 = session.query(A) b1, b2, b3 = session.query(B) a1.name = "first a modified" b2.a = a2 session.delete(b3) session.commit()
def test_find_direct_conflicts(): addstuff() changestuff() session = Session() message_ops = create_fake_operations() conflicts = find_direct_conflicts( message_ops, session.query(models.Operation).all()) expected = [ (message_ops[0], models.Operation(row_id=3, content_type_id=ct_b_id, command='d')), # b3 (message_ops[1], models.Operation(row_id=1, content_type_id=ct_a_id, command='u'))] # a1 logging.info(conflicts) logging.info(expected) assert repr(conflicts) == repr(expected)
def test_find_direct_conflicts(): addstuff() changestuff() session = Session() message_ops = create_fake_operations() conflicts = find_direct_conflicts(message_ops, session.query(models.Operation).all()) expected = [ (message_ops[0], models.Operation(row_id=3, content_type_id=ct_b_id, command='d')), # b3 (message_ops[1], models.Operation(row_id=1, content_type_id=ct_a_id, command='u')) ] # a1 logging.info(conflicts) logging.info(expected) assert repr(conflicts) == repr(expected)
def teardown(): session = Session() map(session.delete, session.query(A)) map(session.delete, session.query(B)) map(session.delete, session.query(models.Operation)) map(session.delete, session.query(models.Version)) session.commit()
def test_message_query(): addstuff() session = Session() message = PullMessage() version = session.query(models.Version).first() message.add_version(version) # test equal representation, because the test models are well printed for b in session.query(B): assert repr(b) == repr(message.query(B).filter( attr('id') == b.id).all()[0]) for op in session.query(models.Operation): assert repr(op) == repr(message.query(models.Operation).filter( attr('order') == op.order).all()[0]) try: message.query(1) raise Exception("Message query did not fail") except TypeError: pass
def changestuff(): session = Session() a1, a2 = session.query(A) b1, b2, b3 = session.query(B) a1.name = "first a modified" b2.a = a2 session.delete(b3) session.commit()
def test_find_dependency_conflicts(): addstuff() changestuff() session = Session() message_ops = create_fake_operations() conflicts = find_dependency_conflicts( message_ops, session.query(models.Operation).all(), session) expected = [ (message_ops[1], # a1 models.Operation(row_id=1, content_type_id=ct_b_id, command='i')), # b1 (message_ops[2], # a2 models.Operation(row_id=2, content_type_id=ct_b_id, command='i')), # b2 (message_ops[2], # a2 models.Operation(row_id=2, content_type_id=ct_b_id, command='u'))] # b2 logging.info(conflicts) logging.info(expected) assert repr(conflicts) == repr(expected)
def addstuff(): a1 = A(name="first a") a2 = A(name="second a") b1 = B(name="first b", a=a1) b2 = B(name="second b", a=a1) b3 = B(name="third b", a=a2) session = Session() session.add_all([a1, a2, b1, b2, b3]) session.commit()
def test_find_dependency_conflicts(): addstuff() changestuff() session = Session() message_ops = create_fake_operations() conflicts = find_dependency_conflicts( message_ops, session.query(models.Operation).all(), session) expected = [ ( message_ops[1], # a1 models.Operation(row_id=1, content_type_id=ct_b_id, command='i')), # b1 ( message_ops[2], # a2 models.Operation(row_id=2, content_type_id=ct_b_id, command='i')), # b2 ( message_ops[2], # a2 models.Operation(row_id=2, content_type_id=ct_b_id, command='u')) ] # b2 logging.info(conflicts) logging.info(expected) assert repr(conflicts) == repr(expected)
def teardown(): session = Session() As = session.query(A).all() Bs = session.query(B).all() Ops = session.query(models.Operation).all() for a in As: session.delete(a) # map(session.delete, As) # for some reason mapping session.delete does not execute for b in Bs: session.delete(b) # map(session.delete, Bs) # for some reason mapping session.delete does not execute for op in Ops: session.delete(op) # map(session.delete, Ops) # for some reason mapping session.delete does not execute session.commit()
def teardown(): session = Session() # map(session.delete, session.query(A)) for a in session.query(A).all(): session.delete(a) for b in session.query(B).all(): session.delete(b) for op in session.query(models.Operation).all(): session.delete(op) for ver in session.query(models.Version).all(): session.delete(ver) session.commit()
def addstuff(): a1 = A(name="first a") a2 = A(name="second a") b1 = B(name="first b", a=a1) b2 = B(name="second b", a=a1) b3 = B(name="third b", a=a2) session = Session() session.add_all([a1, a2, b1, b2, b3]) version = models.Version() version.created = datetime.datetime.now() session.add(version) session.flush() version_id = version.version_id session.commit() session = Session() for op in session.query(models.Operation): op.version_id = version_id session.commit()
def teardown(): session = Session() for a in session.query(A).all(): session.delete(a) for b in session.query(B).all(): session.delete(b) for op in session.query(models.Operation).all(): session.delete(op) session.commit()