def test_can_create_new_database(self):
     # don't use db from setUp(); create new in-memory DB from scratch
     dbnew = ProjectDB()
     dbnew.createDB(":memory:")
     self.assertTrue(dbnew.isOpened())
     self.assertFalse(dbnew.isInitialized())
     dbnew.closeDB()
 def test_that_closed_db_reports_as_uninitialized(self):
     # don't use db from setUp(); create new in-memory DB from scratch
     dbnew = ProjectDB()
     dbnew.createDB(":memory:")
     # and then close it
     dbnew.closeDB()
     self.assertFalse(dbnew.isInitialized())
     self.assertIsNone(dbnew.session)
     self.assertIsNone(dbnew.engine)
 def test_can_open_existing_db(self):
     # create in temporary directory on disk, so we can re-open DB
     # (testfixtures will wipe out the directory at end of test)
     with TempDirectory() as td:
         dbPath = os.path.join(td.path, "tmp.db")
         dbnew = ProjectDB()
         dbnew.createDB(dbPath)
         dbnew.initializeDBTables()
         dbnew.closeDB()
         # and reopen it
         dbnew.openDB(dbPath)
         self.assertTrue(dbnew.isOpened())
         self.assertTrue(dbnew.isInitialized())
         dbnew.closeDB()
    def test_open_db_fails_if_invalid_magic_number(self):
        # create in temporary directory on disk, so we can re-open it
        # (testfixtures will wipe out the directory at end of test)
        with TempDirectory() as td:
            dbPath = os.path.join(td.path, "tmp.db")
            dbnew = ProjectDB()
            dbnew.createDB(dbPath)
            dbnew.initializeDBTables()

            # set invalid magic number
            query = dbnew.session.query(Config).filter(Config.key == "magic")
            query.update({Config.value: "invalidMagic"})
            dbnew.session.commit()
            dbnew.closeDB()

            # and reopen it
            with self.assertRaises(ProjectDBConfigError):
                dbnew.openDB(dbPath)
            self.assertFalse(dbnew.isOpened())
            self.assertFalse(dbnew.isInitialized())
Esempio n. 5
0
class DBConfigUnitTestSuite(unittest.TestCase):
    """spdxLicenseManager unit test suite for configuration data in DB."""
    def setUp(self):
        # create and initialize an in-memory database
        self.db = ProjectDB()
        self.db.createDB(":memory:")
        self.db.initializeDBTables()

        # insert sample data
        self.insertSampleConfigData()

    def tearDown(self):
        self.db.closeDB()
        self.db = None

    def insertSampleConfigData(self):
        configs = [
            Config(key="report-strip-licenseref", value="yes"),
        ]
        self.db.session.bulk_save_objects(configs)
        self.db.session.commit()

    ##### Test cases below

    def test_can_retrieve_config_value_from_key(self):
        value = self.db.getConfigValue(key="report-strip-licenseref")
        self.assertEqual(value, "yes")

    def test_cannot_retrieve_config_value_for_unknown_key(self):
        with self.assertRaises(ProjectDBQueryError):
            self.db.getConfigValue(key="unknown_key")

    def test_can_set_and_get_config_value_for_new_key_if_known(self):
        self.db.setConfigValue(key="analyze-extensions-list", value="json")
        value = self.db.getConfigValue(key="analyze-extensions-list")
        self.assertEqual(value, "json")

    def test_can_update_and_get_config_value_for_existing_key(self):
        self.db.setConfigValue(key="report-strip-licenseref", value="no")
        value = self.db.getConfigValue(key="report-strip-licenseref")
        self.assertEqual(value, "no")

    def test_cannot_update_config_value_for_reserved_keys(self):
        with self.assertRaises(ProjectDBUpdateError):
            self.db.setConfigValue(key="magic", value="blah")
        with self.assertRaises(ProjectDBUpdateError):
            self.db.setConfigValue(key="initialized", value="blah")

    def test_cannot_set_unknown_key(self):
        with self.assertRaises(ProjectDBInsertError):
            self.db.setConfigValue(key="new_key", value="123abc")

    def test_can_get_all_configs(self):
        configs = self.db.getConfigsAll()
        self.assertIsInstance(configs, list)
        self.assertEqual(len(configs), 3)
        self.assertEqual(configs[0].key, "initialized")
        self.assertEqual(configs[0].value, "yes")
        self.assertEqual(configs[1].key, "magic")
        self.assertEqual(configs[1].value, "spdxLicenseManager")
        self.assertEqual(configs[2].key, "report-strip-licenseref")
        self.assertEqual(configs[2].value, "yes")

    def test_can_unset_config(self):
        self.db.unsetConfigValue(key="report-strip-licenseref")
        with self.assertRaises(ProjectDBQueryError):
            self.db.getConfigValue(key="report-strip-licenseref")

    def test_cannot_unset_config_for_internal_config(self):
        with self.assertRaises(ProjectDBDeleteError):
            self.db.unsetConfigValue(key="magic")

    def test_cannot_unset_config_for_unknown_config(self):
        with self.assertRaises(ProjectDBDeleteError):
            self.db.unsetConfigValue(key="blah")

    def test_cannot_unset_config_for_valid_config_that_is_not_set(self):
        with self.assertRaises(ProjectDBDeleteError):
            self.db.unsetConfigValue(key="analyze-extensions-list")
