Skip to content

Commit 243a01a

Browse files
committed
update tests for context serialization
1 parent eba1e80 commit 243a01a

File tree

6 files changed

+25
-20
lines changed

6 files changed

+25
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
## Unreleased
44

55
### Added
6-
- Plugin based serialization capability to context
6+
- Plugin-based serialization capability to context
77
- Documentation and safety for Tables-kt-dataframe
88
- Ability to unwrap NamedData
99

1010
### Changed
11+
- Change context `modify` to context `deriveContext` and add modification suffix as parameter.
1112
- Updated Exposed in tables to 1.0.0
1213
- **BREAKING** DataTree builders refactor.
1314
- Updated Exposed in tables to 1.0.0

dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/context/Context.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public open class Context internal constructor(
8888
@ThreadSafe
8989
public fun buildContext(name: Name? = null, block: ContextBuilder.() -> Unit = {}): Context {
9090
val existing = name?.let { childrenContexts[name] }
91-
return existing?.modify(block) ?: ContextBuilder(this, name).apply(block).build().also {
91+
return existing?.deriveContext(block = block) ?: ContextBuilder(this, name).apply(block).build().also {
9292
childrenContexts[it.name] = it
9393
}
9494
}
@@ -121,8 +121,8 @@ public open class Context internal constructor(
121121
*/
122122
public val serializationModule: SerializersModule by lazy {
123123
val pluginModules = plugins.mapNotNull { it.serializerModule }
124-
if(pluginModules.isEmpty()) {
125-
parent?.serializationModule ?: SerializersModule{}
124+
if (pluginModules.isEmpty()) {
125+
parent?.serializationModule ?: SerializersModule {}
126126
} else {
127127
val pluginModule = pluginModules.reduce { acc, module -> acc + module }
128128
parent?.serializationModule?.overwriteWith(pluginModule) ?: pluginModule

dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/context/ContextBuilder.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,21 @@ public class ContextBuilder internal constructor(
9797
}
9898

9999
/**
100-
* Check if current context contains all plugins required by the builder and return it does or forks to a new context
100+
* Check if the current context contains all plugins required by the builder and properties are the same and return it does or forks to a new context
101101
* if it does not.
102102
*/
103103
@DFExperimental
104-
public fun Context.modify(block: ContextBuilder.() -> Unit): Context {
104+
public fun Context.deriveContext(deriveSuffix: String = "mod", block: ContextBuilder.() -> Unit): Context {
105105

106106
fun Context.contains(factory: PluginFactory<*>, meta: Meta): Boolean {
107107
val loaded = plugins[factory.tag] ?: return false
108108
return loaded.meta == meta
109109
}
110110

111-
val builder = ContextBuilder(this, name + "mod", properties).apply(block)
112-
val requiresFork = builder.factories.any { (factory, meta) ->
111+
val builder = ContextBuilder(this, name + deriveSuffix, properties).apply(block)
112+
val requiresFork = !Meta.equals(properties,builder.meta) || builder.factories.any { (factory, meta) ->
113113
!contains(factory, meta)
114-
} || ((properties as Meta) == builder.meta)
114+
}
115115

116116
return if (requiresFork) builder.build() else this
117117
}

dataforge-context/src/commonTest/kotlin/space/kscience/dataforge/context/ContextSerializerTest.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import kotlinx.serialization.modules.SerializersModule
99
import kotlinx.serialization.modules.polymorphic
1010
import kotlinx.serialization.modules.subclass
1111
import kotlinx.serialization.serializer
12-
import space.kscience.dataforge.names.asName
1312
import kotlin.test.Test
13+
import kotlin.test.assertEquals
1414
import kotlin.test.assertFails
1515
import kotlin.test.assertTrue
1616

@@ -60,7 +60,7 @@ class ContextSerializerTest {
6060

6161
}
6262

63-
class PluginB2 : AbstractPlugin() {
63+
class PluginBFail : AbstractPlugin() {
6464
override val tag: PluginTag = PluginTag("B2")
6565

6666
override val serializerModule: SerializersModule = SerializersModule {
@@ -74,7 +74,7 @@ class ContextSerializerTest {
7474

7575
@Test
7676
fun testContextWithSerializers() {
77-
val context = Context("test") {
77+
val context = Context {
7878
plugin(PluginA())
7979
plugin(PluginB())
8080
}
@@ -88,30 +88,33 @@ class ContextSerializerTest {
8888

8989
@Test
9090
fun testContextInheritance() {
91-
val parentContext = Context("parent") {
91+
val parentContext = Context {
9292
plugin(PluginA())
93+
plugin(PluginBFail())
9394
}
9495

95-
val childContext = parentContext.buildContext("child".asName()) {
96+
val childContext = parentContext.buildContext {
9697
plugin(PluginB())
9798
}
9899

99-
100100
val stringA = childContext.json.encodeToString(serializer<Body>(), BodyA())
101101

102102
assertTrue { childContext.json.decodeFromString<Body>(stringA) is BodyA }
103103
val stringB = childContext.json.encodeToString(serializer<Body>(), BodyB())
104104

105105
assertTrue { childContext.json.decodeFromString<Body>(stringB) is BodyB }
106+
107+
assertEquals("\"Fail\"", parentContext.json.encodeToString(serializer<Body>(), BodyB()))
108+
106109
}
107110

108111

109112
@Test
110113
fun testConflict() {
111-
val context = Context("test") {
114+
val context = Context {
112115
plugin(PluginA())
113116
plugin(PluginB())
114-
plugin(PluginB2())
117+
plugin(PluginBFail())
115118
}
116119

117120
assertFails {

dataforge-context/src/commonTest/kotlin/space/kscience/dataforge/context/ContextTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ContextTest {
2020

2121
@Test
2222
fun testPluginManager() {
23-
val context = Context("test") {
23+
val context = Context{
2424
plugin(DummyPlugin())
2525
}
2626
val members = context.gather<Name>("test")

dataforge-context/src/jvmMain/kotlin/space/kscience/dataforge/descriptors/reflectiveDescriptors.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory
77
import space.kscience.dataforge.meta.descriptors.MetaDescriptor
88
import space.kscience.dataforge.meta.descriptors.MetaDescriptorBuilder
99
import space.kscience.dataforge.misc.DFExperimental
10+
import java.net.URI
1011
import java.net.URL
1112
import kotlin.reflect.KAnnotatedElement
1213
import kotlin.reflect.KProperty
@@ -60,7 +61,7 @@ public fun MetaDescriptorBuilder.forAnnotatedElement(element: KAnnotatedElement)
6061

6162
is DescriptorResource -> loadDescriptorFromResource(it)
6263

63-
is DescriptorUrl -> loadDescriptorFromUrl(URL(it.url))
64+
is DescriptorUrl -> loadDescriptorFromUrl(URI.create(it.url).toURL())
6465
}
6566
}
6667
}
@@ -73,7 +74,7 @@ public fun MetaDescriptorBuilder.forProperty(property: KProperty<*>) {
7374

7475
is DescriptorResource -> loadDescriptorFromResource(it)
7576

76-
is DescriptorUrl -> loadDescriptorFromUrl(URL(it.url))
77+
is DescriptorUrl -> loadDescriptorFromUrl(URI.create(it.url).toURL())
7778
}
7879
}
7980
}

0 commit comments

Comments
 (0)