def test_get_set_export_serialized_value_single_select_field(data_fixture): user = data_fixture.create_user() group = data_fixture.create_group(user=user) imported_group = data_fixture.create_group(user=user) database = data_fixture.create_database_application(group=group) table = data_fixture.create_database_table(database=database) field = data_fixture.create_single_select_field(table=table) option_a = data_fixture.create_select_option(field=field, value="A", color="green") option_b = data_fixture.create_select_option(field=field, value="B", color="red") core_handler = CoreHandler() model = table.get_model() model.objects.create() model.objects.create(**{f"field_{field.id}_id": option_a.id}) model.objects.create(**{f"field_{field.id}_id": option_b.id}) exported_applications = core_handler.export_group_applications( group, BytesIO()) imported_applications, id_mapping = core_handler.import_applications_to_group( imported_group, exported_applications, BytesIO(), None) imported_database = imported_applications[0] imported_table = imported_database.table_set.all()[0] imported_field = imported_table.field_set.all().first().specific assert imported_table.id != table.id assert imported_field.id != field.id imported_model = imported_table.get_model() all = imported_model.objects.all() assert len(all) == 3 imported_row_1 = all[0] imported_row_2 = all[1] imported_row_3 = all[2] assert getattr(imported_row_1, f"field_{imported_field.id}") is None assert getattr(imported_row_2, f"field_{imported_field.id}_id") != option_a.id assert getattr(imported_row_2, f"field_{imported_field.id}").value == "A" assert getattr(imported_row_2, f"field_{imported_field.id}").color == "green" assert getattr(imported_row_3, f"field_{imported_field.id}_id") != option_b.id assert getattr(imported_row_3, f"field_{imported_field.id}").value == "B" assert getattr(imported_row_3, f"field_{imported_field.id}").color == "red"
def test_export_import_group_application(data_fixture): group = data_fixture.create_group() imported_group = data_fixture.create_group() database = data_fixture.create_database_application(group=group) data_fixture.create_database_table(database=database) handler = CoreHandler() exported_applications = handler.export_group_applications(group, BytesIO()) imported_applications, id_mapping = handler.import_applications_to_group( imported_group, exported_applications, BytesIO(), None) assert len(imported_applications) == 1 imported_database = imported_applications[0] assert imported_database.id != database.id assert imported_database.name == database.name assert imported_database.order == database.order assert imported_database.table_set.all().count() == 1 assert database.id in id_mapping["applications"] assert id_mapping["applications"][database.id] == imported_database.id
def test_import_export_link_row_field(data_fixture, user_tables_in_separate_db): user = data_fixture.create_user() imported_group = data_fixture.create_group(user=user) database = data_fixture.create_database_application(user=user, name="Placeholder") table = data_fixture.create_database_table(name="Example", database=database) customers_table = data_fixture.create_database_table( name="Customers", database=database ) field_handler = FieldHandler() core_handler = CoreHandler() link_row_field = field_handler.create_field( user=user, table=table, type_name="link_row", link_row_table=customers_table ) row_handler = RowHandler() c_row = row_handler.create_row(user=user, table=customers_table, values={}) c_row_2 = row_handler.create_row(user=user, table=customers_table, values={}) row = row_handler.create_row( user=user, table=table, values={f"field_{link_row_field.id}": [c_row.id, c_row_2.id]}, ) exported_applications = core_handler.export_group_applications( database.group, BytesIO() ) imported_applications, id_mapping = core_handler.import_applications_to_group( imported_group, exported_applications, BytesIO(), None ) imported_database = imported_applications[0] imported_tables = imported_database.table_set.all() imported_table = imported_tables[0] imported_customers_table = imported_tables[1] imported_link_row_field = imported_table.field_set.all().first().specific imported_link_row_relation_field = ( imported_customers_table.field_set.all().first().specific ) assert imported_table.id != table.id assert imported_table.name == table.name assert imported_customers_table.id != customers_table.id assert imported_customers_table.name == customers_table.name assert imported_link_row_field.id != link_row_field.id assert imported_link_row_field.name == link_row_field.name assert imported_link_row_field.link_row_table_id == imported_customers_table.id assert imported_link_row_relation_field.link_row_table_id == imported_table.id assert imported_link_row_field.link_row_relation_id == ( imported_link_row_relation_field.link_row_relation_id ) imported_c_row = row_handler.get_row( user=user, table=imported_customers_table, row_id=c_row.id ) imported_c_row_2 = row_handler.get_row( user=user, table=imported_customers_table, row_id=c_row_2.id ) imported_row = row_handler.get_row(user=user, table=imported_table, row_id=row.id) assert imported_row.id == row.id assert imported_c_row.id == c_row.id assert imported_c_row_2.id == c_row_2.id assert [ r.id for r in getattr(imported_row, f"field_{imported_link_row_field.id}").all() ] == [imported_c_row.id, imported_c_row_2.id]
def test_import_export_file_field(data_fixture, tmpdir, user_tables_in_separate_db): user = data_fixture.create_user() imported_group = data_fixture.create_group(user=user) database = data_fixture.create_database_application(user=user) table = data_fixture.create_database_table(database=database) field = data_fixture.create_file_field(table=table, name="File") storage = FileSystemStorage(location=str(tmpdir), base_url="http://localhost") handler = UserFileHandler() user_file = handler.upload_user_file(user, "test.txt", ContentFile(b"Hello World"), storage=storage) core_handler = CoreHandler() row_handler = RowHandler() model = table.get_model() row_1 = row_handler.create_row( user=user, table=table, values={ f"field_{field.id}": [{ "name": user_file.name, "visible_name": "a.txt" }] }, model=model, ) row_2 = row_handler.create_row( user=user, table=table, values={}, model=model, ) row_3 = row_handler.create_row( user=user, table=table, values={f"field_{field.id}": [{ "name": user_file.name }]}, model=model, ) files_buffer = BytesIO() exported_applications = core_handler.export_group_applications( database.group, files_buffer=files_buffer, storage=storage) # We expect that the exported zip file contains the user file used in the created # rows. with ZipFile(files_buffer, "r", ZIP_DEFLATED, False) as zip_file: assert zip_file.read(user_file.name) == b"Hello World" assert (exported_applications[0]["tables"][0]["rows"][0] [f"field_{field.id}"][0]["name"] == user_file.name) assert ( exported_applications[0]["tables"][0]["rows"][0][f"field_{field.id}"] [0]["original_name"] == user_file.original_name) assert exported_applications[0]["tables"][0]["rows"][1][ f"field_{field.id}"] == [] assert (exported_applications[0]["tables"][0]["rows"][2] [f"field_{field.id}"][0]["name"] == user_file.name) # Change the original name for enforce that the file is re-uploaded when saved. exported_applications[0]["tables"][0]["rows"][0][f"field_{field.id}"][0][ "original_name"] = "test2.txt" exported_applications[0]["tables"][0]["rows"][2][f"field_{field.id}"][0][ "original_name"] = "test2.txt" imported_applications, id_mapping = core_handler.import_applications_to_group( imported_group, exported_applications, files_buffer, storage) imported_database = imported_applications[0] imported_tables = imported_database.table_set.all() imported_table = imported_tables[0] imported_field = imported_table.field_set.all().first().specific imported_user_file = UserFile.objects.all()[1] import_row_1 = row_handler.get_row(user=user, table=imported_table, row_id=row_1.id) import_row_2 = row_handler.get_row(user=user, table=imported_table, row_id=row_2.id) import_row_3 = row_handler.get_row(user=user, table=imported_table, row_id=row_3.id) assert len(getattr(import_row_1, f"field_{imported_field.id}")) == 1 assert (getattr( import_row_1, f"field_{imported_field.id}")[0]["name"] == imported_user_file.name) assert (getattr( import_row_1, f"field_{imported_field.id}")[0]["visible_name"] == "a.txt") assert len(getattr(import_row_2, f"field_{imported_field.id}")) == 0 assert len(getattr(import_row_3, f"field_{imported_field.id}")) == 1 assert (getattr( import_row_3, f"field_{imported_field.id}")[0]["name"] == imported_user_file.name) assert (getattr( import_row_3, f"field_{imported_field.id}")[0]["visible_name"] == "test.txt") assert UserFile.objects.all().count() == 2 assert user_file.name != imported_user_file.name file_path = tmpdir.join("user_files", imported_user_file.name) assert file_path.isfile() assert file_path.open().read() == "Hello World"