class DBConversionUnitTestSuite(unittest.TestCase):
  """spdxLicenseManager unit test suite for converting license names in DB."""

  def setUp(self):
    # create and initialize an in-memory database
    self.db = ProjectDB()
    self.db.createDB(":memory:")
    self.db.initializeDBTables()

    # insert sample data
    self.insertSampleCategoryData()
    self.insertSampleLicenseData()
    self.insertSampleConversionData()

  def tearDown(self):
    self.db.closeDB()
    self.db = None

  def insertSampleCategoryData(self):
    categories = [
      Category(_id=1, name="a category", order=3),
      Category(_id=2, name="cat", order=2),
      Category(_id=3, name="blah category", order=1),
    ]
    self.db.session.bulk_save_objects(categories)
    self.db.session.commit()

  def insertSampleLicenseData(self):
    licenses = [
      License(_id=1, name="DoAnything", category_id=1),
      License(_id=2, name="HarshEULA", category_id=2),
      License(_id=3, name="293PageEULA", category_id=3),
      License(_id=4, name="DoAnythingNoncommercial", category_id=1),
    ]
    self.db.session.bulk_save_objects(licenses)
    self.db.session.commit()

  def insertSampleConversionData(self):
    conversions = [
      Conversion(_id=1, old_text="293", new_license_id=3),
      Conversion(_id=2, old_text="NC", new_license_id=4),
      Conversion(_id=3, old_text="anything", new_license_id=1),
      Conversion(_id=4, old_text="Anything", new_license_id=1),
    ]
    self.db.session.bulk_save_objects(conversions)
    self.db.session.commit()

  ##### Test cases below

  def test_can_retrieve_one_conversion_by_id(self):
    conversion = self.db.getConversion(_id=2)
    self.assertEqual(conversion.old_text, "NC")
    self.assertEqual(conversion.new_license_id, 4)
    self.assertEqual(conversion.new_license._id, 4)
    self.assertEqual(conversion.new_license.name, "DoAnythingNoncommercial")

  def test_can_retrieve_one_conversion_by_name(self):
    conversion = self.db.getConversion(old_text="293")
    self.assertEqual(conversion._id, 1)

  def test_cannot_retrieve_conversion_by_both_name_and_id(self):
    with self.assertRaises(ProjectDBQueryError):
      self.db.getConversion(_id=3, old_text="anything")

  def test_cannot_retrieve_conversion_without_either_name_or_id(self):
    with self.assertRaises(ProjectDBQueryError):
      self.db.getConversion()

  def test_cannot_retrieve_conversion_with_positional_args(self):
    with self.assertRaises(TypeError):
      self.db.getConversion("NC")

  def test_returns_none_if_conversion_not_found_by_id(self):
    conversion = self.db.getConversion(_id=17)
    self.assertIsNone(conversion)

  def test_returns_none_if_conversion_not_found_by_name(self):
    conversion = self.db.getConversion(old_text="noSuchConversion")
    self.assertIsNone(conversion)

  def test_can_add_and_retrieve_conversions(self):
    conv_id = self.db.addConversion(old_text="harsh", new_license="HarshEULA")

    # confirm that we now have five conversions
    convs = self.db.getConversionsAll()
    self.assertEqual(len(convs), 5)

    # and confirm that we can retrieve this one by its text
    conv = self.db.getConversion(old_text="harsh")
    self.assertEqual(conv._id, 5)
    self.assertEqual(conv.new_license_id, 2)
    self.assertIsNotNone(conv.new_license)
    self.assertEqual(conv.new_license.name, "HarshEULA")

    # and confirm that we can retrieve this one by id
    conv = self.db.getConversion(_id=5)
    self.assertEqual(conv.old_text, "harsh")
    self.assertEqual(conv.new_license_id, 2)

  def test_can_start_adding_but_rollback_conversion(self):
    conv_id = self.db.addConversion(old_text="will rollback",
      new_license="293PageEULA", commit=False)
    self.db.rollback()
    # confirm that we still only have four conversions
    convs = self.db.getConversionsAll()
    self.assertEqual(len(convs), 4)
    # and confirm that this conversion ID doesn't exist in database
    conv = self.db.getConversion(_id=5)
    self.assertIsNone(conv)

  def test_can_start_adding_and_then_commit_conversions(self):
    c1_id = self.db.addConversion(old_text="c1", new_license="293PageEULA",
      commit=False)
    c2_id = self.db.addConversion(old_text="c2", new_license="293PageEULA",
      commit=False)
    self.db.commit()
    # confirm that we now have six conversions
    convs = self.db.getConversionsAll()
    self.assertEqual(len(convs), 6)

  def test_cannot_add_conversion_without_license(self):
    with self.assertRaises(TypeError):
      self.db.addConversion(old_text="oops")
    # confirm it wasn't added either
    conv = self.db.getConversion(old_text="oops")
    self.assertIsNone(conv)

  def test_cannot_add_conversion_without_existing_license(self):
    with self.assertRaises(ProjectDBInsertError):
      self.db.addConversion(old_text="oops", new_license="blah")
    # confirm it wasn't added either
    conv = self.db.getConversion(old_text="oops")
    self.assertIsNone(conv)

  def test_cannot_add_conversion_with_duplicate_name(self):
    with self.assertRaises(ProjectDBInsertError):
      self.db.addConversion(old_text="NC", new_license="293PageEULA")

  def test_can_edit_conversion_license(self):
    self.db.changeConversion(old_text="NC", new_license="DoAnything")
    conv = self.db.getConversion(old_text="NC")
    self.assertEqual(conv.new_license_id, 1)

  def test_cannot_edit_conversion_name_that_does_not_exist(self):
    with self.assertRaises(ProjectDBUpdateError):
      self.db.changeConversion(old_text="invalid", new_license="will fail")

  def test_can_retrieve_all_conversions(self):
    convs = self.db.getConversionsAll()
    self.assertIsInstance(convs, list)
    self.assertEqual(len(convs), 4)
    self.assertIsInstance(convs[0], Conversion)
    # will sort alphabetically
    self.assertEqual(convs[2]._id, 2)
    self.assertEqual(convs[2].old_text, "NC")
