PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/perf/cli_tools/soundwave/pandas_sqlite_test.py

https://github.com/chromium/chromium
Python | 78 lines | 62 code | 10 blank | 6 comment | 11 complexity | 574a378f3010726abf6b8ad5a5cdaa31 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  1. # Copyright 2018 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. try:
  5. import sqlite3
  6. except ImportError:
  7. pass
  8. import unittest
  9. import six
  10. from cli_tools.soundwave import pandas_sqlite
  11. from core.external_modules import pandas
  12. @unittest.skipIf(pandas is None, 'pandas not available')
  13. class TestPandasSQLite(unittest.TestCase):
  14. def testCreateTableIfNotExists_newTable(self):
  15. df = pandas_sqlite.DataFrame(
  16. [('bug_id', int), ('summary', str), ('status', str)], index='bug_id')
  17. con = sqlite3.connect(':memory:')
  18. try:
  19. self.assertFalse(pandas.io.sql.has_table('bugs', con))
  20. pandas_sqlite.CreateTableIfNotExists(con, 'bugs', df)
  21. self.assertTrue(pandas.io.sql.has_table('bugs', con))
  22. finally:
  23. con.close()
  24. def testCreateTableIfNotExists_alreadyExists(self):
  25. df = pandas_sqlite.DataFrame(
  26. [('bug_id', int), ('summary', str), ('status', str)], index='bug_id')
  27. con = sqlite3.connect(':memory:')
  28. try:
  29. self.assertFalse(pandas.io.sql.has_table('bugs', con))
  30. pandas_sqlite.CreateTableIfNotExists(con, 'bugs', df)
  31. self.assertTrue(pandas.io.sql.has_table('bugs', con))
  32. # It's fine to call a second time.
  33. pandas_sqlite.CreateTableIfNotExists(con, 'bugs', df)
  34. self.assertTrue(pandas.io.sql.has_table('bugs', con))
  35. finally:
  36. con.close()
  37. def testInsertOrReplaceRecords_tableNotExistsRaises(self):
  38. column_types = (('bug_id', int), ('summary', str), ('status', str))
  39. rows = [(123, 'Some bug', 'Started'), (456, 'Another bug', 'Assigned')]
  40. df = pandas_sqlite.DataFrame(column_types, index='bug_id', rows=rows)
  41. con = sqlite3.connect(':memory:')
  42. try:
  43. with self.assertRaises(AssertionError):
  44. pandas_sqlite.InsertOrReplaceRecords(con, 'bugs', df)
  45. finally:
  46. con.close()
  47. def testInsertOrReplaceRecords_existingRecords(self):
  48. column_types = (('bug_id', int), ('summary', str), ('status', str))
  49. rows1 = [(123, 'Some bug', 'Started'), (456, 'Another bug', 'Assigned')]
  50. df1 = pandas_sqlite.DataFrame(column_types, index='bug_id', rows=rows1)
  51. rows2 = [(123, 'Some bug', 'Fixed'), (789, 'A new bug', 'Untriaged')]
  52. df2 = pandas_sqlite.DataFrame(column_types, index='bug_id', rows=rows2)
  53. con = sqlite3.connect(':memory:')
  54. try:
  55. pandas_sqlite.CreateTableIfNotExists(con, 'bugs', df1)
  56. # Write first data frame to database.
  57. pandas_sqlite.InsertOrReplaceRecords(con, 'bugs', df1)
  58. df = pandas.read_sql('SELECT * FROM bugs', con, index_col='bug_id')
  59. self.assertEqual(len(df), 2)
  60. self.assertEqual(df.loc[123]['status'], 'Started')
  61. # Write second data frame to database.
  62. pandas_sqlite.InsertOrReplaceRecords(con, 'bugs', df2)
  63. df = pandas.read_sql('SELECT * FROM bugs', con, index_col='bug_id')
  64. self.assertEqual(len(df), 3) # Only one extra record added.
  65. self.assertEqual(df.loc[123]['status'], 'Fixed') # Bug is now fixed.
  66. six.assertCountEqual(self, df.index, (123, 456, 789))
  67. finally:
  68. con.close()