def datoms(self, dbname, basis_t='-', index='eavt', **kwargs): '''Returns a set or range of datoms from an index''' options = { 'e': 'e', 'a': 'a', 'v': 'v', 'start': 'start', 'end': 'end', 'offset': 'offset', 'limit': 'limit', 'as_of_t': 'as-of', 'since_t': 'since', 'history': 'history', } get_args = {'index': index} for k, v in kwargs.iteritems(): if k not in options: raise ValueError("Unknown option '{}'".format(k)) get_args[options[k]] = v url = '{}/{}/datoms'.format(self.db_url(dbname), basis_t) r = requests.get(url, params=get_args, headers={'Accept': 'application/edn'}) assert r.status_code == 200 return loads(r.content)
def transact(self, dbname, data): data = '[%s\n]' % '\n'.join(data) r = requests.post(self.db_url(dbname) + '/', data={'tx-data': data}, headers={'Accept': 'application/edn'}) assert r.status_code in (200, 201), (r.status_code, r.text) return loads(r.content)
def query(self, dbname, query, extra_args=[], history=False): args = '[{:db/alias ' + self.storage + '/' + dbname if history: args += ' :history true' args += '} ' + ' '.join(str(a) for a in extra_args) + ']' r = requests.get(urljoin(self.location, 'api/query'), params={'args': args, 'q':query}, headers={'Accept':'application/edn'}) assert r.status_code == 200, r.text return loads(r.content)
def test_all_data(self): data = { '"helloworld"': "helloworld", "23": 23, "23.11": 23.11, "true": True, "false": False, "nil": None, ":hello": ":hello", r'"string\"ing"': 'string"ing', '"string\n"': 'string\n', '[:hello]':(":hello",), '-10.4':-10.4, '"你"': u'你', '\\€': u'€', "[1 2]": (1, 2), "#{true \"hello\" 12}": set([True, "hello", 12]), '#inst "2012-09-10T23:51:55.840-00:00"': datetime(2012, 9, 10, 23, 51, 55, 840000), "(\\a \\b \\c \\d)": ("a", "b", "c", "d"), "{:a 1 :b 2 :c 3 :d 4}": {":a":1, ":b":2, ":c":3,":d":4}, "[1 2 3,4]": (1,2,3,4), "{:a [1 2 3] :b #{23.1 43.1 33.1}}": {":a":(1, 2, 3), ":b":frozenset([23.1, 43.1, 33.1])}, "{:a 1 :b [32 32 43] :c 4}": {":a":1, ":b":(32,32,43), ":c":4}, "\\你": u"你", '#db/fn{:lang "clojure" :code "(map l)"}': {':lang':u'clojure', ':code':u'(map l)'}, "#_ {[#{}] #{[]}} [23[34][32][4]]": (23, (34,), (32,), (4,)), '(:graham/stratton true \n , "A string with \\n \\"s" true #uuid "f81d4fae7dec11d0a76500a0c91e6bf6")': ( u':graham/stratton', True, u'A string with \n "s', True, UUID('f81d4fae-7dec-11d0-a765-00a0c91e6bf6') ), '[\space \\\xE2\x82\xAC [true []] ;true\n[true #inst "2012-09-10T23:39:43.309-00:00" true ""]]': ( ' ', u'\u20ac', (True, ()), (True, datetime(2012, 9, 10, 23, 39, 43, 309000), True, '') ), ' {true false nil [true, ()] 6 {#{nil false} {nil \\newline} }}': { None: (True, ()), True: False, 6: {frozenset([False, None]): {None: '\n'}} }, '[#{6.22e-18, -3.1415, 1} true #graham #{"pie" "chips"} "work"]': ( frozenset([6.22e-18, -3.1415, 1]), True, u'work' ), '(\\a .5)': (u'a', 0.5), '(List #{[123 456 {}] {a 1 b 2 c ({}, [])}})': ( u'List', ((123, 456, {}), {u'a': 1, u'c': ({}, ()), u'b': 2}) ), } for k, v in data.items(): self.assertEqual(edn.loads(k), v)
def datoms(self, dbname, index, components, **kwargs): assert index in ('eavt', 'aevt', 'avet', 'vaet') t = kwargs.get('t', '-') offset = kwargs.get('offset', None) limit = kwargs.get('limit', None) history = kwargs.get('history', None) asof = kwargs.get('as_of', None) since = kwargs.get('since', None) params = {'index': index, 'components': '[{0}]'.format(' '.join(str(a) for a in components))} if offset is not None: params['offset'] = str(offset) if limit is not None: params['limit'] = str(limit) if history: params['history'] = 'true' if asof is not None: params['as-of'] = str(asof) if since is not None: params['since'] = str(since) r = requests.get(self.db_url(dbname) + '/' + t + '/datoms', params=params, headers={'Accept': 'application/edn'}) assert r.status_code == 200 return loads(r.content)
def entity(self, dbname, eid): r = requests.get(self.db_url(dbname) + '/-/entity', params={'e':eid}, headers={'Accept':'application/edn'}) assert r.status_code == 200 return loads(r.content)
def transact(self, dbname, data): data = '[%s\n]' % '\n'.join(data) r = requests.post(self.db_url(dbname)+'/', data={'tx-data':data}, headers={'Accept':'application/edn'}) assert r.status_code in (200, 201), (r.status_code, r.text) return loads(r.content)
def test_all_data(self): data = { '"helloworld"': "helloworld", "23": 23, "23.11": 23.11, "true": True, "false": False, "nil": None, ":hello": ":hello", r'"string\"ing"': 'string"ing', '"string\n"': 'string\n', '[:hello]': (":hello", ), '-10.4': -10.4, '"你"': u'你', '\\€': u'€', "[1 2]": (1, 2), "#{true \"hello\" 12}": set([True, "hello", 12]), '#inst "2012-09-10T23:51:55.840-00:00"': datetime(2012, 9, 10, 23, 51, 55, 840000), "(\\a \\b \\c \\d)": ("a", "b", "c", "d"), "{:a 1 :b 2 :c 3 :d 4}": { ":a": 1, ":b": 2, ":c": 3, ":d": 4 }, "[1 2 3,4]": (1, 2, 3, 4), "{:a [1 2 3] :b #{23.1 43.1 33.1}}": { ":a": (1, 2, 3), ":b": frozenset([23.1, 43.1, 33.1]) }, "{:a 1 :b [32 32 43] :c 4}": { ":a": 1, ":b": (32, 32, 43), ":c": 4 }, "\\你": u"你", '#db/fn{:lang "clojure" :code "(map l)"}': { ':lang': u'clojure', ':code': u'(map l)' }, "#_ {[#{}] #{[]}} [23[34][32][4]]": (23, (34, ), (32, ), (4, )), '(:graham/stratton true \n , "A string with \\n \\"s" true #uuid "f81d4fae7dec11d0a76500a0c91e6bf6")': (u':graham/stratton', True, u'A string with \n "s', True, UUID('f81d4fae-7dec-11d0-a765-00a0c91e6bf6')), '[\space \\\xE2\x82\xAC [true []] ;true\n[true #inst "2012-09-10T23:39:43.309-00:00" true ""]]': (' ', u'\u20ac', (True, ()), (True, datetime(2012, 9, 10, 23, 39, 43, 309000), True, '')), ' {true false nil [true, ()] 6 {#{nil false} {nil \\newline} }}': { None: (True, ()), True: False, 6: { frozenset([False, None]): { None: '\n' } } }, '[#{6.22e-18, -3.1415, 1} true #graham #{"pie" "chips"} "work"]': (frozenset([6.22e-18, -3.1415, 1]), True, u'work'), '(\\a .5)': (u'a', 0.5), '(List #{[123 456 {}] {a 1 b 2 c ({}, [])}})': (u'List', ((123, 456, {}), { u'a': 1, u'c': ({}, ()), u'b': 2 })), } for k, v in data.items(): self.assertEqual(edn.loads(k), v)
def test_all_data(): for (k, v) in test_data.items(): assert edn.loads(k) == v, 'Failed to load {}'.format(k)
def test_malformed_data(): '''Verify ValueError() exception raise on malformed data''' data = ['[1 2 3', '@EE', '[@nil tee]'] for d in data: with pytest.raises(ValueError): edn.loads(d)