Esempio n. 7
0
class DBScanUnitTestSuite(unittest.TestCase):
    """spdxLicenseManager unit test suite for scan metadata in DB."""
    def setUp(self):
        # create and initialize an in-memory database
        self.db = ProjectDB()
        self.db.createDB(":memory:")
        self.db.initializeDBTables()

        # insert sample data
        self.insertSampleSubprojectData()
        self.insertSampleScanData()

    def tearDown(self):
        self.db.closeDB()
        self.db = None

    def insertSampleSubprojectData(self):
        subprojects = [
            Subproject(_id=1, name="sub1", desc="subproject 1"),
            Subproject(_id=2, name="subX", desc="subproject XYZ"),
            Subproject(_id=3, name="subC", desc="subproject B"),
        ]
        self.db.session.bulk_save_objects(subprojects)
        self.db.session.commit()

    def insertSampleScanData(self):
        scans = [
            Scan(_id=1,
                 subproject_id=2,
                 scan_dt=datetime.date(2017, 1, 10),
                 desc="XYZ initial scan"),
            Scan(_id=2,
                 subproject_id=1,
                 scan_dt=datetime.date(2017, 1, 3),
                 desc="1 initial scan"),
            Scan(_id=3,
                 subproject_id=2,
                 scan_dt=datetime.date(2017, 2, 10),
                 desc="XYZ 2017-02 monthly scan"),
            Scan(_id=4,
                 subproject_id=2,
                 scan_dt=datetime.date(2017, 2, 17),
                 desc="XYZ 2017-02 rescan"),
        ]
        self.db.session.bulk_save_objects(scans)
        self.db.session.commit()

    ##### Test cases below

    def test_can_retrieve_all_scans(self):
        scans = self.db.getScansAll()
        self.assertIsInstance(scans, list)
        self.assertEqual(len(scans), 4)
        # will sort by ID
        self.assertEqual(scans[0]._id, 1)
        self.assertEqual(scans[0].subproject.name, "subX")
        self.assertEqual(scans[1]._id, 2)
        self.assertEqual(scans[1].subproject.name, "sub1")
        self.assertEqual(scans[2]._id, 3)
        self.assertEqual(scans[2].subproject.name, "subX")
        self.assertEqual(scans[3]._id, 4)
        self.assertEqual(scans[3].subproject.name, "subX")

    def test_can_retrieve_scans_in_just_one_subproject(self):
        scans = self.db.getScansFiltered(subproject="subX")
        self.assertIsInstance(scans, list)
        self.assertEqual(len(scans), 3)
        # will sort by scan ID
        self.assertEqual(scans[0]._id, 1)
        self.assertEqual(scans[0].desc, "XYZ initial scan")
        self.assertEqual(scans[1]._id, 3)
        self.assertEqual(scans[1].desc, "XYZ 2017-02 monthly scan")
        self.assertEqual(scans[2]._id, 4)
        self.assertEqual(scans[2].desc, "XYZ 2017-02 rescan")

    def test_cannot_retrieve_scans_in_subproject_that_does_not_exist(self):
        with self.assertRaises(ProjectDBQueryError):
            self.db.getScansFiltered(subproject="invalid")

    def test_can_retrieve_scans_by_subproject_and_month(self):
        scans = self.db.getScansFiltered(subproject="subX",
                                         month_tuple=(2017, 2))
        self.assertIsInstance(scans, list)
        self.assertEqual(len(scans), 2)
        # will sort by scan ID
        self.assertEqual(scans[0]._id, 3)
        self.assertEqual(scans[0].desc, "XYZ 2017-02 monthly scan")
        self.assertEqual(scans[1]._id, 4)
        self.assertEqual(scans[1].desc, "XYZ 2017-02 rescan")

    def test_cannot_retrieve_scans_in_subproject_with_integer_month(self):
        with self.assertRaises(ProjectDBQueryError):
            self.db.getScansFiltered(subproject="subX", month_tuple=2)

    def test_cannot_retrieve_scans_in_subproject_with_invalid_month_tuple(
            self):
        with self.assertRaises(ProjectDBQueryError):
            self.db.getScansFiltered(subproject="subX",
                                     month_tuple=("hi", "there"))

    def test_retrieve_scan_with_unknown_month_returns_empty_list(self):
        scans = self.db.getScansFiltered(subproject="subX",
                                         month_tuple=(2017, 3))
        self.assertIsInstance(scans, list)
        self.assertEqual(scans, [])

    def test_can_retrieve_scans_by_month_without_subproject(self):
        scans = self.db.getScansFiltered(month_tuple=(2017, 1))
        self.assertIsInstance(scans, list)
        self.assertEqual(len(scans), 2)
        # will sort by scan ID
        self.assertEqual(scans[0]._id, 1)
        self.assertEqual(scans[0].desc, "XYZ initial scan")
        self.assertEqual(scans[1]._id, 2)
        self.assertEqual(scans[1].desc, "1 initial scan")

    def test_cannot_retrieve_filtered_scans_without_subproject_or_month(self):
        with self.assertRaises(ProjectDBQueryError):
            self.db.getScansFiltered()

    def test_cannot_retrieve_filtered_scans_with_positional_args(self):
        with self.assertRaises(TypeError):
            self.db.getScansFiltered("subX")
        with self.assertRaises(TypeError):
            self.db.getScansFiltered("subX", (2017, 1))
        with self.assertRaises(TypeError):
            self.db.getScansFiltered((2017, 1))

    def test_can_retrieve_one_scan_by_id(self):
        scan = self.db.getScan(_id=2)
        self.assertEqual(scan.desc, "1 initial scan")

    def test_returns_none_if_scan_not_found_by_id(self):
        scan = self.db.getScan(_id=17)
        self.assertIsNone(scan)

    def test_can_add_and_retrieve_scans(self):
        scan_id = self.db.addScan(subproject="subX",
                                  scan_dt_str="2017-03-05",
                                  desc="XYZ 2017-03 monthly scan")

        # confirm that we now have five scans
        scans = self.db.getScansAll()
        self.assertEqual(len(scans), 5)
        self.assertEqual(scan_id, 5)

        # and confirm that we can retrieve this one by id
        scan = self.db.getScan(_id=5)
        self.assertEqual(scan.desc, "XYZ 2017-03 monthly scan")
        self.assertEqual(scan.subproject.name, "subX")

    def test_can_start_adding_but_rollback_scan(self):
        scan_id = self.db.addScan(subproject="subX",
                                  scan_dt_str="2011-01-01",
                                  desc="will rollback",
                                  commit=False)
        self.db.rollback()
        # confirm that we still only have four scans
        scans = self.db.getScansAll()
        self.assertEqual(len(scans), 4)
        # and confirm that this scan ID doesn't exist in database
        scan = self.db.getScan(_id=scan_id)
        self.assertIsNone(scan)

    def test_can_start_adding_and_then_commit_scans(self):
        s1_id = self.db.addScan(subproject="subX",
                                scan_dt_str="2011-01-01",
                                desc="s1",
                                commit=False)
        s2_id = self.db.addScan(subproject="subX",
                                scan_dt_str="2012-02-02",
                                desc="s2",
                                commit=False)
        self.db.commit()
        # confirm that we now have six scans
        scans = self.db.getScansAll()
        self.assertEqual(len(scans), 6)

    def test_cannot_add_scan_without_subproject(self):
        with self.assertRaises(TypeError):
            self.db.addScan(scan_dt_str="2011-01-01", desc="oops")
        # confirm it wasn't added either
        scan = self.db.getScan(_id=5)
        self.assertIsNone(scan)

    def test_cannot_add_scan_without_scan_date_string(self):
        with self.assertRaises(TypeError):
            self.db.addScan(subproject="subX", desc="oops")
        # confirm it wasn't added either
        scan = self.db.getScan(_id=5)
        self.assertIsNone(scan)

    def test_can_add_scan_with_duplicate_desc(self):
        scan_id = self.db.addScan(subproject="sub1",
                                  scan_dt_str="2017-02-02",
                                  desc="1 initial scan")

        # confirm that we now have five scans and desc matches
        scans = self.db.getScansAll()
        self.assertEqual(len(scans), 5)
        self.assertEqual(scan_id, 5)
        scan = self.db.getScan(_id=5)
        self.assertEqual(scan.desc, "1 initial scan")

    def test_can_add_scan_with_duplicate_scan_date(self):
        scan_id = self.db.addScan(subproject="sub1",
                                  scan_dt_str="2017-01-03",
                                  desc="1 rescan")

        # confirm that we now have five scans and desc matches
        scans = self.db.getScansAll()
        self.assertEqual(len(scans), 5)
        self.assertEqual(scan_id, 5)
        scan = self.db.getScan(_id=5)
        self.assertEqual(scan.desc, "1 rescan")
