def test_format_value_for_type_text(self): schema_field = SchemaField('coffee', 200, 'TEXT') string_test_input = '100' num_test_input = 52 self.assertEqual('100', schema_field.format_value_for_type(string_test_input)) self.assertTrue('52', schema_field.format_value_for_type(num_test_input))
def test_format_value_for_type_boolean(self): schema_field = SchemaField('coffee', 200, 'BOOLEAN') string_test_input = '1' num_test_input = 1 bad_test_input = 'marshmellow' self.assertTrue(schema_field.format_value_for_type(string_test_input)) self.assertTrue(schema_field.format_value_for_type(num_test_input)) self.assertEqual(bad_test_input, schema_field.format_value_for_type(bad_test_input))
def test_format_value_for_type_integer(self): schema_field = SchemaField('coffee', 200, 'INTEGER') string_test_input = '5' num_test_input = 5 bad_test_input = 'juniper' self.assertEqual(5, schema_field.format_value_for_type(string_test_input)) self.assertEqual(5, schema_field.format_value_for_type(num_test_input)) self.assertEqual(bad_test_input, schema_field.format_value_for_type(bad_test_input))
def test_init(self): value = 18 schema_field = SchemaField('name', 0, 'TEXT') record_item = RecordItem(value, schema_field) self.assertEqual(value, record_item.value) self.assertEqual(schema_field, record_item.schema_field)
def test_add_schema_field(self): schema_field = SchemaField('', 1, '') schema = Schema() schema.add_schema_field(schema_field) schema.add_schema_field(schema_field) self.assertEqual(2, len(schema.schema_fields))
def test_add_record_item(self): schema_field = SchemaField('', 0, 'TEXT') record_item = RecordItem(1, schema_field) record = Record() record.add_record_item(record_item) record.add_record_item(record_item) self.assertEqual(2, len(record.record_items))
def test_init(self): field_name = 'bananas' field_length = 32 field_type = 'TEXT' schema_field = SchemaField(field_name, field_length, field_type) self.assertEqual(field_name, schema_field.field_name) self.assertEqual(field_length, schema_field.field_length) self.assertEqual(field_type, schema_field.field_type)
def convert_file_to_schema(cls, file): schema = Schema() csv_file_reader = csv.reader(file, delimiter=' ') for row in csv_file_reader: fields = row[0].split(',') field_name = fields[0] field_length = fields[1] field_type = fields[2] schema_field = SchemaField(field_name, field_length, field_type) schema.add_schema_field(schema_field) file.close() return schema
def test_to_dict(self): record = Record() schema_field_text = SchemaField('field1', 10, 'TEXT') record_item_text = RecordItem('hello', schema_field_text) record.add_record_item(record_item_text) schema_field_int = SchemaField('field2', 11, 'INTEGER') record_item_int = RecordItem(18, schema_field_int) record.add_record_item(record_item_int) schema_field_bool = SchemaField('field3', 1, 'BOOLEAN') record_item_bool = RecordItem(0, schema_field_bool) record.add_record_item(record_item_bool) expected_output = { 'field1': 'hello', 'field2': 18, 'field3': False } self.assertEqual(expected_output, record.to_dict())