def test_table_save_POST_failure(self): """Test failure when saving data using POST edit method.""" t = AssessTable(TestData, "") t.load(False, []) # Test bad decimal POST = {"('a1', 'b1', 'c1', 'value')": 'bad_decimal'} t.save_POST(POST) self.assertEqual(str(t.errors.pop()), str(NotDecimalError('bad_decimal', TestData))) self.assertEqual(t.records[('a1', 'b1', 'c1', 'value')].get_value(), '1,000') # Test bad POST key - item label POST = {"('bad_item', 'b1', 'c1', 'value')": '1'} t.save_POST(POST) self.assertEqual( str(t.errors.pop()), str( KeyNotFound("bad_item in ('bad_item', 'b1', 'c1', 'value')", TestData))) self.assertEqual(t.records[('a1', 'b1', 'c1', 'value')].get_value(), '1,000') # Test bad value field POST = {"('a1', 'b1', 'c1', 'bad_value_field')": '1'} t.save_POST(POST) self.assertEqual(str(t.errors.pop()), str(NoFieldError('bad_value_field', TestData))) self.assertEqual(t.records[('a1', 'b1', 'c1', 'value')].get_value(), '1,000')
def test_table_render_table_context(self): """Test multicolumn row c""" t = AssessTable(TestData, "") context = t.render_table_context('testitemc', False, []) self.assertEqual(context['header_list_index'], ['testitema', 'testitemb']) self.assertEqual(context['header_list_items'], ['c1', 'c2']) self.assertEqual(context['row_list'], rows_c)
def test_get_version_metric(self): """Test the history context (list of versions).""" t = AssessTable(TestData, "current") t.load(False) vd = t.get_version_metric() self.assertEqual(vd['model_name'], 'testdata') self.assertEqual(vd['size'], 8) self.assertEqual(vd['dimension'], '{2 x 2 x 2}') self.assertEqual(vd['metric'], sum([1, 2, 3, 4, 5, 6, 7, 8]) / 8) self.assertEqual(vd['cells'], 8) self.assertEqual(vd['changes'], 4)
def test_table_commit_form_context(self): """Test that all elements in context is present.""" key_list1 = [ 'table_commit_heading', 'table_commit_notice', 'table_commit_submit', 'i18n_user', 'i18n_label', 'i18n_note', 'table_commit_notable' ] t = AssessTable(TestData, "") context = t.get_commit_form_context() key_list2 = context.keys() for e in key_list1: self.assertIn(e, key_list2)
def test_table_save_CSV_success(self): """Test saving of data model through CSV""" t = AssessTable(TestData, "") t.load(False, []) # Test bad decimal csv_string = "testitema\ttestitemb\ttestitemc\tvalue\na1\tb1\tc1\t10" POST = {'csv_string': csv_string, 'column_field': 'value'} t.save_CSV(POST) t = AssessTable(TestData, "proposed") t.load(False, []) self.assertEqual(t.errors, []) self.assertEqual(t.records[('a1', 'b1', 'c1', 'value')].get_value(), '10,000')
def test_parse_dataframe_data_success(self): """Test parsing of dataframe into data_model in TableIO """ # TODO: look for ways that this function might fail and test # Test fully loaded table (8 values) in POST t = AssessTable(TestData,'1') t.load(False) tIO = AssessTableIO(TestData,delimiters) tIO.keys.set_headers('value') DF = [] # We must unpack key/value from record, as objects cannot expected to # identical between dataframes and load (TODO: why not???) for key,record in tIO.parse_dataframe(dfd).items(): DF.append({key: record.get_value()}) DB = [] for key,record in t.records.items(): DB.append({key: record.get_value()}) print_traceback(tIO.errors) self.assertEqual(tIO.errors,[]) self.assertEqual(DB,DF)
def test_table_display_a(self): """Test multicolumn with a as column""" t = AssessTable(TestMappings, "") t.load(False, []) t.set_rows('testitema') context = t.get_context() self.assertEqual(context['header_list_index'], ['testitemb']) self.assertEqual(context['header_list_items'], ['a1', 'a2']) self.assertEqual(context['row_list'], rows_a)
def test_table_get_CSV_form_context(self): """Test get CSV form context.""" t = AssessTable(TestData, "") context = t.get_CSV_form_context() column_field_choices = [{ 'label': 'testitema', 'name': 'Testitema' }, { 'label': 'testitemb', 'name': 'Testitemb' }, { 'checked': ' checked', 'label': 'testitemc', 'name': 'Testitemc' }, { 'label': 'value', 'name': 'Value' }] self.assertEqual(context['column_field_choices'], column_field_choices) self.assertEqual(context['model_name'], 'testdata')
def test_table_save_POST_success(self): """Test saving data using POST edit method.""" t = AssessTable(TestData, "") t.load(False, []) POST = { "('a1', 'b1', 'c1', 'value')": '10', "('a1', 'b1', 'c2', 'value')": '11', } t.save_POST(POST) # Test no errors and that other records were unchanged self.assertEqual(t.errors, []) self.assertEqual(t.records[('a1', 'b2', 'c1', 'value')].get_value(), '3,000') # Load proposed and test that the changes took effect t = AssessTable(TestData, "proposed") t.load(False, []) self.assertEqual(t.records[('a1', 'b1', 'c1', 'value')].get_value(), '10,000') self.assertEqual(t.records[('a1', 'b1', 'c2', 'value')].get_value(), '11,000')
def test_table_commit_success(self): """Test saving data using POST edit method.""" t = AssessTable(TestData, "") t.load(False, []) POST = { "('a1', 'b1', 'c1', 'value')": '10', "('a1', 'b1', 'c2', 'value')": '11', } t.save_POST(POST) # Test no errors and that other records were unchanged self.assertEqual(t.errors, []) self.assertEqual(t.records[('a1', 'b2', 'c1', 'value')].value, Decimal('3')) # Load proposed and test that the changes took effect version_info = {'label': 'new', 'user': '******', 'note': 'haha'} t.commit_rows(version_info) t.load(False, []) self.assertEqual(t.records[('a1', 'b1', 'c1', 'value')].value, Decimal('10')) self.assertEqual(t.records[('a1', 'b1', 'c2', 'value')].value, Decimal('11'))
def test_table_display_b(self): """Test multicolumn row b""" t = AssessTable(TestData, "") t.load(False, []) t.set_rows('testitemb') context = t.get_context() self.assertEqual(context['header_list_index'], ['testitema', 'testitemc']) self.assertEqual(context['header_list_items'], ['b1', 'b2']) self.assertEqual(context['row_list'], rows_b)
def test_table_display_val(self): """Test one-column value table""" t = AssessTable(TestData, "") t.load(False, []) t.set_rows('value') context = t.get_context() self.assertEqual(context['header_list_index'], ['testitema', 'testitemb', 'testitemc']) self.assertEqual(context['header_list_items'], ['value']) self.assertEqual(context['row_list'], rows_v2)
def test_table_display_c_nd(self): """Test multicolumn row c but with one cell missing""" t = AssessTable(TestData, "") t.load(False, []) t.records.pop(('a2', 'b2', 'c2', 'value')) t.set_rows('testitemc') context = t.get_context() self.assertEqual(context['header_list_index'], ['testitema', 'testitemb']) self.assertEqual(context['header_list_items'], ['c1', 'c2']) self.assertEqual(context['row_list'], rows_c_nd)
def test_table_display_c_bad_field(self): """Test multicolumn row c, table must default to model.column_field""" t = AssessTable(TestData, "") t.load(False, []) # PRoviding a bad field name make table use default model.column_field t.set_rows('bad_field_name') context = t.get_context() self.assertEqual(context['header_list_index'], ['testitema', 'testitemb']) self.assertEqual(context['header_list_items'], ['c1', 'c2']) self.assertEqual(context['row_list'], rows_c)
def test_revert_proposed(self): """Test revert table functionality.""" # In our setup, we have 12 records self.assertEqual(TestData.objects.count(), 12) # Then add one proposed record - we now have 13 t = AssessTable(TestData, "") t.load(False, []) POST = { "('a1', 'b1', 'c1', 'value')": '99', } t.save_POST(POST) self.assertEqual(TestData.objects.count(), 13) # Then revert the new proposed record, now we're back to 12 again t.revert_proposed() self.assertEqual(TestData.objects.count(), 12)
def test_count_proposed(self): """Test count proposed functionality.""" # In our setup, we have 12 records self.assertEqual(TestData.objects.count(), 12) # Then add one proposed record - we now have 13 t = AssessTable(TestData, "") t.load(False, []) POST = { "('a1', 'b1', 'c1', 'value')": '99', } t.save_POST(POST) self.assertEqual(t.count_db_records('proposed', changes=True), 1)
def test_table_display_archived2(self): """Test one-column archived value table""" t = AssessTable(TestData, '2') vKwargs = {'version_first__lte': 2} self.assertEqual(t.version.version_id, 2) self.assertEqual(t.version.kwargs_filter_load(False), vKwargs) t.load(False, []) t.set_rows('value') context = t.get_context() self.assertEqual(context['header_list_index'], ['testitema', 'testitemb', 'testitemc']) self.assertEqual(context['header_list_items'], ['value']) self.assertEqual(context['row_list'], rows_v2)
def test_table_save_changed_records_failure(self): """Test save_changed_records. """ # First test modifying an existing record t = AssessTable(TestData, "") t.load(False, []) r = TestData.objects.get(id=1) r.value = "bad_decimal_value" t.changed_records = {r.get_key(): r} try: t.save_changed_records(t.changed_records) except NotCleanRecord as e1: r2 = TestData.objects.get(id=1) r2.value = "bad_decimal_value" try: r2.full_clean() except ValidationError as e3: self.assertEqual(str(e1), str(NotCleanRecord(r, e3)))
def test_table_save_changed_records_success(self): """Test save_changed_records. """ record_dict = { 'testitema': 'a1', 'testitemb': 'b1', 'testitemc': 'c1', 'value': '10,000' } r = TestData() r.set_fk_labels_objects() r.set_from_record_dict(record_dict) # First test modifying an existing record t = AssessTable(TestData, "") t.load(False, []) t.changed_records = {r.get_key(): r} t.save_changed_records(t.changed_records) t = AssessTable(TestData, "proposed") # Load proposed records t.load(False, []) self.assertEqual(t.records[r.get_key()], r) # Then delete the old (id=1) new record (id=13) and insert it to test insertion as well TestData.objects.filter(id=1).delete() TestData.objects.filter(id=13).delete() t.load(False, []) t.changed_records = {r.get_key(): r} t.save_changed_records(t.changed_records) t = AssessTable(TestData, "proposed") # Load proposed records t.load(False, []) self.assertEqual(t.records[r.get_key()].value, r.value)