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_read(self, cassandra_document_store, product, product_fixture_cassandra_store, cassandra_fixture_keyspace, product_fixture_table): product_fixture_cassandra_store.create(product) identifier = Identifier(cassandra_fixture_keyspace, product_fixture_table, product.key) document = cassandra_document_store.read(identifier) assert TimestampUtil.are_equal_by_less_than(product.timestamp, document.timestamp, 0.001) assert document.identifier == identifier assert document.get_field_value("name") == product.name assert document.get_field_value("description") == product.description assert document.get_field_value("price") == product.price assert document.get_field_value("quantity") == product.quantity assert document.get_field_value("enabled") == product.enabled assert document.get_field_value("external_id") == product.external_id assert DateTimeUtil.are_equal_by_less_than( document.get_field_value("publish_date"), product.publish_date, 0.001)
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(document): if document.timestamp: return TimestampUtil.seconds_to_milliseconds(document.timestamp) else: return None
def __get_timestamp(cls, document): return TimestampUtil.seconds_to_milliseconds(document.timestamp)
def test_returns_current_timestamp_if_no_updates(self, river): assert TimestampUtil.are_equal_by_less_than(river.propagate_updates(), time(), 1)
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_milliseconds_to_seconds(self): assert TimestampUtil.milliseconds_to_seconds(10345123) == 10345.123
def extract_timestamp(cls, response): if "fields" in response and "_timestamp" in response["fields"]: _timestamp = response["fields"]["_timestamp"] if _timestamp: return TimestampUtil.milliseconds_to_seconds(_timestamp) return None