Esempio n. 8
0
class DBFileUnitTestSuite(unittest.TestCase):
    """spdxLicenseManager unit test suite for scan metadata in DB."""
    def setUp(self):
        # create and initialize an in-memory database
        self.db = ProjectDB()
        self.db.createDB(":memory:")
        self.db.initializeDBTables()

        # insert sample data
        self.insertSampleCategoryData()
        self.insertSampleLicenseData()
        self.insertSampleSubprojectData()
        self.insertSampleScanData()
        self.insertSampleFileData()

    def tearDown(self):
        self.db.closeDB()
        self.db = None

    def insertSampleCategoryData(self):
        categories = [
            Category(_id=1, name="a category", order=3),
            Category(_id=2, name="cat", order=2),
            Category(_id=3, name="blah category", order=1),
        ]
        self.db.session.bulk_save_objects(categories)
        self.db.session.commit()

    def insertSampleLicenseData(self):
        licenses = [
            License(_id=1, name="DoAnything", category_id=1),
            License(_id=2, name="HarshEULA", category_id=2),
            License(_id=3, name="293PageEULA", category_id=3),
            License(_id=4, name="DoAnythingNoncommercial", category_id=1),
        ]
        self.db.session.bulk_save_objects(licenses)
        self.db.session.commit()

    def insertSampleSubprojectData(self):
        subprojects = [
            Subproject(_id=1, name="sub1", desc="subproject 1"),
            Subproject(_id=2, name="subX", desc="subproject XYZ"),
            Subproject(_id=3, name="subC", desc="subproject B"),
        ]
        self.db.session.bulk_save_objects(subprojects)
        self.db.session.commit()

    def insertSampleScanData(self):
        scans = [
            Scan(_id=1,
                 subproject_id=2,
                 scan_dt=datetime.date(2017, 1, 10),
                 desc="XYZ initial scan"),
            Scan(_id=2,
                 subproject_id=1,
                 scan_dt=datetime.date(2017, 1, 3),
                 desc="1 initial scan"),
            Scan(_id=3,
                 subproject_id=2,
                 scan_dt=datetime.date(2017, 2, 10),
                 desc="XYZ 2017-02 monthly scan"),
            Scan(_id=4,
                 subproject_id=2,
                 scan_dt=datetime.date(2017, 2, 17),
                 desc="XYZ 2017-02 rescan"),
        ]
        self.db.session.bulk_save_objects(scans)
        self.db.session.commit()

    def insertSampleFileData(self):
        files = [
            File(_id=1,
                 scan_id=1,
                 path="/fileC.c",
                 license_id=1,
                 sha1="aabbcc",
                 md5="ddeeff",
                 sha256="aaccee"),
            File(_id=2,
                 scan_id=1,
                 path="/fileA.c",
                 license_id=1,
                 sha1="112233",
                 md5="445566",
                 sha256="778899"),
            File(_id=3,
                 scan_id=1,
                 path="/fileB.c",
                 license_id=2,
                 sha1=None,
                 md5=None,
                 sha256=None),
            File(_id=4,
                 scan_id=1,
                 path="/dir/fileA.c",
                 license_id=4,
                 sha1="123456",
                 md5="789012",
                 sha256="345678"),
        ]
        self.db.session.bulk_save_objects(files)
        self.db.session.commit()

    ##### Test cases below

    def test_can_retrieve_files_in_one_scan(self):
        files = self.db.getFiles(scan_id=1)
        self.assertIsInstance(files, list)
        self.assertEqual(len(files), 4)
        # will sort by file path
        self.assertEqual(files[0]._id, 4)
        self.assertEqual(files[0].path, "/dir/fileA.c")
        self.assertEqual(files[1]._id, 2)
        self.assertEqual(files[1].path, "/fileA.c")
        self.assertEqual(files[2]._id, 3)
        self.assertEqual(files[2].path, "/fileB.c")
        self.assertEqual(files[3]._id, 1)
        self.assertEqual(files[3].path, "/fileC.c")

    def test_cannot_retrieve_files_in_scan_that_does_not_exist(self):
        with self.assertRaises(ProjectDBQueryError):
            self.db.getFiles(scan_id=17)

    def test_returns_empty_list_if_no_files_in_known_scan(self):
        files = self.db.getFiles(scan_id=4)
        self.assertEqual(files, [])

    def test_can_get_file_by_id(self):
        file = self.db.getFile(_id=3)
        self.assertEqual(file.path, "/fileB.c")
        self.assertEqual(file.license.name, "HarshEULA")

    def test_can_get_file_by_scan_and_path(self):
        file = self.db.getFile(scan_id=1, path="/fileB.c")
        self.assertEqual(file._id, 3)
        self.assertEqual(file.license.name, "HarshEULA")

    def test_cannot_get_file_by_id_with_scan_or_path(self):
        with self.assertRaises(ProjectDBQueryError):
            self.db.getFile(_id=3, scan_id=1)
        with self.assertRaises(ProjectDBQueryError):
            self.db.getFile(_id=3, path="/fileB.c")

    def test_cannot_get_file_with_no_id_or_scan_or_path(self):
        with self.assertRaises(ProjectDBQueryError):
            self.db.getFile()

    def test_cannot_get_file_with_only_one_of_scan_or_path(self):
        with self.assertRaises(ProjectDBQueryError):
            self.db.getFile(scan_id=1)
        with self.assertRaises(ProjectDBQueryError):
            self.db.getFile(path="/fileB.c")

    def test_returns_none_if_file_not_found_by_id(self):
        file = self.db.getFile(_id=17)
        self.assertIsNone(file)

    def test_returns_none_if_file_not_found_by_scan_plus_path(self):
        file = self.db.getFile(scan_id=1, path="/nope")
        self.assertIsNone(file)
        file = self.db.getFile(scan_id=6, path="/fileB.c")
        self.assertIsNone(file)

    def test_can_add_and_retrieve_files(self):
        self.db.addFile(scan_id=1,
                        path="/file17.py",
                        license_id=3,
                        sha1=None,
                        md5=None,
                        sha256=None)
        self.db.addFile(scan_id=1,
                        path="/file13.py",
                        license_id=2,
                        sha1=None,
                        md5=None,
                        sha256=None)
        file_id = self.db.addFile(scan_id=1,
                                  path="/dir5/file128.py",
                                  license_id=4,
                                  sha1="123456",
                                  md5="789012",
                                  sha256="345678")

        # confirm that we now have seven files in this scan
        files = self.db.getFiles(scan_id=1)
        self.assertEqual(len(files), 7)
        self.assertEqual(file_id, 7)

        # and confirm that we can retrieve this one by id
        file = self.db.getFile(_id=7)
        self.assertEqual(file.path, "/dir5/file128.py")
        self.assertEqual(file.license.name, "DoAnythingNoncommercial")

    def test_can_start_adding_but_rollback_file(self):
        file_id = self.db.addFile(scan_id=1,
                                  path="/will_rollback",
                                  license_id=3,
                                  sha1=None,
                                  md5=None,
                                  sha256=None,
                                  commit=False)
        self.db.rollback()
        # confirm that we still only have four files
        files = self.db.getFiles(scan_id=1)
        self.assertEqual(len(files), 4)
        # and confirm that this file ID doesn't exist in database
        file = self.db.getFile(_id=file_id)
        self.assertIsNone(file)

    def test_can_start_adding_and_then_commit_files(self):
        f1_id = self.db.addFile(scan_id=1,
                                path="/f1",
                                license_id=1,
                                sha1=None,
                                md5=None,
                                sha256=None,
                                commit=False)
        f2_id = self.db.addFile(scan_id=1,
                                path="/f2",
                                license_id=1,
                                sha1=None,
                                md5=None,
                                sha256=None,
                                commit=False)
        self.db.commit()
        # confirm that we now have six files
        files = self.db.getFiles(scan_id=1)
        self.assertEqual(len(files), 6)

    def test_can_bulk_add_and_retrieve_files(self):
        bulkfiles = [
            ("/file17.py", 3, None, None, None),
            ("/file13.py", 2, None, None, None),
            ("/dir5/file128.py", 4, "123456", "789012", "345678"),
        ]
        self.db.addBulkFiles(scan_id=1, file_tuples=bulkfiles)

        # confirm that we now have seven files in this scan
        files = self.db.getFiles(scan_id=1)
        self.assertEqual(len(files), 7)

        # and confirm that we can retrieve last one by id
        file = self.db.getFile(_id=7)
        self.assertEqual(file.path, "/dir5/file128.py")
        self.assertEqual(file.license.name, "DoAnythingNoncommercial")

    def test_can_start_bulk_adding_files_but_rollback(self):
        bulkfiles = [
            ("/file17.py", 3, None, None, None),
            ("/file13.py", 2, None, None, None),
            ("/dir5/file128.py", 4, "123456", "789012", "345678"),
        ]
        self.db.addBulkFiles(scan_id=1, file_tuples=bulkfiles, commit=False)
        self.db.rollback()
        # confirm that we still only have four files
        files = self.db.getFiles(scan_id=1)
        self.assertEqual(len(files), 4)
        # and confirm that this file ID doesn't exist in database
        file = self.db.getFile(_id=7)
        self.assertIsNone(file)

    def test_can_start_bulk_adding_and_then_commit_files(self):
        bulkfiles = [
            ("/file17.py", 3, None, None, None),
            ("/file13.py", 2, None, None, None),
            ("/dir5/file128.py", 4, "123456", "789012", "345678"),
        ]
        self.db.addBulkFiles(scan_id=1, file_tuples=bulkfiles, commit=False)
        self.db.commit()
        # confirm that we now have seven files
        files = self.db.getFiles(scan_id=1)
        self.assertEqual(len(files), 7)
