def test_should_create_new_space(self, easydb_client):
        # when
        space = easydb_client.create_space()

        # then
        self.assertIsNotNone(space)

        # and
        self.assertTrue(space.name)
    def test_should_remove_space(self, easydb_client):
        # given
        created_space = easydb_client.create_space()

        # when
        removed = easydb_client.remove_space(created_space.name)

        # then
        self.assertIsNone(removed)
    def test_should_throw_error_when_passing_invalid_element_to_add(
            self, easydb_client):
        # given
        space = easydb_client.create_space()

        # and
        bucket = space.get_bucket(BUCKET_NAME)

        with self.assertRaises(easydb_client.InvalidElementFormat):  # then
            bucket.add({'fieldWithInvalidValue': []})  # when
    def test_should_throw_error_when_trying_to_remove_nonexistent_element(
            self, easydb_client):
        # given
        space = easydb_client.create_space()

        # and
        bucket = space.get_bucket(BUCKET_NAME)

        with self.assertRaises(easydb_client.ElementNotFound):  # then
            bucket.remove(BUCKET_ELEMENT_ID)  # when
    def test_should_remove_element_from_bucket(self, easydb_client):
        # given
        space = easydb_client.create_space()

        # and
        bucket = space.get_bucket(BUCKET_NAME)

        # and
        saved_element = bucket.add({'firstName': 'John'})

        # when then
        self.assertIsNone(bucket.remove(saved_element['id']))
    def test_should_get_space(self, easydb_client):
        # given
        created_space = easydb_client.create_space()

        # when
        space = easydb_client.get_space(created_space.name)

        # then
        self.assertIsNotNone(space)

        # and
        self.assertEqual(space.name, created_space.name)
    def test_should_tell_if_space_exists(self, easydb_client):
        # given
        created_space = easydb_client.create_space()

        # when
        exists = easydb_client.space_exists(created_space.name)

        # then
        self.assertTrue(exists)

        # when
        exists = easydb_client.space_exists('nonexistent')

        # then
        self.assertFalse(exists)
    def test_should_get_all_elements_from_bucket(self, easydb_client):
        # given
        space = easydb_client.create_space()

        # and
        bucket = space.get_bucket(BUCKET_NAME)

        # and
        bucket.add({'firstName': 'John'})
        bucket.add({'firstName': 'Mark'})

        # when
        elements = list(bucket.all())

        # then
        self.assertEqual(len(elements), 2)
    def test_should_throw_error_when_passing_invalid_query_to_filter(
            self, easydb_client):
        # given
        space = easydb_client.create_space()

        # and
        bucket = space.get_bucket(BUCKET_NAME)

        # and
        bucket.add({'firstName': 'John'})

        # and
        first_name_eq_mark = easydb_client.query.where('firstName').eq('Mark')
        last_name_eq_smith = easydb_client.query.where('lastName').eq(None)

        with self.assertRaises(easydb_client.query.InvalidQuery):  # then
            list(bucket.filter(first_name_eq_mark
                               & last_name_eq_smith))  # when
    def test_should_add_element_to_bucket(self, easydb_client):
        # given
        space = easydb_client.create_space()

        # and
        bucket = space.get_bucket(BUCKET_NAME)

        # when
        saved_element = bucket.add({'firstName': 'John'})

        # then
        self.assertEqual(saved_element['fields']['firstName'], 'John')

        # and
        self.assertIsInstance(saved_element['id'], str)

        # and
        self.assertEqual(saved_element['bucketName'], BUCKET_NAME)
    def test_should_get_element_from_bucket(self, easydb_client):
        # given
        space = easydb_client.create_space()

        # and
        bucket = space.get_bucket(BUCKET_NAME)

        # and
        saved_element = bucket.add({'firstName': 'John'})

        # when
        element = bucket.get(saved_element['id'])

        # then
        self.assertEqual(
            element, {
                'id': saved_element['id'],
                'bucketName': BUCKET_NAME,
                'fields': {
                    'firstName': 'John'
                }
            })
    def test_should_get_elements_filtered_by_multiple_fields_from_bucket(
            self, easydb_client):
        # given
        space = easydb_client.create_space()

        # and
        bucket = space.get_bucket(BUCKET_NAME)

        # and
        bucket.add({'firstName': 'John'})
        bucket.add({'firstName': 'Mark', 'lastName': 'Smith'})
        bucket.add({'firstName': 'Mark', 'lastName': 'Robinson'})

        # and
        first_name_eq_mark = easydb_client.query.where('firstName').eq('Mark')
        last_name_eq_smith = easydb_client.query.where('lastName').eq('Smith')

        # when
        elements = list(bucket.filter(first_name_eq_mark & last_name_eq_smith))

        # then
        self.assertEqual(len(elements), 1)
    def test_should_update_element_in_bucket(self, easydb_client):
        # given
        space = easydb_client.create_space()

        # and
        bucket = space.get_bucket(BUCKET_NAME)

        # and
        saved_element = bucket.add({'firstName': 'John'})

        # when
        updated_element = bucket.update(saved_element['id'],
                                        {'firstName': 'John'})

        # then
        self.assertEqual(updated_element['fields']['firstName'], 'John')

        # and
        self.assertEqual(updated_element['id'], saved_element['id'])

        # and
        self.assertEqual(updated_element['bucketName'], BUCKET_NAME)
    def test_should_get_elements_filtered_by_single_field_from_bucket(
            self, easydb_client):
        # given
        space = easydb_client.create_space()

        # and
        bucket = space.get_bucket(BUCKET_NAME)

        # and
        bucket.add({'firstName': 'John'})
        bucket.add({'firstName': 'Mark', 'lastName': 'Smith'})
        bucket.add({
            'firstName': 'Mark',
            'lastName': 'Robinson',
            'alias': 'meh'
        })

        # when
        elements = list(
            bucket.filter(easydb_client.query.where('alias').eq('meh')))

        # then
        self.assertEqual(len(elements), 1)