class_docstr: Tests ensuring that when a dataset or samples in a dataset are modified,
  those changes are passed on the the database and can be seen in different
  scopes (or processes!).
class_docstr_tok: ["Tests","ensuring","that","when","a","dataset","or","samples","in","a","dataset","are","modified",",","those","changes","are","passed","on","the","the","database","and","can","be","seen","in","different","scopes","(","or","processes","!",")","."]
code: def test_add_delete_sample(self):
      dataset_name = self.test_add_delete_sample.__name__
  
      def create_dataset():
          dataset = fo.Dataset(dataset_name)
  
      create_dataset()
  
      filepath = "test1.png"
  
      # add sample
  
      def add_sample():
          dataset = fo.load_dataset(dataset_name)
          sample = fo.Sample(filepath=filepath)
          return dataset.add_sample(sample)
  
      def check_add_sample(sample_id):
          dataset = fo.load_dataset(dataset_name)
          self.assertEqual(len(dataset), 1)
          sample = dataset[sample_id]
          self.assertTrue(sample.in_dataset)
          self.assertIsNotNone(sample.id)
          self.assertIs(sample.dataset, dataset)
  
      sample_id = add_sample()
      check_add_sample(sample_id)
  
      # remove sample
  
      def delete_sample(sample_id):
          dataset = fo.load_dataset(dataset_name)
          sample = dataset[sample_id]
          dataset.delete_samples(sample)
  
      def check_delete_sample(sample_id):
          dataset = fo.load_dataset(dataset_name)
          self.assertEqual(len(dataset), 0)
          with self.assertRaises(KeyError):
              dataset[sample_id]
  
      delete_sample(sample_id)
      check_delete_sample(sample_id)
  
      # add multiple samples
  
      filepath_template = "test_multi%d.png"
      num_samples = 10
  
      def add_samples():
          dataset = fo.load_dataset(dataset_name)
          samples = [
              fo.Sample(filepath=filepath_template % i)
              for i in range(num_samples)
          ]
          return dataset.add_samples(samples)
  
      def check_add_samples(sample_ids):
          dataset = fo.load_dataset(dataset_name)
          self.assertEqual(len(dataset), num_samples)
          for sample_id in sample_ids:
              sample = dataset[sample_id]
              self.assertTrue(sample.in_dataset)
              self.assertIsNotNone(sample.id)
              self.assertIs(sample.dataset, dataset)
  
      sample_ids = add_samples()
      check_add_samples(sample_ids)
  
      # delete some
  
      num_delete = 7
  
      def delete_samples(sample_ids):
          dataset = fo.load_dataset(dataset_name)
          dataset.delete_samples(sample_ids[:num_delete])
  
      def check_delete_samples(sample_ids):
          dataset = fo.load_dataset(dataset_name)
          self.assertEqual(len(dataset), num_samples - num_delete)
  
          for i, sample_id in enumerate(sample_ids):
              if i < num_delete:
                  with self.assertRaises(KeyError):
                      dataset[sample_id]
              else:
                  sample = dataset[sample_id]
                  self.assertTrue(sample.in_dataset)
                  self.assertIsNotNone(sample.id)
                  self.assertIs(sample.dataset, dataset)
  
      delete_samples(sample_ids)
      check_delete_samples(sample_ids)
  
      # clear dataset
  
      def clear_dataset():
          dataset = fo.load_dataset(dataset_name)
          dataset.clear()
  
      def check_clear_dataset(sample_ids):
          dataset = fo.load_dataset(dataset_name)
          self.assertEqual(len(dataset), 0)
  
          for sample_id in sample_ids:
              with self.assertRaises(KeyError):
                  dataset[sample_id]
  
      clear_dataset()
      check_clear_dataset(sample_ids)
  
file_path: fiftyone/tests/unittests/synchronization_tests.py
imports: import os
  import unittest
  import fiftyone
  from decorators import drop_datasets
label_desc: Use image node_id 2 for calling the ScopedObjectsSynchronizationTests obj's underlying member method code with example usage: obj.test_add_delete_sample() and returns: dataset, dataset