Esempio n. 9
0
class DBSubprojectUnitTestSuite(unittest.TestCase):
    """spdxLicenseManager unit test suite for subproject data in DB."""
    def setUp(self):
        # create and initialize an in-memory database
        self.db = ProjectDB()
        self.db.createDB(":memory:")
        self.db.initializeDBTables()

        # insert sample data
        self.insertSampleSubprojectData()

    def tearDown(self):
        self.db.closeDB()
        self.db = None

    def insertSampleSubprojectData(self):
        subprojects = [
            Subproject(_id=1,
                       name="sub1",
                       spdx_search="sub1",
                       desc="subproject 1"),
            Subproject(_id=2,
                       name="subX",
                       spdx_search="subX",
                       desc="subproject XYZ"),
            Subproject(_id=3,
                       name="subC",
                       spdx_search="subC",
                       desc="subproject B"),
        ]
        self.db.session.bulk_save_objects(subprojects)
        self.db.session.commit()

    ##### Test cases below

    def test_can_retrieve_all_subproject_names_and_descs(self):
        subprojects = self.db.getSubprojectsAll()
        self.assertIsInstance(subprojects, list)
        self.assertEqual(len(subprojects), 3)
        self.assertEqual(subprojects[0]._id, 1)
        self.assertEqual(subprojects[0].name, "sub1")
        self.assertEqual(subprojects[0].desc, "subproject 1")
        self.assertEqual(subprojects[0].spdx_search, "sub1")

    def test_all_subprojects_are_sorted_by_name(self):
        subprojects = self.db.getSubprojectsAll()
        self.assertEqual(subprojects[0].name, "sub1")
        self.assertEqual(subprojects[1].name, "subC")
        self.assertEqual(subprojects[2].name, "subX")

    def test_can_retrieve_one_subproject_by_id(self):
        subproject = self.db.getSubproject(_id=2)
        self.assertEqual(subproject.name, "subX")
        self.assertEqual(subproject.desc, "subproject XYZ")
        self.assertEqual(subproject.spdx_search, "subX")

    def test_can_retrieve_one_subproject_by_name(self):
        subproject = self.db.getSubproject(name="subC")
        self.assertEqual(subproject._id, 3)
        self.assertEqual(subproject.desc, "subproject B")
        self.assertEqual(subproject.spdx_search, "subC")

    def test_cannot_retrieve_subproject_by_both_name_and_id(self):
        with self.assertRaises(ProjectDBQueryError):
            self.db.getSubproject(_id=3, name="subC")

    def test_cannot_retrieve_subproject_without_either_name_or_id(self):
        with self.assertRaises(ProjectDBQueryError):
            self.db.getSubproject()

    def test_cannot_retrieve_subproject_with_positional_args(self):
        with self.assertRaises(TypeError):
            self.db.getSubproject("subC")

    def test_returns_none_if_subproject_not_found_by_id(self):
        subproject = self.db.getSubproject(_id=17)
        self.assertIsNone(subproject)

    def test_returns_none_if_subproject_not_found_by_name(self):
        subproject = self.db.getSubproject(name="noSuchSubproject")
        self.assertIsNone(subproject)

    def test_can_add_and_retrieve_subproject(self):
        subproject_id = self.db.addSubproject("newsub",
                                              "subproject new",
                                              spdx_search="newsub")

        # confirm that we now have four subprojects
        subprojects = self.db.getSubprojectsAll()
        self.assertEqual(len(subprojects), 4)

        # and confirm that we can retrieve this one by name
        subproject = self.db.getSubproject(name="newsub")
        self.assertEqual(subproject.name, "newsub")
        self.assertEqual(subproject.desc, "subproject new")
        self.assertEqual(subproject.spdx_search, "newsub")

        # and confirm that we can retrieve this one by id
        subproject = self.db.getSubproject(_id=4)
        self.assertEqual(subproject.name, "newsub")
        self.assertEqual(subproject.desc, "subproject new")
        self.assertEqual(subproject.spdx_search, "newsub")

    def test_can_add_subproject_without_providing_spdx_search_name(self):
        subproject_id = self.db.addSubproject("newsub", "subproject new")

        # confirm that we now have four subprojects
        subprojects = self.db.getSubprojectsAll()
        self.assertEqual(len(subprojects), 4)

        # and confirm that we can retrieve this one by name
        subproject = self.db.getSubproject(name="newsub")
        self.assertEqual(subproject.name, "newsub")
        self.assertEqual(subproject.desc, "subproject new")
        self.assertEqual(subproject.spdx_search, "newsub")

        # and confirm that we can retrieve this one by id
        subproject = self.db.getSubproject(_id=4)
        self.assertEqual(subproject.name, "newsub")
        self.assertEqual(subproject.desc, "subproject new")
        self.assertEqual(subproject.spdx_search, "newsub")

    def test_can_start_adding_but_rollback_subproject(self):
        subproject_id = self.db.addSubproject(name="newsub",
                                              spdx_search="newsub",
                                              desc="will rollback",
                                              commit=False)
        self.db.rollback()
        # confirm that we still only have three subprojects
        subprojects = self.db.getSubprojectsAll()
        self.assertEqual(len(subprojects), 3)
        # and confirm that this subproject ID doesn't exist in database
        subproject = self.db.getSubproject(_id=subproject_id)
        self.assertIsNone(subproject)

    def test_can_start_adding_and_then_commit_subprojects(self):
        s2_id = self.db.addSubproject(name="news2",
                                      spdx_search="news2",
                                      desc="new sp 2",
                                      commit=False)
        s1_id = self.db.addSubproject(name="news1",
                                      spdx_search="news1",
                                      desc="new sp 1",
                                      commit=False)
        self.db.commit()
        # confirm that we now have five subprojects
        subprojects = self.db.getSubprojectsAll()
        self.assertEqual(len(subprojects), 5)

    def test_can_edit_subproject_spdx_string(self):
        self.db.changeSubprojectSPDXSearch(name="subX",
                                           spdx_search="subXSpecial")
        subproject = self.db.getSubproject(name="subX")
        self.assertEqual(subproject.name, "subX")
        self.assertEqual(subproject.spdx_search, "subXSpecial")

    def test_cannot_edit_subproject_spdx_search_for_nonexistent_name(self):
        with self.assertRaises(ProjectDBUpdateError):
            self.db.changeSubprojectSPDXSearch(name="invalid",
                                               spdx_search="this will fail")

    def test_cannot_change_subproject_spdx_search_to_existing_search(self):
        with self.assertRaises(ProjectDBUpdateError):
            self.db.changeSubprojectSPDXSearch(name="sub1", spdx_search="subX")
