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 _to_document(self, identifier, rows): if len(rows) == 0: return None elif len(rows) > 1: raise InvalidSchemaException(identifier=identifier, message=("More than one row found for entity on Cassandra. " + "Please make sure the schema has a single primary key column with name '%s'. ") % self._id_column_name) row = rows[0] if not hasattr(row, self._timestamp_column_name): raise InvalidSchemaException(identifier=identifier, message=("No timestamp column found for entity on Cassandra. " + "Please make sure the schema has a timestamp column with name '%s'. ") % self._timestamp_column_name) document = Document() document.identifier = identifier timestamp = attrgetter(self._timestamp_column_name)(row) if timestamp: document.timestamp = arrow.get(timestamp).float_timestamp for field_name in row.__dict__: if field_name != self._timestamp_column_name and field_name != self._id_column_name: field_value = attrgetter(field_name)(row) document.add_field(field_name, field_value) return document
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 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 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_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_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 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_document(identifier, timestamp, source): if not timestamp: raise InvalidSchemaException( identifier=identifier, message= "Could not retrieve '_timestamp' for Elasticsearch document. Please check your mappings." ) fields = [] for (field_name, field_value) in source.items(): fields.append(Field(field_name, field_value)) return Document(identifier, timestamp, fields)
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 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 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 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