def fix_dates(orig_obj, db_obj, es_obj):
     """Fix the dates for insert."""
     for date_key in ['created', 'updated', 'deleted']:
         if date_key in orig_obj:
             es_obj[date_key] = db_obj[date_key] = datetime_converts(orig_obj[date_key])
     for date_key in ['created', 'updated']:
         if date_key not in orig_obj:
             es_obj[date_key] = db_obj[date_key] = datetime_converts(datetime_now_nomicrosecond())
     if 'deleted' not in orig_obj:
         db_obj['deleted'] = es_obj['deleted'] = None
 def _update(self, update_json, **kwargs):
     """Internal update method for an object."""
     update_hash = loads(update_json)
     if 'updated' not in update_hash:
         update_hash['updated'] = datetime_now_nomicrosecond()
     did_something = False
     for obj in self.select().where(self.where_clause(kwargs)):
         did_something = True
         obj.from_hash(update_hash)
         obj.save()
     if not did_something:
         raise HTTPError(500, "Get args didn't select any objects.")
     complete_objs = [obj.to_hash() for obj in self.select().where(self.where_clause(kwargs))]
     self.elastic_upload(complete_objs)
Beispiel #3
0
    def test_datetime_converts(self):
        """
        The method supports three types of conversions.

        1. a parsable string either unicode or str
        2. a datetime object
        3. an epoch style int
        """
        self.assertEqual(datetime_converts(0), datetime.fromtimestamp(0))
        now_chk = datetime_now_nomicrosecond()
        self.assertEqual(datetime_converts(now_chk), now_chk)
        chk_date = datetime(2016, 7, 5, 9, 22, 12)
        uni_date = u'2016-07-05T09:22:12'
        str_date = '2016-07-05T09:22:12'
        self.assertEqual(datetime_converts(uni_date), chk_date)
        self.assertEqual(datetime_converts(str_date), chk_date)
Beispiel #4
0
 def _update(self, update_json, **kwargs):
     """Internal update method for an object."""
     update_hash = loads(update_json)
     update_hash['updated'] = update_hash.get('updated', datetime_now_nomicrosecond())
     did_something = False
     updated_objs = []
     for obj in self.select().where(self.where_clause(kwargs)):
         did_something = True
         self._update_dep_objs(obj, updated_objs)
         obj.from_hash(update_hash)
         obj.save()
         self._update_dep_objs(obj, updated_objs)
     if not did_something:
         raise HTTPError(500, "Get args didn't select any objects.")
     complete_objs = [obj.to_hash(1) for obj in self.select().where(self.where_clause(kwargs))]
     self.elastic_upload(complete_objs)
     for obj in updated_objs:
         obj.elastic_upload([obj.to_hash(1)])
    def test_methods(self):
        """Test the PUT (insert) method."""
        req = requests.get(
            '{0}/files?page_number=1&items_per_page=1'.format(self.url))
        self.assertEqual(req.status_code, 200)
        files = loads(req.content)
        self.assertEqual(len(files), 1)
        set_hash = {'name': 'Renamed File', 'updated': None}
        req = requests.post('{0}/files'.format(self.url),
                            data=dumps(set_hash), headers=self.headers)
        self.assertEqual(req.status_code, 500)
        del set_hash['updated']
        set_hash['mtime'] = datetime.now().isoformat()
        req = requests.post('{0}/files'.format(self.url),
                            data=dumps(set_hash), headers=self.headers)
        self.assertEqual(req.status_code, 200)
        req = requests.get('{0}/files'.format(self.url))
        self.assertEqual(req.status_code, 200)
        files = loads(req.content)
        self.assertEqual(len(files), 2)
        for file_hash in files:
            self.assertEqual(file_hash['name'], 'Renamed File')

        req = requests.get('{0}/files'.format(self.url))
        self.assertEqual(req.status_code, 200)
        files = loads(req.content)
        self.assertEqual(len(files), 2)

        # update a foreign key to Keys obj that isn't there
        req = requests.post('{0}/file_key_value?file_id=103'.format(self.url),
                            data='{"key_id": 107}', headers=self.headers)
        self.assertEqual(req.status_code, 500)

        req = requests.put('{0}/file_key_value'.format(self.url),
                           data='{"key_id": 107, "file_id": 103, "value_id": 103}',
                           headers=self.headers)
        self.assertEqual(req.status_code, 500)

    # try changing updating something that works
        req = requests.post('{0}/file_key_value?file_id=103&key_id=103&value_id=103'.format(self.url),
                            data='{"key_id": 104, "file_id": 103, "value_id": 103}',
                            headers=self.headers)
        self.assertEqual(req.status_code, 200)

        # just try invalid json
        req = requests.post('{0}/keys'.format(self.url),
                            data='{ some bad json}', headers=self.headers)
        self.assertEqual(req.status_code, 500)
        req = requests.put('{0}/keys'.format(self.url),
                           data='{ some bad json}', headers=self.headers)
        self.assertEqual(req.status_code, 500)

        # insert one item
        req = requests.put('{0}/keys'.format(self.url),
                           data=dumps({
                               '_id': 1, 'key': 'blarg',
                               'created': datetime_now_nomicrosecond().isoformat()
                           }),
                           headers=self.headers)
        self.assertEqual(req.status_code, 200)

        # try inserting empty array
        req = requests.put('{0}/keys'.format(self.url),
                           data=dumps([]),
                           headers=self.headers)
        self.assertEqual(req.status_code, 200)

        # try to insert the same item again
        req = requests.put('{0}/keys'.format(self.url),
                           data=dumps({'_id': 1, 'key': 'blarg'}), headers=self.headers)
        self.assertEqual(req.status_code, 400)

        req = requests.post('{0}/keys?_id=107'.format(self.url),
                            data='{"_id": 142}', headers=self.headers)
        self.assertEqual(req.status_code, 500)

        # delete the item I just put in
        req = requests.delete('{0}/keys?key=blarg'.format(self.url))
        self.assertEqual(req.status_code, 200)
