Skip to content

Commit

Permalink
Merge pull request #4 from LucoMoro/DDRoot-test
Browse files Browse the repository at this point in the history
Add tests for DDRoot + subclasses
  • Loading branch information
LucoMoro authored Mar 9, 2024
2 parents 9b5c6c9 + 0541b7f commit d534ee4
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/DiscOrDance-Tests/DDGlobalConfigurationTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"
A DDGlobalConfigurationTest is a test class for testing the behavior of DDGlobalConfiguration
"
Class {
#name : #DDGlobalConfigurationTest,
#superclass : #TestCase,
#category : #'DiscOrDance-Tests-Base'
}

{ #category : #test }
DDGlobalConfigurationTest >> testLimitMessages [
| aConfiguration |
aConfiguration := DDGlobalConfiguration new.
aConfiguration limitMessages: true.
self assert: aConfiguration limitMessages equals: true.
]

{ #category : #test }
DDGlobalConfigurationTest >> testMessagesPerChannel [
| aConfiguration |
aConfiguration := DDGlobalConfiguration new.
aConfiguration messagesPerChannel: 250.
self assert: aConfiguration messagesPerChannel equals: 250.
]
134 changes: 132 additions & 2 deletions src/DiscOrDance-Tests/DDScraperTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,150 @@ Class {
#name : #DDScraperTest,
#superclass : #DDTestCase,
#instVars : [
'scraper'
'scraper',
'workingDir',
'isTokenPresent',
'tokenValue'
],
#category : #'DiscOrDance-Tests-Scraper'
}

{ #category : #running }
DDScraperTest >> setUp [
"Hooks that subclasses may override to define the fixture of test."
| modelsDir tempDir serverFiles |
super setUp.
scraper := DDScraper new.
workingDir := FileSystem disk workingDirectory.
isTokenPresent := false.
tokenValue := nil.
modelsDir := workingDir / 'models'.

"Backup files in models folder (if present)"
modelsDir exists
ifTrue: [
tempDir := workingDir / 'temp'.
tempDir exists ifTrue: [ ] ifFalse: [ FileSystem disk createDirectory: tempDir] .
serverFiles := modelsDir children.
serverFiles do: [ :each |
FileSystem disk copy: each to: (tempDir / (each basename)).
FileSystem disk delete: (modelsDir / (each basename)).
].
FileSystem disk delete: modelsDir.
]
ifFalse: [
"Do nothing"
].

"Backup Discord token"
Smalltalk os environment at: 'DISCORD_API_TOKEN' ifPresent: [ :value |
isTokenPresent := true.
tokenValue := value.
Smalltalk os environment removeKey: 'DISCORD_API_TOKEN'.
].

]

{ #category : #running }
DDScraperTest >> tearDown [
| modelsDir tempDir serverFiles testFiles |
workingDir := FileSystem disk workingDirectory.
modelsDir := workingDir / 'models'.
tempDir := workingDir / 'temp'.

"Reset models folder"
modelsDir exists
ifTrue: [
testFiles := modelsDir children.
testFiles do: [ :each |
FileSystem disk delete: (modelsDir / (each basename)).
].
FileSystem disk delete: modelsDir.
]
ifFalse: [
].

"Restore original files (if present)"
tempDir exists
ifTrue: [
FileSystem disk createDirectory: modelsDir.
serverFiles := tempDir children.
serverFiles do: [ :each |
FileSystem disk copy: each to: (modelsDir / (each basename)).
FileSystem disk delete: (tempDir / (each basename)).
].
FileSystem disk delete: tempDir.
]
ifFalse: [
].

"Restore Discord token"
Smalltalk os environment at: 'DISCORD_API_TOKEN' ifPresent: [ :value |
Smalltalk os environment removeKey: 'DISCORD_API_TOKEN'.
].
isTokenPresent
ifTrue: [
Smalltalk os environment at: 'DISCORD_API_TOKEN' put: tokenValue.
].

super tearDown.
]

{ #category : #test }
DDScraperTest >> testBot [
"Test with no Discord API token"
self should: [ scraper bot ] raise: Error.
]

{ #category : #tests }
DDScraperTest >> testBot2 [
"Test with Discord API token"
Smalltalk os environment at: 'DISCORD_API_TOKEN' put: 'test-token'.
self assert: scraper bot token equals: 'test-token'.
]

{ #category : #tests }
DDScraperTest >> testGetLocalServersList [
"Test with non-existing models folder"
| list |
list := scraper getLocalServersList.
self assert: list size equals: 0.


"Original test by wolfenmark"
"| list |
list := scraper getLocalServersList.
(list includes: 'Wolfenmark bot development server') ifFalse: [
scraper createPartialModel: 'Wolfenmark bot development server'.
list := scraper getLocalServersList.
].
self assert: list isNotEmpty.
self assert: (list includes: 'Wolfenmark bot development server')
self assert: (list includes: 'Wolfenmark bot development server')"
]

{ #category : #tests }
DDScraperTest >> testGetLocalServersList2 [
"Test with no files in the models folder"
| list |
FileSystem disk createDirectory: (workingDir / 'models').
list := scraper getLocalServersList.
self assert: list size equals: 0.

]

{ #category : #tests }
DDScraperTest >> testGetLocalServersList3 [
"Test with one file in the models folder"
| list tempFilePath |
FileSystem disk createDirectory: (workingDir / 'models').
"tempFilePath := (workingDir / 'models' / 'my-test-server_clean_stats_v1_2.fl')."
tempFilePath := (workingDir / 'models' / 'my-test-server_clean_stats_v1.fl').
tempFilePath writeStreamDo: [ :stream | ].
list := scraper getLocalServersList.
self assert: list size equals: 1.
"self assert: (list at: 1) equals: ('my-test-server_clean_stats_v1_')."
self assert: (list at: 1) equals: ('my-test-server_clean_stats').
]

{ #category : #tests }
Expand All @@ -36,3 +156,13 @@ DDScraperTest >> testGetServersList [
self assert: list isNotEmpty.
self assert: (list includes: 'Wolfenmark bot development server')
]

{ #category : #test }
DDScraperTest >> testGlobalConfiguration [
| aConfiguration |
aConfiguration := DDGlobalConfiguration new limitMessages: true; messagesPerChannel: 1000.
scraper globalConfiguration: aConfiguration.

self assert: scraper globalConfiguration limitMessages equals: true.
self assert: scraper globalConfiguration messagesPerChannel equals: 1000.
]

0 comments on commit d534ee4

Please sign in to comment.