def test_fail_if_id_is_not_unique(self, cassandra_document_store, cassandra_fixture_client, cassandra_fixture_keyspace): cassandra_fixture_client.execute(""" CREATE TABLE table_with_composite_key ( id uuid, timestamp timestamp, whatever text, PRIMARY KEY (id, timestamp) ) """) statement = cassandra_fixture_client.prepare_statement(""" INSERT INTO table_with_composite_key (id,timestamp,whatever) VALUES (?,?,?) """) _id = uuid4() timestamp1 = TimestampUtil.seconds_to_milliseconds(time()) timestamp2 = TimestampUtil.seconds_to_milliseconds(time() + 1) whatever = "whatever" cassandra_fixture_client.execute(statement, (_id, timestamp1, whatever)) cassandra_fixture_client.execute(statement, (_id, timestamp2, whatever)) identifier = Identifier(cassandra_fixture_keyspace, "table_with_composite_key", str(_id)) with pytest.raises(InvalidSchemaException) as e: cassandra_document_store.read(identifier) assert e.value.identifier == identifier assert "More than one row found for entity on Cassandra." in e.value.message
def test_fail_if_id_is_not_unique(self, cassandra_document_store, cassandra_fixture_client, cassandra_fixture_keyspace): cassandra_fixture_client.execute( """ CREATE TABLE table_with_composite_key ( id uuid, timestamp timestamp, whatever text, PRIMARY KEY (id, timestamp) ) """) statement = cassandra_fixture_client.prepare_statement( """ INSERT INTO table_with_composite_key (id,timestamp,whatever) VALUES (?,?,?) """) _id = uuid4() timestamp1 = TimestampUtil.seconds_to_milliseconds(time()) timestamp2 = TimestampUtil.seconds_to_milliseconds(time() + 1) whatever = "whatever" cassandra_fixture_client.execute(statement, (_id, timestamp1, whatever)) cassandra_fixture_client.execute(statement, (_id, timestamp2, whatever)) identifier = Identifier(cassandra_fixture_keyspace, "table_with_composite_key", str(_id)) with pytest.raises(InvalidSchemaException) as e: cassandra_document_store.read(identifier) assert e.value.identifier == identifier assert "More than one row found for entity on Cassandra." in e.value.message
def test_fail_if_mapping_does_not_have_timestamp_enabled( self, update_applier, elasticsearch_client, elasticsearch_fixture_index): _index = elasticsearch_fixture_index _type = "type_without_timestamp" _id = uuid4() bogus_mapping = {"_timestamp": {"enabled": False}} elasticsearch_client.indices.put_mapping(index=_index, doc_type=_type, body=bogus_mapping) try: elasticsearch_client.index( index=_index, doc_type=_type, id=_id, body={"foo": "bar"}, timestamp=TimestampUtil.seconds_to_milliseconds(time()), refresh=True) update = build_update(namespace=_index, table=_type, key=str(_id), timestamp=time()) with pytest.raises(InvalidSchemaException) as e: update_applier.apply_update(update) assert e.value.identifier == update.identifier assert "Could not retrieve '_timestamp' for Elasticsearch document" in e.value.message finally: elasticsearch_client.indices.delete_mapping( index=elasticsearch_fixture_index, doc_type=_type)
def create(self, product): statement = self.prepare_statement( """ INSERT INTO %s (id, name, quantity, description, price, enabled, external_id, publish_date, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) """ % self.table) self.execute(statement, (product.id, product.name, product.quantity, product.description, product.price, product.enabled, product.external_id, product.publish_date, TimestampUtil.seconds_to_milliseconds(product.timestamp)))
def update(self, product): statement = self.prepare_statement( """ UPDATE %s SET name=?, quantity=?, description=?, price=?, enabled=?, external_id=?, publish_date=?, timestamp=? WHERE id=? """ % self.table) self.execute(statement, (product.name, product.quantity, product.description, product.price, product.enabled, product.external_id, product.publish_date, TimestampUtil.seconds_to_milliseconds(product.timestamp), product.id))
def create(self, product): statement = self.prepare_statement(""" INSERT INTO %s (id, name, quantity, description, price, enabled, external_id, publish_date, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) """ % self.table) self.execute( statement, (product.id, product.name, product.quantity, product.description, product.price, product.enabled, product.external_id, product.publish_date, TimestampUtil.seconds_to_milliseconds(product.timestamp)))
def update(self, product): statement = self.prepare_statement(""" UPDATE %s SET name=?, quantity=?, description=?, price=?, enabled=?, external_id=?, publish_date=?, timestamp=? WHERE id=? """ % self.table) self.execute(statement, (product.name, product.quantity, product.description, product.price, product.enabled, product.external_id, product.publish_date, TimestampUtil.seconds_to_milliseconds( product.timestamp), product.id))
def test_fail_if_mapping_does_not_have_timestamp_enabled(self, update_applier, elasticsearch_client, elasticsearch_fixture_index): _index = elasticsearch_fixture_index _type = "type_without_timestamp" _id = uuid4() bogus_mapping = {"_timestamp": {"enabled": False}} elasticsearch_client.indices.put_mapping(index=_index, doc_type=_type, body=bogus_mapping) try: elasticsearch_client.index(index=_index, doc_type=_type, id=_id, body={"foo": "bar"}, timestamp=TimestampUtil.seconds_to_milliseconds(time()), refresh=True) update = build_update(namespace=_index, table=_type, key=str(_id), timestamp=time()) with pytest.raises(InvalidSchemaException) as e: update_applier.apply_update(update) assert e.value.identifier == update.identifier assert "Could not retrieve '_timestamp' for Elasticsearch document" in e.value.message finally: elasticsearch_client.indices.delete_mapping(index=elasticsearch_fixture_index, doc_type=_type)
def __get_timestamp(cls, document): return TimestampUtil.seconds_to_milliseconds(document.timestamp)
def __filter_by_timestamp_greater_than_or_equal_to(minimum_timestamp): ts = TimestampUtil.seconds_to_milliseconds(minimum_timestamp) return {"filter": {"range": {"_timestamp": {"gte": ts}}}}
def test_seconds_to_milliseconds(self): assert TimestampUtil.seconds_to_milliseconds(10345.123456) == 10345123
def __get_timestamp(document): if document.timestamp: return TimestampUtil.seconds_to_milliseconds(document.timestamp) else: return None