Esempio n. 10
0
class DBCategoryUnitTestSuite(unittest.TestCase):
    """spdxLicenseManager unit test suite for category data in DB."""
    def setUp(self):
        # create and initialize an in-memory database
        self.db = ProjectDB()
        self.db.createDB(":memory:")
        self.db.initializeDBTables()

        # insert sample data
        self.insertSampleCategoryData()

    def tearDown(self):
        self.db.closeDB()
        self.db = None

    def insertSampleCategoryData(self):
        categories = [
            Category(_id=1, name="a category", order=3),
            Category(_id=2, name="cat of crazy licenses", order=2),
            Category(_id=3, name="blah category", order=1),
        ]
        self.db.session.bulk_save_objects(categories)
        self.db.session.commit()

    ##### Test cases below

    def test_can_retrieve_all_category_names_and_descs(self):
        categories = self.db.getCategoriesAll()
        self.assertIsInstance(categories, list)
        self.assertEqual(len(categories), 3)
        self.assertEqual(categories[0]._id, 3)
        self.assertEqual(categories[0].name, "blah category")
        self.assertEqual(categories[0].order, 1)

    def test_all_categories_are_sorted_by_order(self):
        categories = self.db.getCategoriesAll()
        self.assertEqual(categories[0].name, "blah category")
        self.assertEqual(categories[1].name, "cat of crazy licenses")
        self.assertEqual(categories[2].name, "a category")

    def test_can_retrieve_one_category_by_id(self):
        category = self.db.getCategory(_id=2)
        self.assertEqual(category.name, "cat of crazy licenses")

    def test_can_retrieve_one_category_by_name(self):
        category = self.db.getCategory(name="a category")
        self.assertEqual(category._id, 1)

    def test_cannot_retrieve_category_by_both_name_and_id(self):
        with self.assertRaises(ProjectDBQueryError):
            self.db.getCategory(_id=3, name="blah category")

    def test_cannot_retrieve_category_without_either_name_or_id(self):
        with self.assertRaises(ProjectDBQueryError):
            self.db.getCategory()

    def test_cannot_retrieve_category_with_positional_args(self):
        with self.assertRaises(TypeError):
            self.db.getCategory("blah category")

    def test_returns_none_if_category_not_found_by_id(self):
        category = self.db.getCategory(_id=17)
        self.assertIsNone(category)

    def test_returns_none_if_category_not_found_by_name(self):
        category = self.db.getCategory(name="noSuchCategory")
        self.assertIsNone(category)

    def test_can_add_and_retrieve_categories(self):
        category_id = self.db.addCategory(name="newcat", order=4)

        # confirm that we now have four categories
        categories = self.db.getCategoriesAll()
        self.assertEqual(len(categories), 4)

        # and confirm that we can retrieve this one by name
        category = self.db.getCategory(name="newcat")
        self.assertEqual(category._id, 4)
        self.assertEqual(category.order, 4)

        # and confirm that we can retrieve this one by id
        category = self.db.getCategory(_id=4)
        self.assertEqual(category.name, "newcat")
        self.assertEqual(category.order, 4)

    def test_can_start_adding_but_rollback_category(self):
        category_id = self.db.addCategory(name="will rollback",
                                          order=99,
                                          commit=False)
        self.db.rollback()
        # confirm that we still only have three categories
        categories = self.db.getCategoriesAll()
        self.assertEqual(len(categories), 3)
        # and confirm that this category ID doesn't exist in database
        category = self.db.getCategory(_id=category_id)
        self.assertIsNone(category)

    def test_can_start_adding_and_then_commit_categories(self):
        c1_id = self.db.addCategory(name="newc1", order=98, commit=False)
        c2_id = self.db.addCategory(name="newc2", order=99, commit=False)
        self.db.commit()
        # confirm that we now have five categories
        categories = self.db.getCategoriesAll()
        self.assertEqual(len(categories), 5)

    def test_omitting_order_from_new_category_places_it_at_end(self):
        category_id = self.db.addCategory(name="newcat")
        category = self.db.getCategory(name="newcat")
        self.assertEqual(category.order, 4)

    def test_cannot_create_category_with_existing_order(self):
        with self.assertRaises(ProjectDBInsertError):
            self.db.addCategory(name="duplicate order", order=3)

    def test_can_get_highest_category_order(self):
        highestOrder = self.db.getCategoryHighestOrder()
        self.assertEqual(highestOrder, 3)

    def test_highest_order_works_even_with_no_categories(self):
        newdb = ProjectDB()
        newdb.createDB(":memory:")
        newdb.initializeDBTables()
        highestOrder = newdb.getCategoryHighestOrder()
        self.assertEqual(highestOrder, 0)

    def test_can_edit_category_name(self):
        self.db.changeCategoryName(name="a category",
                                   newName="another category")
        category = self.db.getCategory(name="another category")
        self.assertEqual(category._id, 1)

    def test_cannot_edit_category_name_that_does_not_exist(self):
        with self.assertRaises(ProjectDBUpdateError):
            self.db.changeCategoryName(name="invalid",
                                       newName="this will fail")

    def test_cannot_change_category_name_to_existing_name(self):
        with self.assertRaises(ProjectDBUpdateError):
            self.db.changeCategoryName(name="a category",
                                       newName="blah category")

    def test_can_reorder_categories_from_higher_to_lower(self):
        self.db.changeCategoryOrder(name="a category",
                                    sortBefore="blah category")
        categories = self.db.getCategoriesAll()
        self.assertEqual(categories[0].name, "a category")
        self.assertEqual(categories[1].name, "blah category")
        self.assertEqual(categories[2].name, "cat of crazy licenses")

    def test_can_reorder_categories_from_lower_to_higher(self):
        self.db.changeCategoryOrder(name="blah category",
                                    sortBefore="a category")
        categories = self.db.getCategoriesAll()
        self.assertEqual(categories[0].name, "cat of crazy licenses")
        self.assertEqual(categories[1].name, "blah category")
        self.assertEqual(categories[2].name, "a category")

    def test_cannot_reorder_category_name_before_one_that_does_not_exist(self):
        with self.assertRaises(ProjectDBUpdateError):
            self.db.changeCategoryOrder(name="a category", sortBefore="oops")

    def test_cannot_reorder_category_name_that_does_not_exist(self):
        with self.assertRaises(ProjectDBUpdateError):
            self.db.changeCategoryOrder(name="oops", sortBefore="a category")

    def test_cannot_create_category_with_order_less_than_one(self):
        with self.assertRaises(ProjectDBInsertError):
            self.db.addCategory(name="need positive order", order=0)
