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_read_non_existent(self, elasticsearch_document_store, elasticsearch_fixture_index, product_fixture_table): identifier = Identifier(elasticsearch_fixture_index, product_fixture_table, uuid4()) read = elasticsearch_document_store.read(identifier) assert read is None
def test_create_and_read(self, product_fixtures, elasticsearch_documents, elasticsearch_document_store, elasticsearch_fixture_index, product_fixture_table): for document in elasticsearch_documents: elasticsearch_document_store.create(document) for product_fixture in product_fixtures: identifier = Identifier(elasticsearch_fixture_index, product_fixture_table, product_fixture.key) read_document = elasticsearch_document_store.read(identifier) assert abs(read_document.timestamp - product_fixture.timestamp) <= 0.001 assert read_document.identifier.key == product_fixture.key assert read_document.get_field_value( "name") == product_fixture.name assert read_document.get_field_value( "description") == product_fixture.description assert read_document.get_field_value( "quantity") == product_fixture.quantity assert read_document.get_field_value( "enabled") == product_fixture.enabled assert read_document.get_field_value("price") == str( product_fixture.price)
def test_set_field_value(self): identifier = Identifier(namespace="test", table="test", key=uuid4()) document = Document(identifier, timestamp=time(), fields=None) document.fields = [Field("one", 1), Field("two", 3)] document.set_field_value("one", 3) document.set_field_value("two", 5) assert_that(document.fields, contains_inanyorder(Field("one", 3), Field("two", 5)))
def test_get_field_value(self): identifier = Identifier(namespace="test", table="test", key=uuid4()) document = Document(identifier, timestamp=time(), fields=None) document.add_field("one", 1) document.add_field("two", 2) document.add_field("three", 3) assert document.get_field_value("one") is 1 assert document.get_field_value("two") is 2 assert document.get_field_value("three") is 3
def test_set_fields(self): identifier = Identifier(namespace="test", table="test", key=uuid4()) document = Document(identifier, timestamp=time(), fields=[Field("foo", "bar")]) assert document.fields == [Field("foo", "bar")] document.fields = [Field("one", 1), Field("two", 2)] assert_that(document.fields, contains_inanyorder(Field("one", 1), Field("two", 2)))
def build_elasticsearch_document(index, _type, product_fixture): document = Document() document.identifier = Identifier(index, _type, product_fixture.id) document.add_field("name", product_fixture.name) document.add_field("description", product_fixture.description) document.add_field("quantity", product_fixture.quantity) document.add_field("price", product_fixture.price) document.add_field("enabled", product_fixture.enabled) document.timestamp = product_fixture.timestamp return document
def test_add_field(self): identifier = Identifier(namespace="test", table="test", key=uuid4()) document = Document(identifier, timestamp=time(), fields=None) document.fields = [Field("one", 1)] document.add_field("two", 2) document.add_field("three", 3) assert_that( document.fields, contains_inanyorder(Field("one", 1), Field("two", 2), Field("three", 3)))
def build_update(namespace, table, key, timestamp, fields=None, is_delete=False): identifier = Identifier(namespace, table, key) return Update(identifier=identifier, timestamp=timestamp, fields=fields, is_delete=is_delete)
def test_serialize_string_as_decimal_if_column_type_is_decimal( self, cassandra_document_store, cassandra_fixture_keyspace, product_fixture_table): document = Document() document.identifier = Identifier(cassandra_fixture_keyspace, product_fixture_table, uuid4()) document.timestamp = time() document.add_field("price", "99.99") cassandra_document_store.create(document) read_document = cassandra_document_store.read(document.identifier) assert read_document.get_field_value("price") == Decimal("99.99")
def to_document(product, keyspace, table): document = Document() document.identifier = Identifier(keyspace, table, product.key) document.timestamp = product.timestamp document.add_field("name", product.name) document.add_field("description", product.description) document.add_field("price", product.price) document.add_field("quantity", product.quantity) document.add_field("enabled", product.enabled) document.add_field("publish_date", product.publish_date) document.add_field("external_id", product.external_id) return document
def test_serialize_string_as_uuid_if_column_type_is_uuid( self, cassandra_document_store, cassandra_fixture_keyspace, product_fixture_table): external_id = uuid4() document = Document() document.identifier = Identifier(cassandra_fixture_keyspace, product_fixture_table, uuid4()) document.timestamp = time() document.add_field("external_id", str(external_id)) cassandra_document_store.create(document) read_document = cassandra_document_store.read(document.identifier) assert read_document.get_field_value("external_id") == external_id
def test_serialize_string_as_datetime_if_column_type_is_datetime( self, cassandra_document_store, cassandra_fixture_keyspace, product_fixture_table): publish_date = datetime.utcnow() document = Document() document.identifier = Identifier(cassandra_fixture_keyspace, product_fixture_table, uuid4()) document.timestamp = time() document.add_field("publish_date", str(publish_date)) cassandra_document_store.create(document) read_document = cassandra_document_store.read(document.identifier) assert DateTimeUtil.are_equal_by_less_than( read_document.get_field_value("publish_date"), publish_date, 0.001)
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_table_has_no_timestamp(self, cassandra_document_store, cassandra_fixture_client, cassandra_fixture_keyspace): cassandra_fixture_client.execute(""" CREATE TABLE table_without_timestamp ( id uuid PRIMARY KEY, whatever text ) """) statement = cassandra_fixture_client.prepare_statement(""" INSERT INTO table_without_timestamp (id,whatever) VALUES (?,?) """) _id = uuid4() whatever = "whatever" cassandra_fixture_client.execute(statement, (_id, whatever)) identifier = Identifier(cassandra_fixture_keyspace, "table_without_timestamp", str(_id)) with pytest.raises(InvalidSchemaException) as e: cassandra_document_store.read(identifier) assert e.value.identifier == identifier assert "No timestamp column found for entity on Cassandra." in e.value.message
def identifier_with_different_key(): return Identifier(namespace="namespace", table="table", key="different_key")
def extract_identifier(cls, response): _id = response["_id"] _type = response["_type"] _index = response["_index"] return Identifier(_index, _type, _id)
def test_read_not_found(self, cassandra_document_store, cassandra_fixture_keyspace, product_fixture_table): identifier = Identifier(cassandra_fixture_keyspace, product_fixture_table, uuid4()) assert not cassandra_document_store.read(identifier)
def test_get_logged_identifier(self): log_entry = CassandraLogEntry(logged_keyspace="test_keyspace", logged_table="test_table", logged_key="test_key") assert log_entry.logged_identifier == Identifier( "test_keyspace", "test_table", "test_key")
def logged_identifier(self): return Identifier(self.logged_keyspace, self.logged_table, self.logged_key)
def identifier(): return Identifier(namespace="namespace", table="table", key="key")