# Path: tests/models/student.py
# class Student(Model):
#     # def __init__(self, **kwargs):
#         # We define model relations in __init__ method, because Python parser raises a NameError
#         # if we refer to a not yet defined class in body of another class.
#         # self.contact_info = ContactInfo()
#         # super(Student, self).__init__(**kwargs)
#     # contact_info = ContactInfo()
# 
#     # def row_level_access(self):
#     #     self.objects = self.objects.filter(user_in=self._context.user['id'],)
# 
# 
#     number = field.String("Student No", index=True)
#     pno = field.String("TC No", index=True)
#     name = field.String("First Name", type='text_tr', index=False)
#     surname = field.String("Last Name", type='text_tr', index=False)
#     join_date = field.Date("Join Date", index=True)
#     bio = field.Text("Biography", index=True)
# 
#     class AuthInfo(Node):
#         username = field.String("Username")
#         email = field.String("Email")
#         password = field.String("Password", index=False)
# 
#     class Lectures(ListNode):
#         name = field.String(type='text_tr')
#         code = field.String(required=False)
#         credit = field.Integer(default=0)
#         role = Role('Role_1', index=True)
# 
#         class NodeInListNode(Node):
#             foo = field.String(index=False)
# 
#         class Exams(ListNode):
#             type = field.String(index=False)
#             date = field.Date(index=False)
#             point = field.Integer(index=False)
# 
#         class Attendance(ListNode):
#             date = field.Date(index=False)
#             hour = field.Integer(index=False)
#             attended = field.Boolean(default=False, index=False)
# 
#     class Lecturer(ListNode):
#         role = Role('Role_2', index=True)
# 
# Path: pyoko/db/adapter/db_riak.py
# class BlockSave(object):
#     def __init__(self, mdl, query_dict=None):
#         self.mdl = mdl
#         self.query_dict = query_dict or {}
#         self.query_dict['updated_at__gt'] = datetime.now().strftime(DATE_TIME_FORMAT)
# 
#     def __enter__(self):
#         Adapter.block_saved_keys = []
#         Adapter.COLLECT_SAVES = True
#         Adapter.COLLECT_SAVES_FOR_MODEL = self.mdl.__name__
# 
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         key_list = set(Adapter.block_saved_keys)
#         self.make_sure(key_list)
#         Adapter.COLLECT_SAVES = False
# 
#     def make_sure(self, key_list):
#         while Adapter.block_saved_keys and not key_list.issubset(
#                 self.mdl.objects.all(**self.query_dict).values_list('key')):
#             time.sleep(.4)
# 
# class BlockDelete(BlockSave):
#     def __init__(self, mdl, query_dict=None):
#         super(BlockDelete, self).__init__(mdl, query_dict)
#         self.query_dict['deleted'] = True


import time
from .models import Student
from pyoko.db.adapter.db_riak import BlockSave, BlockDelete
# -*-  coding: utf-8 -*-
"""
"""

# Copyright (C) 2015 ZetaOps Inc.
#
# This file is licensed under the GNU General Public License v3
# (GPLv3).  See LICENSE.txt for details.



class TestCase:
    """
    For the sake of DRY and to speedup tests, we're running clear_bucket
    only once at first test, then creating a new object and reusing it.
    """

    def test_block_save(self):
        Student.objects.all().delete()
        t1 = time.time()
        with BlockSave(Student):
            for i in range(10):
                Student(surname='bar', name='foo_%s' % i).save()
        assert Student.objects.count() == 10
        print("BlockSave took %s" % (time.time() - t1))
        student = Student.objects.get(surname='bar', name='foo_9')
        student.blocking_save(query_dict={"name": "foo_10", "surname": "bar_10"})
        assert student.name == "foo_10"
        assert student.surname == "bar_10"


# Path: tests/models/student.py
# class Student(Model):
#     # def __init__(self, **kwargs):
#         # We define model relations in __init__ method, because Python parser raises a NameError
#         # if we refer to a not yet defined class in body of another class.
#         # self.contact_info = ContactInfo()
#         # super(Student, self).__init__(**kwargs)
#     # contact_info = ContactInfo()
# 
#     # def row_level_access(self):
#     #     self.objects = self.objects.filter(user_in=self._context.user['id'],)
# 
# 
#     number = field.String("Student No", index=True)
#     pno = field.String("TC No", index=True)
#     name = field.String("First Name", type='text_tr', index=False)
#     surname = field.String("Last Name", type='text_tr', index=False)
#     join_date = field.Date("Join Date", index=True)
#     bio = field.Text("Biography", index=True)
# 
#     class AuthInfo(Node):
#         username = field.String("Username")
#         email = field.String("Email")
#         password = field.String("Password", index=False)
# 
#     class Lectures(ListNode):
#         name = field.String(type='text_tr')
#         code = field.String(required=False)
#         credit = field.Integer(default=0)
#         role = Role('Role_1', index=True)
# 
#         class NodeInListNode(Node):
#             foo = field.String(index=False)
# 
#         class Exams(ListNode):
#             type = field.String(index=False)
#             date = field.Date(index=False)
#             point = field.Integer(index=False)
# 
#         class Attendance(ListNode):
#             date = field.Date(index=False)
#             hour = field.Integer(index=False)
#             attended = field.Boolean(default=False, index=False)
# 
#     class Lecturer(ListNode):
#         role = Role('Role_2', index=True)
# 
# Path: pyoko/db/adapter/db_riak.py
# class BlockSave(object):
#     def __init__(self, mdl, query_dict=None):
#         self.mdl = mdl
#         self.query_dict = query_dict or {}
#         self.query_dict['updated_at__gt'] = datetime.now().strftime(DATE_TIME_FORMAT)
# 
#     def __enter__(self):
#         Adapter.block_saved_keys = []
#         Adapter.COLLECT_SAVES = True
#         Adapter.COLLECT_SAVES_FOR_MODEL = self.mdl.__name__
# 
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         key_list = set(Adapter.block_saved_keys)
#         self.make_sure(key_list)
#         Adapter.COLLECT_SAVES = False
# 
#     def make_sure(self, key_list):
#         while Adapter.block_saved_keys and not key_list.issubset(
#                 self.mdl.objects.all(**self.query_dict).values_list('key')):
#             time.sleep(.4)
# 
# class BlockDelete(BlockSave):
#     def __init__(self, mdl, query_dict=None):
#         super(BlockDelete, self).__init__(mdl, query_dict)
#         self.query_dict['deleted'] = True


# Path: tests/test_index_delay_solutions.py
import time
from .models import Student
from pyoko.db.adapter.db_riak import BlockSave, BlockDelete

# -*-  coding: utf-8 -*-
"""
"""

# Copyright (C) 2015 ZetaOps Inc.
#
# This file is licensed under the GNU General Public License v3
# (GPLv3).  See LICENSE.txt for details.



class TestCase:
    """
    For the sake of DRY and to speedup tests, we're running clear_bucket
    only once at first test, then creating a new object and reusing it.
    """

    def test_block_save(self):
        Student.objects.all().delete()
        t1 = time.time()
        with BlockSave(Student):
            for i in range(10):
                Student(surname='bar', name='foo_%s' % i).save()
        assert Student.objects.count() == 10
        print("BlockSave took %s" % (time.time() - t1))
        student = Student.objects.get(surname='bar', name='foo_9')
        student.blocking_save(query_dict={"name": "foo_10", "surname": "bar_10"})
        assert student.name == "foo_10"
        assert student.surname == "bar_10"


        with BlockSave(Student, query_dict={'surname': 'zubizu'}):