Esempio n. 11
0
class DBLicenseUnitTestSuite(unittest.TestCase):
  """spdxLicenseManager unit test suite for license data in DB."""

  def setUp(self):
    # create and initialize an in-memory database
    self.db = ProjectDB()
    self.db.createDB(":memory:")
    self.db.initializeDBTables()

    # insert sample data
    self.insertSampleCategoryData()
    self.insertSampleLicenseData()

  def tearDown(self):
    self.db.closeDB()
    self.db = None

  def insertSampleCategoryData(self):
    categories = [
      Category(_id=1, name="a category", order=3),
      Category(_id=2, name="cat", order=2),
      Category(_id=3, name="blah category", order=1),
    ]
    self.db.session.bulk_save_objects(categories)
    self.db.session.commit()

  def insertSampleLicenseData(self):
    licenses = [
      License(_id=1, name="DoAnything", category_id=1),
      License(_id=2, name="HarshEULA", category_id=2),
      License(_id=3, name="293PageEULA", category_id=3),
      License(_id=4, name="DoAnythingNoncommercial", category_id=1),
    ]
    self.db.session.bulk_save_objects(licenses)
    self.db.session.commit()

  ##### Test cases below

  def test_can_retrieve_all_license_names(self):
    licenses = self.db.getLicensesAll()
    self.assertIsInstance(licenses, list)
    self.assertEqual(len(licenses), 4)
    # will sort alphabetically
    self.assertEqual(licenses[0]._id, 3)
    self.assertEqual(licenses[0].name, "293PageEULA")

  def test_can_retrieve_all_license_names_by_category(self):
    cat_lics = self.db.getLicensesAllByCategory()
    self.assertIsInstance(cat_lics, list)
    self.assertEqual(len(cat_lics), 4)
    # returns a flat list of tuples, of form [(cat name, lic name)]
    # categories should be ordered by cat order
    # and within each category, licenses should be ordered alphabetically
    self.assertEqual(cat_lics[0][0], "blah category")
    self.assertEqual(cat_lics[0][1], "293PageEULA")
    self.assertEqual(cat_lics[1][0], "cat")
    self.assertEqual(cat_lics[1][1], "HarshEULA")
    self.assertEqual(cat_lics[2][0], "a category")
    self.assertEqual(cat_lics[2][1], "DoAnything")
    self.assertEqual(cat_lics[3][0], "a category")
    self.assertEqual(cat_lics[3][1], "DoAnythingNoncommercial")

  def test_can_retrieve_licenses_in_just_one_category(self):
    licenses = self.db.getLicensesInCategory(category="a category")
    self.assertIsInstance(licenses, list)
    self.assertEqual(len(licenses), 2)
    # will sort alphabetically
    self.assertEqual(licenses[0]._id, 1)
    self.assertEqual(licenses[0].name, "DoAnything")
    self.assertEqual(licenses[1]._id, 4)
    self.assertEqual(licenses[1].name, "DoAnythingNoncommercial")

  def test_cannot_retrieve_license_in_category_that_does_not_exist(self):
    with self.assertRaises(ProjectDBQueryError):
      self.db.getLicensesInCategory(category="invalid")

  def test_can_retrieve_one_license_by_id(self):
    license = self.db.getLicense(_id=2)
    self.assertEqual(license.name, "HarshEULA")

  def test_can_retrieve_one_license_by_name(self):
    license = self.db.getLicense(name="DoAnything")
    self.assertEqual(license._id, 1)

  def test_cannot_retrieve_license_by_both_name_and_id(self):
    with self.assertRaises(ProjectDBQueryError):
      self.db.getLicense(_id=3, name="293PageEULA")

  def test_cannot_retrieve_license_without_either_name_or_id(self):
    with self.assertRaises(ProjectDBQueryError):
      self.db.getLicense()

  def test_cannot_retrieve_license_with_positional_args(self):
    with self.assertRaises(TypeError):
      self.db.getLicense("DoAnything")

  def test_returns_none_if_license_not_found_by_id(self):
    license = self.db.getLicense(_id=17)
    self.assertIsNone(license)

  def test_returns_none_if_license_not_found_by_name(self):
    license = self.db.getLicense(name="noSuchLicense")
    self.assertIsNone(license)

  def test_can_retrieve_multiple_licenses_by_name(self):
    licenses = [
      "293PageEULA",
      "DoAnything",
      "UnknownLicense",
    ]
    ldict = self.db.getMultipleLicenses(licenses)
    self.assertEqual(ldict["293PageEULA"], 3)
    self.assertEqual(ldict["DoAnything"], 1)
    self.assertIsNone(ldict["UnknownLicense"])

  def test_can_add_and_retrieve_licenses(self):
    license_id = self.db.addLicense(name="SomeOtherEULA",
      category="blah category")

    # confirm that we now have five licenses
    licenses = self.db.getLicensesAll()
    self.assertEqual(len(licenses), 5)

    # and confirm that we can retrieve this one by name
    license = self.db.getLicense(name="SomeOtherEULA")
    self.assertEqual(license._id, 5)
    self.assertEqual(license.category_id, 3)
    self.assertIsNotNone(license.category)
    self.assertEqual(license.category.name, "blah category")

    # and confirm that we can retrieve this one by id
    license = self.db.getLicense(_id=5)
    self.assertEqual(license.name, "SomeOtherEULA")
    self.assertEqual(license.category_id, 3)

  def test_can_start_adding_but_rollback_license(self):
    license_id = self.db.addLicense(name="will rollback",
      category="blah category", commit=False)
    self.db.rollback()
    # confirm that we still only have four licenses
    licenses = self.db.getLicensesAll()
    self.assertEqual(len(licenses), 4)
    # and confirm that this license ID doesn't exist in database
    license = self.db.getLicense(_id=license_id)
    self.assertIsNone(license)

  def test_can_start_adding_and_then_commit_licenses(self):
    l1_id = self.db.addLicense(name="newl1", category="cat", commit=False)
    l2_id = self.db.addLicense(name="newl2", category="cat", commit=False)
    self.db.commit()
    # confirm that we now have six licenses
    licenses = self.db.getLicensesAll()
    self.assertEqual(len(licenses), 6)

  def test_cannot_add_license_without_category(self):
    with self.assertRaises(TypeError):
      self.db.addLicense(name="oops")
    # confirm it wasn't added either
    license = self.db.getLicense(name="oops")
    self.assertIsNone(license)

  def test_cannot_add_license_without_existing_category(self):
    with self.assertRaises(ProjectDBInsertError):
      self.db.addLicense(name="oops", category="noSuchCategory")
    # confirm it wasn't added either
    license = self.db.getLicense(name="oops")
    self.assertIsNone(license)

  def test_cannot_add_license_with_duplicate_name(self):
    with self.assertRaises(ProjectDBInsertError):
      self.db.addLicense(name="DoAnything", category="cat")

  def test_can_edit_license_name(self):
    self.db.changeLicenseName(name="293PageEULA", newName="432PageEULA")
    license = self.db.getLicense(name="432PageEULA")
    self.assertEqual(license._id, 3)

  def test_cannot_edit_license_name_that_does_not_exist(self):
    with self.assertRaises(ProjectDBUpdateError):
      self.db.changeLicenseName(name="invalid", newName="this will fail")

  def test_cannot_change_license_name_to_existing_name(self):
    with self.assertRaises(ProjectDBUpdateError):
      self.db.changeLicenseName(name="293PageEULA", newName="DoAnything")

  def test_can_edit_license_category(self):
    self.db.changeLicenseCategory(name="293PageEULA", newCat="a category")
    license = self.db.getLicense(name="293PageEULA")
    self.assertEqual(license.category.name, "a category")

  def test_cannot_change_license_to_category_that_does_not_exist(self):
    with self.assertRaises(ProjectDBUpdateError):
      self.db.changeLicenseCategory(name="293PageEULA", newCat="invalid")

  def test_can_get_max_license_id(self):
    lic_id = self.db.getLicenseMaxID()
    self.assertEqual(4, lic_id)