Beispiel #6
0
    def _update(self, update_json, **kwargs):
        """
        Internal update method for an object.
        """
        print kwargs
        try:
            objs = self.select().where(self.where_clause(kwargs))
        except DoesNotExist, ex:
            raise HTTPError(404, str(ex))
        for obj in objs:
            try:
                obj.from_json(update_json)
            except ValueError, ex:
                raise HTTPError(500, str(ex))
            obj.updated = datetime_now_nomicrosecond()
            try:
                self.elastic_upload(obj)
            except Exception, ex:
                raise HTTPError(500, str(ex))
            try:
                obj.save()
            except IntegrityError, ex:
                obj.rollback()
                raise HTTPError(500, str(ex))

    def _set_or_create(self, insert_json):
        """
        Set or create the object if it doesn't already exist
        """
        objs = None
Beispiel #7
0
# -*- coding: utf-8 -*-
"""Test the files ORM object."""
from datetime import datetime, timedelta
from json import dumps
from metadata.orm.utils import datetime_now_nomicrosecond
from metadata.orm.test.base import TestBase
from metadata.orm.files import Files
from metadata.orm.transactions import Transactions
from metadata.orm.test.test_transactions import SAMPLE_TRANSACTION_HASH, TestTransactions

SAMPLE_FILE_HASH = {
    '_id': 127,
    'name': 'test.txt',
    'subdir': 'a/b',
    'mimetype': 'text/plain',
    'mtime': datetime_now_nomicrosecond().isoformat(),
    'ctime': datetime_now_nomicrosecond().isoformat(),
    'hashtype': 'sha1',
    'hashsum': 'd8ff327b2f643130b431ae7c1f1b1e191bc419af',
    'size': 1234,
    'transaction_id': SAMPLE_TRANSACTION_HASH['_id'],
    'encoding': 'UTF8'
}

SAMPLE_UNICODE_FILE_HASH = {
    '_id': 127,
    'name': u'abcdé.txt',
    'subdir': u'abcdé/b',
    'mimetype': 'text/plain',
    'mtime': datetime_now_nomicrosecond().isoformat(),
    'ctime': datetime_now_nomicrosecond().isoformat(),
from time import mktime
from json import dumps
from peewee import SqliteDatabase
from playhouse.test_utils import test_database
from metadata.orm.utils import datetime_now_nomicrosecond
from metadata.orm.base import PacificaModel
from metadata.orm.test.base import TestBase

SAMPLE_ZERO_ISO_HASH = {
    'created': '1969-12-31T16:00:00',
    'updated': '1969-12-31T16:00:00',
    'deleted': '1969-12-31T16:00:00'
}
SAMPLE_ZERO_HASH = {'created': 0, 'updated': 0, 'deleted': None}
SAMPLE_REP_HASH = {
    'created': datetime_now_nomicrosecond().isoformat(),
    'updated': datetime_now_nomicrosecond().isoformat(),
    'deleted': None
}
SAMPLE_BAD_HASH = {
    'created': 'blarg',
    'updated': int(mktime(datetime.utcnow().timetuple())),
    'deleted': None
}


class TestDBDates(TestBase):
    """Setup the test cases for the base object attributes for the ORM."""

    obj_cls = PacificaModel
    # pylint: disable=no-member
from datetime import datetime
from json import dumps
from metadata.orm.utils import datetime_now_nomicrosecond
from metadata.orm.test.base import TestBase
from metadata.orm.proposals import Proposals

SAMPLE_PROPOSAL_HASH = {
    '_id': '17a',
    'title': 'My Proposal Title',
    'abstract': """
This is my proposal that's really cool and you should accept it. ;)
""",
    'science_theme': 'Nobel Prize Winners',
    'proposal_type': 'Blarg!',
    'encoding': 'UTF8',
    'submitted_date': datetime_now_nomicrosecond().isoformat(),
    'accepted_date': datetime.utcnow().date().isoformat(),
    'actual_start_date': datetime.utcnow().date().isoformat(),
    'actual_end_date': datetime.utcnow().date().isoformat(),
    'closed_date': datetime.utcnow().date().isoformat()
}

SAMPLE_UNICODE_PROPOSAL_HASH = {
    '_id': u'17é',
    'title': u'My Proposal Titlé',
    'abstract': u"""
This is my proposal that's réally cool and you should accept it. ;)
""",
    'science_theme': u'Nobél Prize Winners',
    'proposal_type': u'Blarg!é',
    'encoding': 'UTF8',
 def _delete(self, **kwargs):
     """Internal delete object method."""
     update_hash = {
         'deleted': datetime_now_nomicrosecond().isoformat()
     }
     self._update(dumps(update_hash), **kwargs)