def test_get_latest_accepted(db_conn, cards_table): """ Expect to pull the latest accepted version out of the database, given a kind and an entity_id. """ cards_table.insert([{ 'id': 'A1', 'entity_id': 'A', 'created': r.time(2004, 11, 3, 'Z'), 'status': 'accepted', }, { 'id': 'B2', 'entity_id': 'A', 'created': r.time(2005, 11, 3, 'Z'), 'status': 'accepted', }, { 'id': 'C3', 'entity_id': 'B', 'created': r.time(2006, 11, 3, 'Z'), 'status': 'accepted', }]).run(db_conn) e = entity.get_latest_accepted('card', 'A') assert isinstance(e, Card)
def create_proposal_in_db(posts_table, units_table, db_conn): posts_table.insert({ 'id': 'jklm', 'created': r.now(), 'modified': r.now(), 'user_id': 'abcd1234', 'topic_id': 'wxyz7890', 'body': '''A Modest Proposal for Preventing the Children of Poor People From Being a Burthen to Their Parents or Country, and for Making Them Beneficial to the Publick.''', 'kind': 'proposal', 'name': 'New Unit', 'replies_to_id': None, 'entity_version': { 'id': 'slash-1', 'kind': 'unit', }, }).run(db_conn) units_table.insert({ 'id': 'slash-1', 'created': r.time(2014, 1, 1, 'Z'), 'modified': r.time(2014, 1, 1, 'Z'), 'entity_id': 'slash', 'previous_id': None, 'language': 'en', 'name': 'Dividing two numbers.', 'status': 'accepted', 'available': True, 'tags': ['math'], 'body': 'The joy and pleasure of dividing numbers.', 'require_ids': ['plus', 'minus', 'times'], }).run(db_conn)
def run(self, conn): (start_date, end_date) = self.time_dist.get() time_1 = r.time(start_date.year, start_date.month, start_date.day, 'Z') time_2 = r.time(end_date.year, end_date.month, end_date.day, 'Z') cursor = r.db(self.db).table(self.table).between(time_1, time_2, index="datetime").count().run(conn) return { }
def get_data(): data = [{ 'id': 'joe', 'last_updated': r.time(2014, 6, 3, 'Z') }, { 'id': 'sam', 'last_updated': r.time(2014, 8, 25, 'Z') }] return as_db_and_table('d', 'people', data)
def test_filter_during(self, conn): table = r.db('d').table('people') result = table.filter( lambda doc: doc['last_updated'].during( r.time(2014, 6, 1, 'Z'), r.time(2014, 6, 30, 'Z') ) ).run(conn) result = list(result) assertEqual(2, len(result))
def test_during_closed_right(self, conn): expected = [{'id': 'joe', 'is_during': True}] result = r.db('d').table('people').map( lambda doc: { 'id': doc['id'], 'is_during': doc['last_updated'].during(r.time(2014, 6, 2, 'Z'), r.time(2014, 8, 25, 'Z'), right_bound='closed') }).run(conn) assertEqUnordered(expected, list(result))
def test_during_1(self, conn): expected = [ {'id': 'joe', 'is_during': False}, {'id': 'sam', 'is_during': True} ] result = r.db('d').table('people').map( lambda doc: { 'id': doc['id'], 'is_during': doc['last_updated'].during(r.time(2014, 7, 10, 'Z'), r.time(2014, 12, 1, 'Z')) } ).run(conn) assertEqUnordered(expected, list(result))
def update_with_date_random(db_name, tbl_name, user_object, id): return r.db(db_name).table(tbl_name).get(id).replace(lambda doc: r.branch( (doc == None), doc.merge(doc).merge({ 'created_at': r.time(random.randrange(1995, 2015, 1), random.randrange(1, 12, 1), random.randrange(1, 30, 1), 'Z') }), doc.merge(doc).merge( { 'created_at': r.time(random.randrange(1995, 2015, 1), random.randrange(1, 12, 1), random.randrange(1, 30, 1), 'Z') }, {'updated_at': r.now()}))).run()
def run(self, conn): cid = "customer%03d" % self.cid_dist.get() typ = "type%d" % self.typ_dist.get() (start_date, end_date) = self.time_dist.get() time_1 = r.time(start_date.year, start_date.month, start_date.day, 'Z') time_2 = r.time(end_date.year, end_date.month, end_date.day, 'Z') res = r.db(self.db).table(self.table).between([cid, time_1], [cid, time_2], index="compound") \ .filter(lambda row: row["type"].eq(typ)) \ .map(lambda row: row["arr"].reduce(lambda acc,val: acc + val, 0)) \ .reduce(lambda acc,val: acc + val, 0) \ .run(conn) return { }
def run(self, conn): cid = "customer%03d" % self.cid_dist.get() typ = "type%d" % self.typ_dist.get() (start_date, end_date) = self.time_dist.get() time_1 = r.time(start_date.year, start_date.month, start_date.day, 'Z') time_2 = r.time(end_date.year, end_date.month, end_date.day, 'Z') res = r.db(self.db).table(self.table).between([cid, time_1], [cid, time_2], index="compound") \ .filter(lambda row: row["type"].eq(typ)) \ .map(lambda row: row["arr"].reduce(lambda acc,val: acc + val, 0)) \ .reduce(lambda acc,val: acc + val, 0) \ .run(conn) return {}
def test_during_closed_right(self, conn): expected = [ {'id': 'joe', 'is_during': True} ] result = r.db('d').table('people').map( lambda doc: { 'id': doc['id'], 'is_during': doc['last_updated'].during( r.time(2014, 6, 2, 'Z'), r.time(2014, 8, 25, 'Z'), right_bound='closed' ) } ).run(conn) assertEqUnordered(expected, list(result))
def test_during_open_left(self, conn): expected = [ {'id': 'joe', 'is_during': False}, {'id': 'sam', 'is_during': False} ] result = r.db('d').table('people').map( lambda doc: { 'id': doc['id'], 'is_during': doc['last_updated'].during( r.time(2014, 6, 3, 'Z'), r.time(2014, 8, 25, 'Z'), left_bound='open' ) } ).run(conn) assertEqUnordered(expected, list(result))
def test_error_with_less_than_4_args(self, conn): try: query = r.db('unimportant').table('very').update({ 'update_time': r.time(2014, 3, 24) }).run(conn) except RqlCompileError as e: err = e assert('expected between 4 and 7' in err.message.lower())
def test_during_3(self, conn): expected = [{ 'id': 'joe', 'is_during': True }, { 'id': 'sam', 'is_during': False }] result = r.db('d').table('people').map( lambda doc: { 'id': doc['id'], 'is_during': doc['last_updated'].during(r.time(2014, 6, 3, 'Z'), r.time(2014, 8, 25, 'Z')) }).run(conn) assertEqUnordered(expected, list(result))
def test_get_latest_accepted(db_conn, cards_table): """ Expect to pull the latest accepted version out of the database, given a kind and an entity_id. """ cards_table.insert( [ {"id": "A1", "entity_id": "A", "created": r.time(2004, 11, 3, "Z"), "status": "accepted", "kind": "video"}, {"id": "B2", "entity_id": "A", "created": r.time(2005, 11, 3, "Z"), "status": "accepted", "kind": "video"}, {"id": "C3", "entity_id": "B", "created": r.time(2006, 11, 3, "Z"), "status": "accepted", "kind": "video"}, ] ).run(db_conn) e = entity.get_latest_accepted(db_conn, "card", "A") assert isinstance(e, Card)
def test_error_with_less_than_4_args(self, conn): try: query = r.db('unimportant').table('very').update({ 'update_time': r.time(2014, 3, 24) }).run(conn) except RqlCompileError as e: err = e assert ('expected between 4 and 7' in err.message.lower())
def create_proposal_in_db(posts_table, units_table, db_conn): posts_table.insert({ 'id': 'jklm', 'created': r.now(), 'modified': r.now(), 'user_id': 'abcd1234', 'topic_id': 'wxyz7890', 'body': '''A Modest Proposal for Preventing the Children of Poor People From Being a Burthen to Their Parents or Country, and for Making Them Beneficial to the Publick.''', 'kind': 'proposal', 'name': 'New Unit', 'replies_to_id': None, 'entity_versions': [{ 'id': 'slash-1', 'kind': 'unit', }], }).run(db_conn) units_table.insert({ 'id': 'slash-1', 'created': r.time(2014, 1, 1, 'Z'), 'modified': r.time(2014, 1, 1, 'Z'), 'entity_id': 'slash', 'previous_id': None, 'language': 'en', 'name': 'Dividing two numbers.', 'status': 'accepted', 'available': True, 'tags': ['math'], 'body': 'The joy and pleasure of dividing numbers.', 'require_ids': ['plus', 'minus', 'times'], }).run(db_conn)
def test_time_year_month_day_tz(self, conn): r.db('unimportant').table('very').update( lambda doc: doc.merge({'updated': r.time(2014, 6, 10, 'Z')}) ).run(conn) result = r.db('unimportant').table('very').get('say_anything').run(conn) update_time = result['updated'] assertEqual(2014, update_time.year) assertEqual(6, update_time.month) assertEqual(10, update_time.day) assert(isinstance(update_time.tzinfo, RqlTzinfo))
def test_time_year_month_day_tz(self, conn): r.db('unimportant').table('very').update(lambda doc: doc.merge( {'updated': r.time(2014, 6, 10, 'Z')})).run(conn) result = r.db('unimportant').table('very').get('say_anything').run( conn) update_time = result['updated'] assertEqual(2014, update_time.year) assertEqual(6, update_time.month) assertEqual(10, update_time.day) assert (isinstance(update_time.tzinfo, RqlTzinfo))
def test_list_required_by(db_conn, cards_table): """ Expect to list all the entity that require the given one. """ cards_table.insert([{ 'entity_id': 'abcd', 'unit_id': 'zytx', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'kind': 'video', 'requires': ['zxyz'], }, { 'entity_id': 'abcd', 'unit_id': 'zytx', 'created': r.time(1986, 11, 3, 'Z'), 'modified': r.time(1986, 11, 3, 'Z'), 'status': 'accepted', 'kind': 'video', }, { 'entity_id': 'zxyz', 'unit_id': 'zytx', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'kind': 'video', }, { 'entity_id': 'qwer', 'unit_id': 'zytx', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'kind': 'choice', 'requires': ['abcd'], }]).run(db_conn) cards = Card.list_required_by(db_conn, 'abcd') assert len(cards) == 1 assert cards[0]['entity_id'] == 'qwer'
def test_get_versions(db_conn, cards_table): """ Expect to get the latest versions of the card. """ cards_table.insert([{ 'id': 'A1', 'entity_id': 'A', 'created': r.time(2004, 11, 3, 'Z'), 'status': 'accepted', }, { 'id': 'B2', 'entity_id': 'A', 'created': r.time(2005, 11, 3, 'Z'), }, { 'id': 'C3', 'entity_id': 'B', 'created': r.time(2006, 11, 3, 'Z'), 'status': 'accepted', }]).run(db_conn) card_versions = Card.get_versions(db_conn, 'A') assert len(card_versions) == 2
def test_time_year_month_day_hour_minute_second_tz(self, conn): r.db('unimportant').table('very').update({ 'updated': r.time(2014, 6, 10, 15, 30, 45, 'Z') }).run(conn) result = r.db('unimportant').table('very').get('say_anything').run(conn) update_time = result['updated'] assertEqual(2014, update_time.year) assertEqual(6, update_time.month) assertEqual(10, update_time.day) assertEqual(15, update_time.hour) assertEqual(30, update_time.minute) assertEqual(45, update_time.second) assert(isinstance(update_time.tzinfo, RqlTzinfo))
def test_latest_accepted(db_conn, units_table): """ Expect to get the latest accepted unit version. """ units_table.insert([{ 'id': 'A1', 'entity_id': 'A', 'created': r.time(2004, 11, 3, 'Z'), 'status': 'accepted', }, { 'id': 'B2', 'entity_id': 'A', 'created': r.time(2005, 11, 3, 'Z'), 'status': 'accepted', }, { 'id': 'C3', 'entity_id': 'B', 'created': r.time(2006, 11, 3, 'Z'), 'status': 'accepted', }]).run(db_conn) unit = Unit.get_latest_accepted(db_conn, 'A') assert unit['id'] == 'B2'
def test_latest_accepted_subject(db_conn, subjects_table): """ Expect to get the latest accepted subject version. """ subjects_table.insert([{ 'id': 'A1', 'entity_id': 'A', 'created': r.time(2004, 11, 3, 'Z'), 'status': 'accepted', }, { 'id': 'B2', 'entity_id': 'A', 'created': r.time(2005, 11, 3, 'Z'), 'status': 'accepted', }, { 'id': 'C3', 'entity_id': 'B', 'created': r.time(2006, 11, 3, 'Z'), 'status': 'accepted', }]).run(db_conn) subject = Subject.get_latest_accepted(db_conn, 'A') assert subject['id'] == 'B2'
def test_latest_accepted_set(db_conn, sets_table): """ Expect to get the latest accepted set version. """ sets_table.insert([{ 'id': 'A1', 'entity_id': 'A', 'created': r.time(2004, 11, 3, 'Z'), 'status': 'accepted', }, { 'id': 'B2', 'entity_id': 'A', 'created': r.time(2005, 11, 3, 'Z'), 'status': 'accepted', }, { 'id': 'C3', 'entity_id': 'B', 'created': r.time(2006, 11, 3, 'Z'), 'status': 'accepted', }]).run(db_conn) set_ = Set.get_latest_accepted(db_conn, 'A') assert set_['id'] == 'B2'
def test_latest_accepted_card(db_conn, cards_table): """ Expect to get the latest accepted card version. """ cards_table.insert([{ 'id': 'A1', 'entity_id': 'A', 'created': r.time(2004, 11, 3, 'Z'), 'status': 'accepted', }, { 'id': 'B2', 'entity_id': 'A', 'created': r.time(2005, 11, 3, 'Z'), 'status': 'accepted', }, { 'id': 'C3', 'entity_id': 'B', 'created': r.time(2006, 11, 3, 'Z'), 'status': 'accepted', }]).run(db_conn) card = Card.get_latest_accepted(db_conn, 'A') assert card['id'] == 'B2'
def test_time_year_month_day_hour_minute_second_tz(self, conn): r.db('unimportant').table('very').update({ 'updated': r.time(2014, 6, 10, 15, 30, 45, 'Z') }).run(conn) result = r.db('unimportant').table('very').get('say_anything').run( conn) update_time = result['updated'] assertEqual(2014, update_time.year) assertEqual(6, update_time.month) assertEqual(10, update_time.day) assertEqual(15, update_time.hour) assertEqual(30, update_time.minute) assertEqual(45, update_time.second) assert (isinstance(update_time.tzinfo, RqlTzinfo))
def to_rql_repr(self, value): if isinstance(value, str): return r.iso8601(value, default_timezone=self.DEFAULT_TIMEZONE).in_timezone(self.timezone) elif isinstance(value, int) or isinstance(value, float): return datetime.fromtimestamp(value).isoformat() elif isinstance(value, dict): return r.time( value.get('year', None), value.get('month', None), value.get('day', None), value.get('hour', None), value.get('minute', None), value.get('second', None), value.get('timezone', self.timezone), ).in_timezone(self.timezone) else: return r.iso8601(value.isoformat(), default_timezone=self.DEFAULT_TIMEZONE).as_timezone(self.timezone)
def get_daily_activity_by_minute(self, cutoff_dt): assert cutoff_dt.tzinfo is not None, \ "Cutoff timestamp must be timezone-aware." cutoff_dt = cutoff_dt.astimezone(pytz.UTC) t = self.get_table(_TABLE_DNS_MESSAGES) rows = t.filter( rethinkdb.row['timestamp'].during( rethinkdb.time( cutoff_dt.year, cutoff_dt.month, cutoff_dt.day, cutoff_dt.hour, cutoff_dt.minute, cutoff_dt.second, 'Z' ), rethinkdb.now() ) )\ .filter( lambda row: row['type'].match('^query') )\ .group([ rethinkdb.row['timestamp'].year(), rethinkdb.row['timestamp'].month(), rethinkdb.row['timestamp'].day(), rethinkdb.row['timestamp'].hours(), rethinkdb.row['timestamp'].minutes(), rethinkdb.row['type'] ])\ .count()\ .run(self.connection) for group, count in rows.items(): (year, month, day, hour, minute, type_) = group yield ((year, month, day, hour, minute), type_, count)
def get_data(): data = [ {'id': 'joe', 'last_updated': r.time(2014, 6, 3, 'Z')}, {'id': 'sam', 'last_updated': r.time(2014, 8, 25, 'Z')} ] return as_db_and_table('d', 'people', data)
def insert_into_contacts(my_connections): i = 0 while (i < len(my_connections) - 1): if (my_connections[i].get('name')): name = (my_connections[i].get('name')) name_list = name.split() first_name = name_list[0] if (len(name_list) > 2): last_name = name_list[1] + " " + name_list[2] elif (len(name_list) == 2): last_name = name_list[1] else: last_name = " " full_name = first_name + " " + last_name db_count = rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").count().run(rethinkDBConnection.connection) if (db_count == 0): count = rethinkdb.table("Contacts").count().run( rethinkDBConnection.connection) count += 1 rethinkdb.table("Contacts").insert({ "first_name": first_name, "last_name": last_name, 'full_name': full_name, 'serial_number': count }).run(rethinkDBConnection.connection) if (my_connections[i].get('birthdate')): date_of_birth = (my_connections[i].get('birthdate')) if date_of_birth.get('year'): year = date_of_birth['year'] if date_of_birth.get('month'): month = date_of_birth['month'] if date_of_birth.get('day'): day = date_of_birth['day'] if (day and month and year): if (rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").has_fields({ 'Contact': { 'date_of_birth': True } }).is_empty().run(rethinkDBConnection.connection)): age = rethinkdb.now().year().run( rethinkDBConnection.connection) - year rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "PersonalInfo": { 'date_of_birth': rethinkdb.time(year, month, day, 'Z').run( rethinkDBConnection.connection), 'age': age } }).run(rethinkDBConnection.connection) db_date = rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname")['PersonalInfo'].get_field( 'date_of_birth')[0].run(rethinkDBConnection.connection) if (db_date.date().year != year or db_date.date().month != month or db_date.date().day != day): age = rethinkdb.now().year().run( rethinkDBConnection.connection) - year rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "PersonalInfo": { 'date_of_birth': rethinkdb.time(year, month, day, 'Z').run( rethinkDBConnection.connection), 'age': age } }).run(rethinkDBConnection.connection) if (my_connections[i].get('numbers')): numbers = my_connections[i].get('numbers') # Add function for numbers using for if (len(numbers) >= 1): phone_number = numbers[0] if (phone_number): if (rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").has_fields({ 'Contact': { 'phone_number': True } }).is_empty().run(rethinkDBConnection.connection)): rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Contact": { 'phone_number': phone_number } }).run(rethinkDBConnection.connection) db_phone_number = rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname")['Contact'].get_field( 'phone_number')[0].run( rethinkDBConnection.connection) if db_phone_number != phone_number: rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Contact": { 'phone_number': phone_number } }).run(rethinkDBConnection.connection) if (len(numbers) == 2): phone_number1 = numbers[1] if (phone_number1): if (rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").has_fields({ 'Contact': { 'phone_number1': True } }).is_empty().run(rethinkDBConnection.connection)): rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Contact": { 'phone_number1': phone_number1 } }).run(rethinkDBConnection.connection) db_phone_number = rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname")['Contact'].get_field( 'phone_number1')[0].run( rethinkDBConnection.connection) if db_phone_number != phone_number1: rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Contact": { 'phone_number1': phone_number1 } }).run(rethinkDBConnection.connection) if (len(numbers) > 2): phone_number2 = numbers[2] if (phone_number2): if (rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").has_fields({ 'Contact': { 'phone_number2': True } }).is_empty().run(rethinkDBConnection.connection)): rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Contact": { 'phone_number2': phone_number2 } }).run(rethinkDBConnection.connection) db_phone_number = rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname")['Contact'].get_field( 'phone_number2')[0].run( rethinkDBConnection.connection) if db_phone_number != phone_number2: rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Contact": { 'phone_number2': phone_number2 } }).run(rethinkDBConnection.connection) if (my_connections[i].get('emails')): emails = my_connections[i].get('emails') if (len(emails) >= 1): email_id = emails[0] if (email_id): if (rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").has_fields({ 'Email': { 'email_id': True } }).is_empty().run(rethinkDBConnection.connection)): rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Email": { 'email_id': email_id } }).run(rethinkDBConnection.connection) db_email_id = rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname")['Email'].get_field( 'email_id')[0].run(rethinkDBConnection.connection) if db_email_id != email_id: rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Email": { 'email_id': email_id } }).run(rethinkDBConnection.connection) if (len(emails) == 2): email_id1 = emails[1] if (email_id1): if (rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").has_fields({ 'Email': { 'email_id1': True } }).is_empty().run(rethinkDBConnection.connection)): rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Email": { 'email_id1': email_id1 } }).run(rethinkDBConnection.connection) db_email_id = rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname")['Email'].get_field( 'email_id1')[0].run(rethinkDBConnection.connection) if db_email_id != email_id1: rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Email": { 'email_id1': email_id1 } }).run(rethinkDBConnection.connection) if (len(emails) > 2): email_id2 = emails[2] if (email_id2): if (rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").has_fields({ 'Email': { 'email_id2': True } }).is_empty().run(rethinkDBConnection.connection)): rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Email": { 'email_id2': email_id2 } }).run(rethinkDBConnection.connection) db_email_id = rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname")['Email'].get_field( 'email_id2')[0].run(rethinkDBConnection.connection) if db_email_id != email_id2: rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Email": { 'email_id2': email_id2 } }).run(rethinkDBConnection.connection) if (my_connections[i].get('address')): address = my_connections[i].get('address') if address['formattedValue'] is not None: str_address = address['formattedValue'] if str_address: if (rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").has_fields({ 'Address': { 'address': True } }).is_empty().run(rethinkDBConnection.connection)): rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Address": { 'address': str_address } }).run(rethinkDBConnection.connection) db_address = rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname")['Address'].get_field( 'address')[0].run(rethinkDBConnection.connection) if db_address != str_address: rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Address": { 'address': str_address } }).run(rethinkDBConnection.connection) if address['city'] is not None: city = address['city'] if city: if (rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").has_fields({ 'Address': { 'address': True } }).is_empty().run(rethinkDBConnection.connection)): rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Address": { 'city': city } }).run(rethinkDBConnection.connection) db_address = rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname")['Address'].get_field('city').run( rethinkDBConnection.connection) if db_address != city: rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Address": { 'city': city } }).run(rethinkDBConnection.connection) if address['country'] is not None: country = address['country'] if country: if (rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").has_fields({ 'Address': { 'country': True } }).is_empty().run(rethinkDBConnection.connection)): rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Address": { 'country': country } }).run(rethinkDBConnection.connection) db_address = rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname")['Address'].get_field( 'country')[0].run(rethinkDBConnection.connection) if db_address != country: rethinkdb.table("Contacts").get_all( [first_name, last_name], index="fullname").update({ "Address": { 'country': country } }).run(rethinkDBConnection.connection) i += 1
def test_filter_during(self, conn): table = r.db('d').table('people') result = table.filter(lambda doc: doc['last_updated'].during( r.time(2014, 6, 1, 'Z'), r.time(2014, 6, 30, 'Z'))).run(conn) result = list(result) assertEqual(2, len(result))
def add_test_set(db_conn, users_table=None, units_table=None, responses_table=None, sets_table=None): """ Add doesn't require anything. Multiply requires add. Subtract requires add. Divide requires multiply. Add is done, Subtract needs review, Multiply needs to be learned, Divide needs to be diagnosed. """ if users_table: users_table.insert({ 'id': 'user' }).run(db_conn) if units_table: units_table.insert([{ 'entity_id': 'add', 'status': 'accepted', 'created': r.now() }, { 'entity_id': 'subtract', 'require_ids': ['add'], 'status': 'accepted', 'created': r.now() }, { 'entity_id': 'multiply', 'require_ids': ['add'], 'status': 'accepted', 'created': r.now() }, { 'entity_id': 'divide', 'require_ids': ['multiply', 'subtract'], 'status': 'accepted', 'created': r.now() }]).run(db_conn) if responses_table: responses_table.insert([{ 'user_id': 'user', 'unit_id': 'add', 'learned': 0.99, 'created': r.now() }, { 'user_id': 'user', 'unit_id': 'multiply', 'learned': 0.0, 'created': r.now() }, { 'user_id': 'user', 'unit_id': 'subtract', 'learned': 0.99, 'created': r.time(2004, 11, 3, 'Z') }]).run(db_conn) if sets_table: sets_table.insert({ 'entity_id': 'set', 'status': 'accepted', 'members': [ {'id': 'add', 'kind': 'unit'}, {'id': 'subtract', 'kind': 'unit'}, {'id': 'multiply', 'kind': 'unit'}, {'id': 'divide', 'kind': 'unit'}, ] }).run(db_conn)
'posts', 'follows', 'notices', 'users_sets', 'responses', ): (r.table(kind) .delete() .run(db_conn)) es.indices.delete(index='entity', ignore=[400, 404]) (r.table('users') .insert([{ 'id': 'doris', 'created': r.time(2014, 1, 1, 'Z'), 'modified': r.time(2014, 1, 1, 'Z'), 'name': 'doris', 'email': '*****@*****.**', 'password': bcrypt.encrypt('example1'), 'settings': { 'email_frequency': 'daily', 'view_sets': 'public', 'view_follows': 'public', } }, { 'id': 'eileen', 'created': r.time(2014, 1, 1, 'Z'), 'modified': r.time(2014, 1, 1, 'Z'), 'name': 'eileen', 'email': '*****@*****.**',
def test_get_card(db_conn, cards_table, cards_parameters_table, units_table, topics_table): """ Expect to get the card information for displaying to a contributor. """ cards_table.insert([{ 'entity_id': 'abcd', 'unit_id': 'zytx', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'kind': 'video', 'requires': ['zxyz'], }, { 'entity_id': 'abcd', 'unit_id': 'zytx', 'created': r.time(1986, 11, 3, 'Z'), 'modified': r.time(1986, 11, 3, 'Z'), 'status': 'accepted', 'kind': 'video', }, { 'entity_id': 'zxyz', 'unit_id': 'zytx', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'kind': 'video', }, { 'entity_id': 'qwer', 'unit_id': 'zytx', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'kind': 'choice', 'requires': ['abcd'], }]).run(db_conn) cards_parameters_table.insert({ 'entity_id': 'abcd', }).run(db_conn) units_table.insert({ 'entity_id': 'zytx', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'name': 'Wildwood', }).run(db_conn) topics_table.insert([{ 'created': r.now(), 'modified': r.now(), 'user_id': 'abcd1234', 'name': 'A Modest Proposal', 'entity': { 'id': 'abcd', 'kind': 'card' } }, { 'created': r.now(), 'modified': r.now(), 'user_id': 'abcd1234', 'name': 'Another Proposal', 'entity': { 'id': 'abcd', 'kind': 'card' } }]).run(db_conn) code, response = routes.card.get_card_route({}, 'abcd') assert code == 200 # Model assert response['card']['entity_id'] == 'abcd' assert response['card']['kind'] == 'video' # Unit assert response['unit']['name'] == 'Wildwood' # Versions assert len(response['versions']) == 2 assert response['versions'][0]['kind'] == 'video' # Topics assert len(response['topics']) == 2 assert response['topics'][0]['entity']['id'] == 'abcd' # Requires assert len(response['requires']) == 1 assert response['requires'][0]['entity_id'] == 'zxyz' # Required By assert len(response['required_by']) == 1 assert response['required_by'][0]['entity_id'] == 'qwer'
def update_with_date_random(db_name,tbl_name, user_object,id): return r.db(db_name).table(tbl_name).get(id).replace( lambda doc: r.branch( (doc == None), doc.merge(doc).merge({'created_at': r.time(random.randrange(1995,2015,1), random.randrange(1,12,1), random.randrange(1,30,1), 'Z')}), doc.merge(doc).merge({'created_at': r.time(random.randrange(1995,2015,1), random.randrange(1,12,1), random.randrange(1,30,1), 'Z')},{'updated_at': r.now()}))).run()
def test_get_set(db_conn, sets_table, units_table, topics_table): """ Expect to get the set information for displaying to a contributor. """ sets_table.insert([{ 'entity_id': 'zytx', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'name': 'Wildwood', 'members': [{ 'kind': 'unit', 'id': 'W' }] }, { 'entity_id': 'zytx', 'created': r.time(1986, 11, 3, 'Z'), 'modified': r.time(1986, 11, 3, 'Z'), 'status': 'accepted', 'name': 'Umberwood', }]).run(db_conn) units_table.insert({ 'entity_id': 'W', 'name': 'Wood', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', }).run(db_conn) topics_table.insert([{ 'created': r.now(), 'modified': r.now(), 'user_id': 'abcd1234', 'name': 'A Modest Proposal', 'entity': { 'id': 'zytx', 'kind': 'set' } }, { 'created': r.now(), 'modified': r.now(), 'user_id': 'abcd1234', 'name': 'Another Proposal', 'entity': { 'id': 'zytx', 'kind': 'set' } }, { 'created': r.now(), 'modified': r.now(), 'user_id': 'abcd1234', 'name': 'A Third Proposal', 'entity': { 'id': 'abcd', 'kind': 'card' } }]).run(db_conn) code, response = routes.set.get_set_route({}, 'zytx') assert code == 200 # Model assert response['set']['entity_id'] == 'zytx' assert response['set']['name'] == 'Wildwood' # Topics assert len(response['topics']) == 2 assert response['topics'][0]['entity']['kind'] == 'set' # Versions assert len(response['versions']) == 2 assert response['versions'][1]['name'] == 'Umberwood' # Units assert len(response['units']) == 1 assert response['units'][0]['entity_id'] == 'W'
def add_test_set(db_conn, users_table=None, units_table=None, responses_table=None, sets_table=None): """ Add doesn't require anything. Multiply requires add. Subtract requires add. Divide requires multiply. Add is done, Subtract needs review, Multiply needs to be learned, Divide needs to be diagnosed. """ if users_table: users_table.insert({'id': 'user'}).run(db_conn) if units_table: units_table.insert([{ 'entity_id': 'add', 'status': 'accepted', 'created': r.now() }, { 'entity_id': 'subtract', 'require_ids': ['add'], 'status': 'accepted', 'created': r.now() }, { 'entity_id': 'multiply', 'require_ids': ['add'], 'status': 'accepted', 'created': r.now() }, { 'entity_id': 'divide', 'require_ids': ['multiply', 'subtract'], 'status': 'accepted', 'created': r.now() }]).run(db_conn) if responses_table: responses_table.insert([{ 'user_id': 'user', 'unit_id': 'add', 'learned': 0.99, 'created': r.now() }, { 'user_id': 'user', 'unit_id': 'multiply', 'learned': 0.0, 'created': r.now() }, { 'user_id': 'user', 'unit_id': 'subtract', 'learned': 0.99, 'created': r.time(2004, 11, 3, 'Z') }]).run(db_conn) if sets_table: sets_table.insert({ 'entity_id': 'set', 'status': 'accepted', 'members': [ { 'id': 'add', 'kind': 'unit' }, { 'id': 'subtract', 'kind': 'unit' }, { 'id': 'multiply', 'kind': 'unit' }, { 'id': 'divide', 'kind': 'unit' }, ] }).run(db_conn)
def test_get_unit(db_conn, units_table, sets_table, topics_table): """ Expect to get the unit information for displaying to a contributor. """ units_table.insert([{ 'entity_id': 'zytx', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'name': 'Wildwood', 'requires': ['ntza'], }, { 'entity_id': 'zytx', 'created': r.time(1986, 11, 3, 'Z'), 'modified': r.time(1986, 11, 3, 'Z'), 'status': 'accepted', 'name': 'Umberwood', }, { 'entity_id': 'ntza', 'created': r.time(1986, 11, 3, 'Z'), 'modified': r.time(1986, 11, 3, 'Z'), 'status': 'accepted', 'name': 'Limberwood', }, { 'entity_id': 'tyui', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'name': 'Wildwood', 'requires': ['zytx'], }]).run(db_conn) sets_table.insert({ 'entity_id': 'W', 'name': 'Woods', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'unit', 'id': 'zytx', }] }).run(db_conn) topics_table.insert([{ 'created': r.now(), 'modified': r.now(), 'user_id': 'abcd1234', 'name': 'A Modest Proposal', 'entity': { 'id': 'zytx', 'kind': 'unit' } }, { 'created': r.now(), 'modified': r.now(), 'user_id': 'abcd1234', 'name': 'Another Proposal', 'entity': { 'id': 'zytx', 'kind': 'unit' } }, { 'created': r.now(), 'modified': r.now(), 'user_id': 'abcd1234', 'name': 'A Third Proposal', 'entity': { 'id': 'abcd', 'kind': 'card' } }]).run(db_conn) code, response = routes.unit.get_unit_route({ 'db_conn': db_conn }, 'zytx') assert code == 200 # Model assert response['unit']['entity_id'] == 'zytx' assert response['unit']['name'] == 'Wildwood' # Topics assert len(response['topics']) == 2 assert response['topics'][0]['entity']['kind'] == 'unit' # Versions assert len(response['versions']) == 2 assert response['versions'][1]['name'] == 'Umberwood' # Requires assert len(response['requires']) == 1 assert response['requires'][0]['entity_id'] == 'ntza' # Required By assert len(response['required_by']) == 1 assert response['required_by'][0]['entity_id'] == 'tyui' # Sets assert len(response['belongs_to']) == 1 assert response['belongs_to'][0]['entity_id'] == 'W'
def test_get_set(db_conn, sets_table, units_table, topics_table): """ Expect to get the set information for displaying to a contributor. """ sets_table.insert([{ 'entity_id': 'zytx', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'name': 'Wildwood', 'members': [{ 'kind': 'unit', 'id': 'W' }] }, { 'entity_id': 'zytx', 'created': r.time(1986, 11, 3, 'Z'), 'modified': r.time(1986, 11, 3, 'Z'), 'status': 'accepted', 'name': 'Umberwood', }]).run(db_conn) units_table.insert({ 'entity_id': 'W', 'name': 'Wood', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', }).run(db_conn) topics_table.insert([{ 'created': r.now(), 'modified': r.now(), 'user_id': 'abcd1234', 'name': 'A Modest Proposal', 'entity': { 'id': 'zytx', 'kind': 'set' } }, { 'created': r.now(), 'modified': r.now(), 'user_id': 'abcd1234', 'name': 'Another Proposal', 'entity': { 'id': 'zytx', 'kind': 'set' } }, { 'created': r.now(), 'modified': r.now(), 'user_id': 'abcd1234', 'name': 'A Third Proposal', 'entity': { 'id': 'abcd', 'kind': 'card' } }]).run(db_conn) code, response = routes.set.get_set_route({ 'db_conn': db_conn }, 'zytx') assert code == 200 # Model assert response['set']['entity_id'] == 'zytx' assert response['set']['name'] == 'Wildwood' # Topics assert len(response['topics']) == 2 assert response['topics'][0]['entity']['kind'] == 'set' # Versions assert len(response['versions']) == 2 assert response['versions'][1]['name'] == 'Umberwood' # Units assert len(response['units']) == 1 assert response['units'][0]['entity_id'] == 'W'
def test_get_card(db_conn, cards_table, cards_parameters_table, units_table, topics_table): """ Expect to get the card information for displaying to a contributor. """ cards_table.insert([{ 'entity_id': 'abcd', 'unit_id': 'zytx', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'kind': 'video', 'requires': ['zxyz'], }, { 'entity_id': 'abcd', 'unit_id': 'zytx', 'created': r.time(1986, 11, 3, 'Z'), 'modified': r.time(1986, 11, 3, 'Z'), 'status': 'accepted', 'kind': 'video', }, { 'entity_id': 'zxyz', 'unit_id': 'zytx', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'kind': 'video', }, { 'entity_id': 'qwer', 'unit_id': 'zytx', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'kind': 'choice', 'requires': ['abcd'], }]).run(db_conn) cards_parameters_table.insert({ 'entity_id': 'abcd', }).run(db_conn) units_table.insert({ 'entity_id': 'zytx', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'name': 'Wildwood', }).run(db_conn) topics_table.insert([{ 'created': r.now(), 'modified': r.now(), 'user_id': 'abcd1234', 'name': 'A Modest Proposal', 'entity': { 'id': 'abcd', 'kind': 'card' } }, { 'created': r.now(), 'modified': r.now(), 'user_id': 'abcd1234', 'name': 'Another Proposal', 'entity': { 'id': 'abcd', 'kind': 'card' } }]).run(db_conn) code, response = routes.card.get_card_route({ 'db_conn': db_conn }, 'abcd') assert code == 200 # Model assert response['card']['entity_id'] == 'abcd' assert response['card']['kind'] == 'video' # Unit assert response['unit']['name'] == 'Wildwood' # Versions assert len(response['versions']) == 2 assert response['versions'][0]['kind'] == 'video' # Topics assert len(response['topics']) == 2 assert response['topics'][0]['entity']['id'] == 'abcd' # Requires assert len(response['requires']) == 1 assert response['requires'][0]['entity_id'] == 'zxyz' # Required By assert len(response['required_by']) == 1 assert response['required_by'][0]['entity_id'] == 'qwer'
'sets', 'sets_parameters', 'topics', 'posts', 'follows', 'notices', 'users_sets', 'responses', ): (database.db.table(kind).delete().run(database.db_conn)) es.indices.delete(index='entity', ignore=[400, 404]) (database.db.table('users').insert([{ 'id': 'doris', 'created': r.time(2014, 1, 1, 'Z'), 'modified': r.time(2014, 1, 1, 'Z'), 'name': 'doris', 'email': '*****@*****.**', 'password': bcrypt.encrypt('example1'), 'settings': { 'email_frequency': 'daily', 'view_sets': 'public', 'view_follows': 'public', } }, { 'id': 'eileen', 'created': r.time(2014, 1, 1, 'Z'), 'modified': r.time(2014, 1, 1, 'Z'), 'name': 'eileen', 'email': '*****@*****.**',
def add_test_set(db_conn, users_table=None, units_table=None, responses_table=None, sets_table=None): """ Add doesn't require anything. Multiply requires add. Subtract requires add. Divide requires multiply. Add is done, Subtract needs review, Multiply needs to be learned, Divide needs to be diagnosed. """ if users_table: users_table.insert({"id": "user"}).run(db_conn) if units_table: units_table.insert( [ {"entity_id": "add", "status": "accepted", "created": r.now()}, {"entity_id": "subtract", "require_ids": ["add"], "status": "accepted", "created": r.now()}, {"entity_id": "multiply", "require_ids": ["add"], "status": "accepted", "created": r.now()}, { "entity_id": "divide", "require_ids": ["multiply", "subtract"], "status": "accepted", "created": r.now(), }, ] ).run(db_conn) if responses_table: responses_table.insert( [ {"user_id": "user", "unit_id": "add", "learned": 0.99, "created": r.now()}, {"user_id": "user", "unit_id": "multiply", "learned": 0.0, "created": r.now()}, {"user_id": "user", "unit_id": "subtract", "learned": 0.99, "created": r.time(2004, 11, 3, "Z")}, ] ).run(db_conn) if sets_table: sets_table.insert( { "entity_id": "set", "status": "accepted", "members": [ {"id": "add", "kind": "unit"}, {"id": "subtract", "kind": "unit"}, {"id": "multiply", "kind": "unit"}, {"id": "divide", "kind": "unit"}, ], } ).run(db_conn)