Skip to content

Commit

Permalink
[DI-1132] Add Delete By Id Functionality (#47)
Browse files Browse the repository at this point in the history
* Add IsDeleteOperation to DataMap

* Allow UI to set,edit IsDeleteOperation

* Enable DataMap editing for delete by id use case

* Updating DataImport exe. Successfully deleting by id

* Simplify UI for use case

* Delete by Id disclaimer

* Naming and formatting

* DataMapSerializerTests coverage of new serialize and deserialize paths

* Update pkg with vulnerabiities

* Fix param order

* troubleshooting build

* Fix python.exe process tests having to do with accessing process name after process has completed.

* Add Web tests

* use await on migrate commands

* Try only one test project

* Force test projects to run in series

* Move delete serialization to dedicated class
  • Loading branch information
simpat-adam authored Dec 7, 2023
1 parent 7af04dc commit 8ac5a9e
Show file tree
Hide file tree
Showing 30 changed files with 2,939 additions and 81 deletions.
2 changes: 1 addition & 1 deletion DataImport.Common.Tests/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
{
"PythonExecutableLocation": "..\\..\\..\\..\\.tools\\python\\python.exe"
}
2 changes: 1 addition & 1 deletion DataImport.Common/DataImport.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void WriteToCollection(DataReceivedEventArgs eventArgs, Collection<string> colle
{
using var process = Process.Start(startInfo);

var processName = process.ProcessName;
var processName = process.HasExited ? "Exited python process" : process.ProcessName;
_logger.LogInformation("External preprocess {ProcessName} started", processName);

process.OutputDataReceived += (_, e) => { WriteToCollection(e, outputLines); };
Expand Down
135 changes: 97 additions & 38 deletions DataImport.Models.Tests/DataMapSerializerTests.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion DataImport.Models/DataMapSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private List<DataMapper> DeserializeObject(IReadOnlyList<ResourceMetadata> nodeM

foreach (var node in nodes)
{
if (nodeMetadatas.All(x => x.Name != node.Name))
if (node.Name != "Id" && nodeMetadatas.All(x => x.Name != node.Name))
throw new InvalidOperationException(
$"Cannot deserialize mappings from JSON, because the key '{node.Name}' should not exist " +
$"according to the metadata for resource '{_resourcePath}'.");
Expand Down
2 changes: 2 additions & 0 deletions DataImport.Models/Datamap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ public DataMap()
public Script FileProcessorScript { get; set; }

public ICollection<DataMapAgent> DataMapAgents { get; set; }

public bool IsDeleteOperation { get; set; }
}
}
86 changes: 86 additions & 0 deletions DataImport.Models/DeleteDataMapSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// SPDX-License-Identifier: Apache-2.0
// Licensed to the Ed-Fi Alliance under one or more agreements.
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace DataImport.Models
{
public class DeleteDataMapSerializer
{
public string Serialize(DataMapper[] mappings)
{
return SerializeObjectForDeleteById(mappings.Single()).ToString(Formatting.Indented);
}

public DataMapper[] Deserialize(string jsonMap)
{
JObject jobject;
try
{
jobject = JObject.Parse(jsonMap);
}
catch (Exception exception)
{
throw new ArgumentException(
"Cannot deserialize mappings from JSON, because the map text is not a valid JSON object. " +
"Check the inner exception for details. Invalid JSON Map text:" +
$"{Environment.NewLine}{Environment.NewLine}{jsonMap}"
, exception);
}

return Deserialize(jobject);
}
public DataMapper[] Deserialize(JObject jsonMap)
{
return DeserializeObjectForDeleteById(jsonMap).ToArray();
}

private JObject SerializeObjectForDeleteById(DataMapper node)
{
var result = new JObject { new JProperty("Id", new JObject { new JProperty("Column", node.SourceColumn) }) };
return result;
}

private List<DataMapper> DeserializeObjectForDeleteById(JToken objectToken)
{
var jobject = objectToken as JObject;

if (jobject == null)
throw new InvalidOperationException(
"Cannot deserialize mappings from JSON, because an object literal was expected. " +
"Instead, found: " +
$"{objectToken.ToString(Formatting.Indented)}");

var result = new List<DataMapper>();

var nodes = jobject.Children().Cast<JProperty>().ToArray();

var node = nodes.Single(n => n.Name == "Id");

var propertyValue = node.Children().Single();

var sourceColumn = ((JObject) propertyValue).Children().Cast<JProperty>().Single().Value;

result.Add(new DataMapper() { Name = "Id", SourceColumn = DeserializeRawValue(sourceColumn) });

return result;
}

private static string DeserializeRawValue(object rawValue)
{
if (rawValue == null)
return null;

if (rawValue is bool)
return rawValue.ToString().ToLower();

return rawValue.ToString();
}
}
}
Loading

0 comments on commit 8ac5a9e

Please sign in to comment.