Esempio n. 12
0
class ProjectDBUnitTestSuite(unittest.TestCase):
    """spdxLicenseManager unit test suite for DB initialization and lifecycle."""
    def setUp(self):
        # create and initialize an in-memory database
        self.db = ProjectDB()
        self.db.createDB(":memory:")
        self.db.initializeDBTables()

    def tearDown(self):
        self.db.closeDB()
        self.db = None

    def test_can_create_new_database(self):
        # don't use db from setUp(); create new in-memory DB from scratch
        dbnew = ProjectDB()
        dbnew.createDB(":memory:")
        self.assertTrue(dbnew.isOpened())
        self.assertFalse(dbnew.isInitialized())
        dbnew.closeDB()

    @mock.patch('slm.projectdb.os.path.exists', return_value=True)
    def test_cannot_create_new_database_if_file_already_exists(
            self, os_exists):
        dbnew = ProjectDB()
        with self.assertRaises(ProjectDBConfigError):
            dbnew.createDB("/tmp/fake/existing.db")

    def test_that_initialized_db_reports_as_initialized(self):
        self.assertTrue(self.db.isInitialized())

    def test_that_closed_db_reports_as_uninitialized(self):
        # don't use db from setUp(); create new in-memory DB from scratch
        dbnew = ProjectDB()
        dbnew.createDB(":memory:")
        # and then close it
        dbnew.closeDB()
        self.assertFalse(dbnew.isInitialized())
        self.assertIsNone(dbnew.session)
        self.assertIsNone(dbnew.engine)

    def test_can_open_existing_db(self):
        # create in temporary directory on disk, so we can re-open DB
        # (testfixtures will wipe out the directory at end of test)
        with TempDirectory() as td:
            dbPath = os.path.join(td.path, "tmp.db")
            dbnew = ProjectDB()
            dbnew.createDB(dbPath)
            dbnew.initializeDBTables()
            dbnew.closeDB()
            # and reopen it
            dbnew.openDB(dbPath)
            self.assertTrue(dbnew.isOpened())
            self.assertTrue(dbnew.isInitialized())
            dbnew.closeDB()

    def test_cannot_open_in_memory_db(self):
        dbnew = ProjectDB()
        with self.assertRaises(ProjectDBConfigError):
            dbnew.openDB(":memory:")

    def test_open_db_fails_if_invalid_magic_number(self):
        # create in temporary directory on disk, so we can re-open it
        # (testfixtures will wipe out the directory at end of test)
        with TempDirectory() as td:
            dbPath = os.path.join(td.path, "tmp.db")
            dbnew = ProjectDB()
            dbnew.createDB(dbPath)
            dbnew.initializeDBTables()

            # set invalid magic number
            query = dbnew.session.query(Config).filter(Config.key == "magic")
            query.update({Config.value: "invalidMagic"})
            dbnew.session.commit()
            dbnew.closeDB()

            # and reopen it
            with self.assertRaises(ProjectDBConfigError):
                dbnew.openDB(dbPath)
            self.assertFalse(dbnew.isOpened())
            self.assertFalse(dbnew.isInitialized())

    def test_cannot_open_some_random_file_as_db(self):
        # create in temporary directory on disk, so we can re-open it
        # (testfixtures will wipe out the directory at end of test)
        with TempDirectory() as td:
            fakeDBPath = os.path.join(td.path, "tmp.txt")
            with open(fakeDBPath, "w") as f:
                f.write("some random text")

            dbnew = ProjectDB()
            with self.assertRaises(ProjectDBConfigError):
                dbnew.openDB(fakeDBPath)
            self.assertFalse(dbnew.isOpened())
            self.assertFalse(dbnew.isInitialized())